mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
perf(parsers): optimize Safetensors, ElectronFuse, and SourceMap (#13645)
perf(parsers): optimize Safetensors JSON, ElectronFuse scan, and SourceMap hashing
This commit is contained in:
1 parent
49b8470f4e
commit
7a2be3c7d4
3 files changed
+159
-57
No files matched your search
@@ -329,6 +329,27 @@ const scanSlices = async (
|
||||
return { sha256: hash.digest("hex"), slices };
|
||||
};
|
||||
|
||||
const BOUNDARY_BRIDGE = Buffer.allocUnsafe(2 * (SENTINEL.length - 1));
|
||||
|
||||
const recordSentinelHit = (
|
||||
state: {
|
||||
readonly slice: SliceRange;
|
||||
readonly hits: number[];
|
||||
},
|
||||
sentinelOffset: number,
|
||||
): void => {
|
||||
if (state.hits.includes(sentinelOffset)) return;
|
||||
if (state.hits.length >= ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice)
|
||||
throw new ElectronFuseResourceFailure(
|
||||
{
|
||||
name: "maximum_sentinels_per_slice",
|
||||
maximum: ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice,
|
||||
},
|
||||
`Slice ${state.slice.index} contains more than ${ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice} Electron fuse sentinels.`,
|
||||
);
|
||||
state.hits.push(sentinelOffset);
|
||||
};
|
||||
|
||||
const absorbChunk = (
|
||||
states: readonly {
|
||||
readonly slice: SliceRange;
|
||||
@@ -339,38 +360,49 @@ const absorbChunk = (
|
||||
position: number,
|
||||
): void => {
|
||||
const chunkEnd = position + chunk.length;
|
||||
const tail = SENTINEL.length - 1;
|
||||
for (const state of states) {
|
||||
const start = Math.max(position, state.slice.fileOffset);
|
||||
const end = Math.min(chunkEnd, state.slice.fileOffset + state.slice.size);
|
||||
if (start >= end) continue;
|
||||
const piece = chunk.subarray(start - position, end - position);
|
||||
const carryLength = state.carry.length;
|
||||
const window =
|
||||
carryLength === 0 ? piece : Buffer.concat([state.carry, piece]);
|
||||
const windowFileStart = start - carryLength;
|
||||
let from = 0;
|
||||
while (from <= window.length - SENTINEL.length) {
|
||||
const found = window.indexOf(SENTINEL, from);
|
||||
if (found < 0) break;
|
||||
const sentinelOffset = windowFileStart + found;
|
||||
if (!state.hits.includes(sentinelOffset)) {
|
||||
if (state.hits.length >= ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice)
|
||||
throw new ElectronFuseResourceFailure(
|
||||
{
|
||||
name: "maximum_sentinels_per_slice",
|
||||
maximum: ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice,
|
||||
},
|
||||
`Slice ${state.slice.index} contains more than ${ELECTRON_FUSE_LIMITS.maximumSentinelsPerSlice} Electron fuse sentinels.`,
|
||||
);
|
||||
state.hits.push(sentinelOffset);
|
||||
// Scan only the small carry+prefix bridge for boundary-straddling matches,
|
||||
// then scan `piece` directly without copying the full chunk.
|
||||
if (carryLength > 0) {
|
||||
const bridgePieceLength = Math.min(piece.length, tail);
|
||||
const bridgeLength = carryLength + bridgePieceLength;
|
||||
if (bridgeLength >= SENTINEL.length) {
|
||||
state.carry.copy(BOUNDARY_BRIDGE, 0, 0, carryLength);
|
||||
piece.copy(BOUNDARY_BRIDGE, carryLength, 0, bridgePieceLength);
|
||||
const bridge = BOUNDARY_BRIDGE.subarray(0, bridgeLength);
|
||||
const bridgeFileStart = start - carryLength;
|
||||
let from = 0;
|
||||
while (from <= bridgeLength - SENTINEL.length) {
|
||||
const found = bridge.indexOf(SENTINEL, from);
|
||||
if (found < 0) break;
|
||||
recordSentinelHit(state, bridgeFileStart + found);
|
||||
from = found + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let from = 0;
|
||||
while (from <= piece.length - SENTINEL.length) {
|
||||
const found = piece.indexOf(SENTINEL, from);
|
||||
if (found < 0) break;
|
||||
recordSentinelHit(state, start + found);
|
||||
from = found + 1;
|
||||
}
|
||||
const tail = SENTINEL.length - 1;
|
||||
state.carry =
|
||||
window.length <= tail
|
||||
? Buffer.from(window)
|
||||
: Buffer.from(window.subarray(window.length - tail));
|
||||
if (piece.length >= tail) {
|
||||
state.carry = Buffer.from(piece.subarray(piece.length - tail));
|
||||
} else {
|
||||
const keepCarry = Math.min(carryLength, tail - piece.length);
|
||||
const nextCarry = Buffer.allocUnsafe(keepCarry + piece.length);
|
||||
if (keepCarry > 0)
|
||||
state.carry.copy(nextCarry, 0, carryLength - keepCarry, carryLength);
|
||||
piece.copy(nextCarry, keepCarry);
|
||||
state.carry = nextCarry;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -43,6 +43,10 @@ export const traceSourceMap = (
|
||||
const matched = greatestPosition(decodedLeaves, position);
|
||||
let bytes = 0;
|
||||
const matches: WebSourceMapReport["matches"] = [];
|
||||
const contentFactsCache = new Map<
|
||||
number,
|
||||
{ readonly utf8_sha256: string; readonly utf8_bytes: number }
|
||||
>();
|
||||
let sourceBase = 0;
|
||||
for (const { leaf, map, rows } of decodedLeaves) {
|
||||
if (matched !== null) {
|
||||
@@ -61,6 +65,7 @@ export const traceSourceMap = (
|
||||
url,
|
||||
inspected.declarations,
|
||||
sourceBase,
|
||||
contentFactsCache,
|
||||
);
|
||||
bytes += Buffer.byteLength(JSON.stringify(match)) + 1;
|
||||
if (bytes > limits.outputBytes)
|
||||
@@ -198,6 +203,10 @@ const originalMatch = (
|
||||
mapUrl: string,
|
||||
declarations: readonly SourceMapSourceDeclaration[],
|
||||
sourceBase: number,
|
||||
contentFactsCache: Map<
|
||||
number,
|
||||
{ readonly utf8_sha256: string; readonly utf8_bytes: number }
|
||||
>,
|
||||
): WebSourceMapReport["matches"][number] => {
|
||||
if (segment.length === 1) return { state: "generated-only" };
|
||||
const sourceIndex = sourceBase + segment[1];
|
||||
@@ -228,13 +237,24 @@ const originalMatch = (
|
||||
content:
|
||||
declaration.content === null
|
||||
? { state: "unavailable", reason: "sourcesContent-not-retained" }
|
||||
: {
|
||||
state: "embedded",
|
||||
text: declaration.content,
|
||||
utf8_sha256: sha256Hex(declaration.content),
|
||||
utf8_bytes: Buffer.byteLength(declaration.content),
|
||||
representation: "UTF-8 encoding of decoded sourcesContent text",
|
||||
},
|
||||
: (() => {
|
||||
let cached = contentFactsCache.get(sourceIndex);
|
||||
if (cached === undefined) {
|
||||
cached = {
|
||||
utf8_sha256: sha256Hex(declaration.content),
|
||||
utf8_bytes: Buffer.byteLength(declaration.content),
|
||||
};
|
||||
contentFactsCache.set(sourceIndex, cached);
|
||||
}
|
||||
return {
|
||||
state: "embedded" as const,
|
||||
text: declaration.content,
|
||||
utf8_sha256: cached.utf8_sha256,
|
||||
utf8_bytes: cached.utf8_bytes,
|
||||
representation:
|
||||
"UTF-8 encoding of decoded sourcesContent text" as const,
|
||||
};
|
||||
})(),
|
||||
original_position: {
|
||||
...original,
|
||||
offset: offset ?? null,
|
||||
|
||||
@@ -40,25 +40,45 @@ class HeaderJson {
|
||||
readonly budget: ModelReportBudget,
|
||||
) {}
|
||||
#space(): void {
|
||||
while (" \t\r\n".includes(this.text[this.#position] ?? "x"))
|
||||
this.#position++;
|
||||
const text = this.text;
|
||||
const len = text.length;
|
||||
while (this.#position < len) {
|
||||
const code = text.charCodeAt(this.#position);
|
||||
if (code === 0x20 || code === 0x09 || code === 0x0d || code === 0x0a)
|
||||
this.#position++;
|
||||
else break;
|
||||
}
|
||||
}
|
||||
#string(): string {
|
||||
const start = this.#position++;
|
||||
while (this.#position < this.text.length) {
|
||||
const c = this.text[this.#position++];
|
||||
if (c === "\\") this.#position++;
|
||||
else if (c === '"') {
|
||||
const raw = this.text.slice(start, this.#position);
|
||||
const text = this.text;
|
||||
const len = text.length;
|
||||
while (this.#position < len) {
|
||||
const code = text.charCodeAt(this.#position++);
|
||||
if (code === 0x5c) this.#position++;
|
||||
else if (code === 0x22) {
|
||||
const raw = text.slice(start, this.#position);
|
||||
this.budget.charge(6 * raw.length + 2);
|
||||
try {
|
||||
const value: unknown = parseJsonStringLiteral(raw);
|
||||
if (typeof value !== "string")
|
||||
return rejectModel("Invalid JSON string");
|
||||
for (const unit of value) {
|
||||
const code = unit.codePointAt(0) ?? 0;
|
||||
if (code >= 0xd800 && code <= 0xdfff)
|
||||
rejectModel("Safetensors JSON contains an unpaired surrogate");
|
||||
// `text` was decoded via strict UTF-8, so only `\u` escapes can
|
||||
// introduce unpaired UTF-16 surrogates.
|
||||
if (raw.includes("\\u")) {
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
const code = value.charCodeAt(i);
|
||||
if (code >= 0xd800 && code <= 0xdbff) {
|
||||
const next = value.charCodeAt(i + 1);
|
||||
if (next >= 0xdc00 && next <= 0xdfff) i++;
|
||||
else
|
||||
rejectModel(
|
||||
"Safetensors JSON contains an unpaired surrogate",
|
||||
);
|
||||
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
||||
rejectModel("Safetensors JSON contains an unpaired surrogate");
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
} catch (cause) {
|
||||
@@ -124,33 +144,63 @@ class HeaderJson {
|
||||
if (next !== ",") rejectModel("Invalid safetensors JSON container");
|
||||
}
|
||||
}
|
||||
const text = this.text;
|
||||
const len = text.length;
|
||||
const start = this.#position;
|
||||
if (this.text[this.#position] === "-") this.#position++;
|
||||
const integerStart = this.#position;
|
||||
if (this.text[this.#position] === "0") this.#position++;
|
||||
else if (/^[1-9]$/u.test(this.text[this.#position] ?? "")) {
|
||||
let isNonNegativeInt = true;
|
||||
if (this.#position < len && text.charCodeAt(this.#position) === 0x2d) {
|
||||
this.#position++;
|
||||
while (/^[0-9]$/u.test(this.text[this.#position] ?? "")) this.#position++;
|
||||
isNonNegativeInt = false;
|
||||
}
|
||||
const integerStart = this.#position;
|
||||
if (this.#position < len) {
|
||||
const firstDigit = text.charCodeAt(this.#position);
|
||||
if (firstDigit === 0x30) {
|
||||
this.#position++;
|
||||
} else if (firstDigit >= 0x31 && firstDigit <= 0x39) {
|
||||
this.#position++;
|
||||
while (this.#position < len) {
|
||||
const d = text.charCodeAt(this.#position);
|
||||
if (d >= 0x30 && d <= 0x39) this.#position++;
|
||||
else break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.#position === integerStart)
|
||||
return rejectModel("Invalid safetensors JSON value");
|
||||
if (this.text[this.#position] === ".") {
|
||||
if (this.#position < len && text.charCodeAt(this.#position) === 0x2e) {
|
||||
isNonNegativeInt = false;
|
||||
this.#position++;
|
||||
const fractionStart = this.#position;
|
||||
while (/^[0-9]$/u.test(this.text[this.#position] ?? "")) this.#position++;
|
||||
while (this.#position < len) {
|
||||
const d = text.charCodeAt(this.#position);
|
||||
if (d >= 0x30 && d <= 0x39) this.#position++;
|
||||
else break;
|
||||
}
|
||||
if (this.#position === fractionStart)
|
||||
return rejectModel("Invalid safetensors JSON value");
|
||||
}
|
||||
if (/[eE]/u.test(this.text[this.#position] ?? "")) {
|
||||
this.#position++;
|
||||
if (/[+-]/u.test(this.text[this.#position] ?? "")) this.#position++;
|
||||
const exponentStart = this.#position;
|
||||
while (/^[0-9]$/u.test(this.text[this.#position] ?? "")) this.#position++;
|
||||
if (this.#position === exponentStart)
|
||||
return rejectModel("Invalid safetensors JSON value");
|
||||
if (this.#position < len) {
|
||||
const exp = text.charCodeAt(this.#position);
|
||||
if (exp === 0x65 || exp === 0x45) {
|
||||
isNonNegativeInt = false;
|
||||
this.#position++;
|
||||
if (this.#position < len) {
|
||||
const sign = text.charCodeAt(this.#position);
|
||||
if (sign === 0x2b || sign === 0x2d) this.#position++;
|
||||
}
|
||||
const exponentStart = this.#position;
|
||||
while (this.#position < len) {
|
||||
const d = text.charCodeAt(this.#position);
|
||||
if (d >= 0x30 && d <= 0x39) this.#position++;
|
||||
else break;
|
||||
}
|
||||
if (this.#position === exponentStart)
|
||||
return rejectModel("Invalid safetensors JSON value");
|
||||
}
|
||||
}
|
||||
const numeric = this.text.slice(start, this.#position);
|
||||
if (/^(?:0|[1-9][0-9]*)$/u.test(numeric)) {
|
||||
const numeric = text.slice(start, this.#position);
|
||||
if (isNonNegativeInt) {
|
||||
if (numeric.length > 20) return new JsonNumber(numeric);
|
||||
const integer = BigInt(numeric);
|
||||
if (integer <= 0xffffffffffffffffn) return integer;
|
||||
|
||||
Reference in new issue
Block a user