Skip to content

Commit

Permalink
perf(ts): simple monomorphic message parse
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Aug 15, 2024
1 parent 4fcdd33 commit 3a86677
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
12 changes: 10 additions & 2 deletions typescript/core/src/McapStreamReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { crc32 } from "@foxglove/crc";

import Reader from "./Reader";
import { MCAP_MAGIC } from "./constants";
import { parseMagic, parseRecord } from "./parse";
import { monoParseMessage, parseMagic, parseRecord } from "./parse";
import { Channel, DecompressHandlers, McapMagic, TypedMcapRecord, TypedMcapRecords } from "./types";

type McapReaderOptions = {
Expand Down Expand Up @@ -233,7 +233,15 @@ export default class McapStreamReader {
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
const chunkReader = new Reader(view);
let chunkRecord;
while ((chunkRecord = parseRecord(chunkReader, this.#validateCrcs))) {
while (true) {
if (chunkRecord = monoParseMessage(chunkReader)) {
yield chunkRecord;
continue;
}
chunkRecord = parseRecord(chunkReader, this.#validateCrcs)
if (!chunkRecord) {
break;
}
switch (chunkRecord.type) {
case "Unknown":
break;
Expand Down
25 changes: 25 additions & 0 deletions typescript/core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,28 @@ function parseDataEnd(reader: Reader, recordLength: number): TypedMcapRecord {
dataSectionCrc,
};
}


export function monoParseMessage(reader: Reader): TypedMcapRecord | undefined | null {
const RECORD_HEADER_SIZE = 1 /*opcode*/ + 8; /*record content length*/
if (reader.bytesRemaining() < RECORD_HEADER_SIZE) {
return undefined;
}
const start = reader.offset;
const opcode = reader.uint8();
const recordLength = reader.uint64();

if (opcode !== Opcode.MESSAGE) {
reader.offset = start; // Rewind to the start of the record
return null;
}

const recordLengthNum = Number(recordLength);

if (reader.bytesRemaining() < recordLengthNum) {
reader.offset = start; // Rewind to the start of the record
return undefined;
}

return parseMessage(reader, recordLengthNum);
}

0 comments on commit 3a86677

Please sign in to comment.