mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 06:55:55 +00:00
perf(parsers): eliminate Flutter snapshot re-read/concat, PTX per-char regexes, HEX reduce loops, MFT magic copies, and per-entry TextDecoder allocs (#13615)
* perf(parsers): eliminate Flutter snapshot re-read/concat, PTX per-char regexes, HEX reduce loops, MFT magic copies, and per-entry TextDecoder allocs * test(flutter): follow the snapshot copy and hash path * fix(flutter): preserve owned snapshot stat and digest boundary in ownSnapshot * test(flutter): restore owned snapshot stat and digest coverage
This commit is contained in:
1 parent
5391e40dde
commit
a9ebb65d88
9 files changed
+127
-64
No files matched your search
@@ -11,7 +11,7 @@ import {
|
||||
InterfaceBuilderDecodeBudget,
|
||||
interfaceBuilderDecodeBudgetExceeded,
|
||||
} from "./InterfaceBuilderDecodeBudget.js";
|
||||
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
|
||||
import { decodeStrictUtf8 } from "../../domain/strictUtf8.js";
|
||||
import { isRecord } from "../../kernel/isRecord.js";
|
||||
|
||||
const DEFAULT_DECODE_BUDGET_BYTES = 128 * 1024 * 1024;
|
||||
@@ -566,7 +566,7 @@ const decodeUtf8 = (
|
||||
): string => {
|
||||
try {
|
||||
// These are length-prefixed names, not standalone text documents.
|
||||
return strictUtf8Decoder("preserve").decode(bytes.subarray(start, end));
|
||||
return decodeStrictUtf8(bytes.subarray(start, end), "preserve");
|
||||
} catch (cause: unknown) {
|
||||
throw malformedInput(
|
||||
"nib-archive",
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
artifactReaderLimitFailure,
|
||||
abortIfNeeded,
|
||||
} from "../../resources/index.js";
|
||||
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
|
||||
import { decodeStrictUtf8 } from "../../domain/strictUtf8.js";
|
||||
|
||||
/** SHA-256 marker embedded by Microsoft's apphost; the preceding UInt64 is the header offset. */
|
||||
export const DOTNET_BUNDLE_SIGNATURE = Buffer.from(
|
||||
@@ -326,7 +326,7 @@ class BundleCursor {
|
||||
const at = await this.#advance(length);
|
||||
const bytes = this.#cache.range(at, length, "manifest string");
|
||||
try {
|
||||
return strictUtf8Decoder("preserve").decode(bytes);
|
||||
return decodeStrictUtf8(bytes, "preserve");
|
||||
} catch (cause) {
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
pyInstallerLayoutFailure,
|
||||
type PyInstallerFailure,
|
||||
} from "./PyInstallerFailure.js";
|
||||
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
|
||||
import { decodeStrictUtf8 } from "../../domain/strictUtf8.js";
|
||||
|
||||
/** Magic at the start of a CArchive cookie. */
|
||||
const PYINSTALLER_COOKIE_MAGIC = Buffer.from("MEI\x0c\x0b\x0a\x0b\x0e");
|
||||
@@ -305,7 +305,7 @@ const libraryName = (
|
||||
);
|
||||
if (text.length === 0) return ok(null);
|
||||
try {
|
||||
return ok(strictUtf8Decoder("preserve").decode(text));
|
||||
return ok(decodeStrictUtf8(text, "preserve"));
|
||||
} catch {
|
||||
return err(
|
||||
pyInstallerFailure(
|
||||
@@ -325,7 +325,7 @@ const tocName = (bytes: Buffer): Result<string, PyInstallerFailure> => {
|
||||
if (end === 0)
|
||||
return err(pyInstallerFailure("format", "CArchive entry name is empty"));
|
||||
try {
|
||||
return ok(strictUtf8Decoder("preserve").decode(bytes.subarray(0, end)));
|
||||
return ok(decodeStrictUtf8(bytes.subarray(0, end), "preserve"));
|
||||
} catch {
|
||||
return err(
|
||||
pyInstallerFailure("format", "CArchive entry name is not valid UTF-8"),
|
||||
|
||||
@@ -47,7 +47,7 @@ import {
|
||||
type FatSliceDeclaration,
|
||||
type FatSliceRangeIssue,
|
||||
} from "./machoContainer.js";
|
||||
import { strictUtf8Decoder } from "../strictUtf8.js";
|
||||
import { decodeStrictUtf8 } from "../strictUtf8.js";
|
||||
|
||||
/** Read `length` bytes at `offset`; shorter results mean end of file. */
|
||||
export type ReadAt = (offset: number, length: number) => Promise<Uint8Array>;
|
||||
@@ -1042,7 +1042,7 @@ const payloadString = (
|
||||
chargeLazyString(work, end - offset + 1);
|
||||
let text: string;
|
||||
try {
|
||||
text = strictUtf8Decoder("preserve").decode(payload.subarray(offset, end));
|
||||
text = decodeStrictUtf8(payload.subarray(offset, end), "preserve");
|
||||
} catch {
|
||||
throw machoIssue(
|
||||
"malformed",
|
||||
@@ -1114,7 +1114,7 @@ const throwCommandStringIssue = (
|
||||
const decodeCommandString = (bytes: Uint8Array, index: number): string => {
|
||||
try {
|
||||
// lc_str contains name/path data, so a leading U+FEFF is part of its identity.
|
||||
return strictUtf8Decoder("preserve").decode(bytes);
|
||||
return decodeStrictUtf8(bytes, "preserve");
|
||||
} catch {
|
||||
throw machoIssue(
|
||||
"malformed",
|
||||
|
||||
@@ -358,7 +358,8 @@ const asSafeInt = (value: bigint | string | number): number | null => {
|
||||
return Number(numeric);
|
||||
};
|
||||
|
||||
const hex = (bytes: Uint8Array): string => Buffer.from(bytes).toString("hex");
|
||||
const hex = (bytes: Uint8Array): string =>
|
||||
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("hex");
|
||||
|
||||
const protobufFailure = (cause: unknown): AndroidOtaPayloadParseError =>
|
||||
malformed(
|
||||
|
||||
@@ -62,11 +62,24 @@ const HEX_VALUES = ((): Int8Array => {
|
||||
return table;
|
||||
})();
|
||||
|
||||
const INTEL_HEX_TYPE_NAMES: readonly string[] = [
|
||||
"00",
|
||||
"01",
|
||||
"02",
|
||||
"03",
|
||||
"04",
|
||||
"05",
|
||||
];
|
||||
|
||||
export const hex = (value: number, digits: number): string =>
|
||||
value.toString(16).padStart(digits, "0");
|
||||
|
||||
export const readBigEndian = (bytes: Uint8Array): number =>
|
||||
bytes.reduce((value, byte) => value * 256 + byte, 0);
|
||||
export const readBigEndian = (bytes: Uint8Array): number => {
|
||||
let value = 0;
|
||||
for (let index = 0; index < bytes.length; index += 1)
|
||||
value = value * 256 + bytes[index]!;
|
||||
return value;
|
||||
};
|
||||
|
||||
export const maxLineChars = (encoding: HexRecordEncoding): number =>
|
||||
encoding === "intel-hex" ? INTEL_HEX_MAX_LINE_CHARS : SREC_MAX_LINE_CHARS;
|
||||
@@ -100,8 +113,11 @@ const decodeHexDigits = (
|
||||
return ok(bytes);
|
||||
};
|
||||
|
||||
const byteSum = (bytes: Uint8Array): number =>
|
||||
bytes.reduce((sum, byte) => sum + byte, 0);
|
||||
const byteSum = (bytes: Uint8Array, end = bytes.length): number => {
|
||||
let sum = 0;
|
||||
for (let index = 0; index < end; index += 1) sum += bytes[index]!;
|
||||
return sum;
|
||||
};
|
||||
|
||||
const startCharacterFailure = (line: Uint8Array, expected: string): string =>
|
||||
`invalid character 0x${hex(line[0] ?? 0, 2)} at column 1; a record starts with '${expected}'`;
|
||||
@@ -117,13 +133,13 @@ const parseIntelRecord = (line: Uint8Array): Result<HexRecord, string> => {
|
||||
);
|
||||
const count = bytes[0] ?? 0;
|
||||
const typeCode = bytes[3] ?? 0;
|
||||
const type = hex(typeCode, 2);
|
||||
const type = INTEL_HEX_TYPE_NAMES[typeCode] ?? hex(typeCode, 2);
|
||||
if (bytes.length !== count + 5)
|
||||
return err(
|
||||
`record type ${type} byte count 0x${hex(count, 2)} declares ${String(count)} data bytes, but the line holds ${String(bytes.length - 5)}`,
|
||||
);
|
||||
const declared = bytes[bytes.length - 1] ?? 0;
|
||||
const computed = -byteSum(bytes.subarray(0, -1)) & 0xff;
|
||||
const computed = -byteSum(bytes, bytes.length - 1) & 0xff;
|
||||
if (declared !== computed)
|
||||
return err(
|
||||
`record type ${type} checksum 0x${hex(declared, 2)} does not match computed 0x${hex(computed, 2)}`,
|
||||
|
||||
@@ -17,15 +17,16 @@ import type {
|
||||
} from "../firmware/regionDecoder.js";
|
||||
|
||||
/** An NTFS FILE record begins with this signature. */
|
||||
const MFT_FILE_RECORD_MAGIC = "FILE";
|
||||
const MFT_FILE_RECORD_MAGIC_BYTES = 4;
|
||||
/** The shortest header worth reading: a FILE record header ends at `nextAttributeId`. */
|
||||
const NTFS_MFT_MIN_RECORD_BYTES = 42;
|
||||
|
||||
const hasMftFileRecordSignature = (prefix: Uint8Array): boolean =>
|
||||
prefix.length >= MFT_FILE_RECORD_MAGIC_BYTES &&
|
||||
text(prefix.subarray(0, MFT_FILE_RECORD_MAGIC_BYTES)) ===
|
||||
MFT_FILE_RECORD_MAGIC;
|
||||
prefix[0] === 0x46 &&
|
||||
prefix[1] === 0x49 &&
|
||||
prefix[2] === 0x4c &&
|
||||
prefix[3] === 0x45;
|
||||
|
||||
/** A record's declared size is a power of two from 512 upward; 1024 is NTFS's default. */
|
||||
const MFT_DEFAULT_RECORD_BYTES = 1024;
|
||||
@@ -171,9 +172,6 @@ export interface MftMetadata {
|
||||
readonly attribute_incomplete_record_count: number;
|
||||
}
|
||||
|
||||
const text = (ascii: Uint8Array): string =>
|
||||
Buffer.from(ascii).toString("latin1");
|
||||
|
||||
const failure = (
|
||||
message: string,
|
||||
boundary?: RegionDecodeFailure["boundary"],
|
||||
|
||||
@@ -1394,7 +1394,6 @@ const projectStringPool = async (
|
||||
const tokenBudget =
|
||||
Math.min(poolPolicy.token_limit, section?.length ?? 0) * COLLECTION_SLACK;
|
||||
if (section !== null) {
|
||||
let run = "";
|
||||
const consider = (text: string): void => {
|
||||
if (text.length < 5) return;
|
||||
runCount += 1;
|
||||
@@ -1420,29 +1419,43 @@ const projectStringPool = async (
|
||||
}
|
||||
};
|
||||
let overflowed = false;
|
||||
let runStart = -1;
|
||||
let runLength = 0;
|
||||
const flushRun = (): void => {
|
||||
if (runStart >= 0) {
|
||||
if (!overflowed && runLength >= 5)
|
||||
consider(section.toString("latin1", runStart, runStart + runLength));
|
||||
runStart = -1;
|
||||
runLength = 0;
|
||||
}
|
||||
overflowed = false;
|
||||
};
|
||||
for (let start = 0; start < section.length; start += 64 * 1024) {
|
||||
await yieldToEventLoop();
|
||||
assertArtifactReadActive(signal);
|
||||
for (const byte of section.subarray(start, start + 64 * 1024)) {
|
||||
const end = Math.min(section.length, start + 64 * 1024);
|
||||
for (let index = start; index < end; index += 1) {
|
||||
const byte = section[index]!;
|
||||
if (byte >= 0x20 && byte < 0x7f) {
|
||||
// A single unbounded run (an adversarial NUL-free section) never
|
||||
// materializes: growth stops at the cap and the entire run is
|
||||
// discarded at its delimiter, since a truncated prefix would be
|
||||
// fabricated evidence rather than an observation.
|
||||
if (run.length < poolPolicy.run_bytes)
|
||||
run += String.fromCharCode(byte);
|
||||
else {
|
||||
if (runStart < 0) {
|
||||
runStart = index;
|
||||
runLength = 1;
|
||||
} else if (runLength < poolPolicy.run_bytes) {
|
||||
runLength += 1;
|
||||
} else {
|
||||
overflowed = true;
|
||||
limitsReached.add("run_bytes");
|
||||
}
|
||||
} else {
|
||||
if (!overflowed) consider(run);
|
||||
overflowed = false;
|
||||
run = "";
|
||||
flushRun();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!overflowed) consider(run);
|
||||
flushRun();
|
||||
}
|
||||
assertArtifactReadActive(signal);
|
||||
return {
|
||||
|
||||
+67
-32
@@ -9,64 +9,99 @@ interface Token {
|
||||
readonly start: number;
|
||||
readonly end: number;
|
||||
}
|
||||
const isPtxWordChar = (code: number): boolean =>
|
||||
(code >= 0x30 && code <= 0x39) ||
|
||||
(code >= 0x41 && code <= 0x5a) ||
|
||||
(code >= 0x61 && code <= 0x7a) ||
|
||||
code === 0x5f ||
|
||||
code === 0x2e ||
|
||||
code === 0x24 ||
|
||||
code === 0x25 ||
|
||||
code === 0x40;
|
||||
|
||||
const isWhitespaceAt = (text: string, at: number, code: number): boolean =>
|
||||
code < 0x80
|
||||
? code === 0x20 || (code >= 0x09 && code <= 0x0d)
|
||||
: /\s/u.test(text[at]!);
|
||||
|
||||
/** Byte coordinates are accumulated once, including non-ASCII comments and quoted strings. */
|
||||
function* tokens(text: string): Generator<Token> {
|
||||
const len = text.length;
|
||||
let at = 0,
|
||||
byte = 0;
|
||||
const advance = (): void => {
|
||||
const first = text.charCodeAt(at);
|
||||
if (first < 0x80) {
|
||||
at += 1;
|
||||
byte += 1;
|
||||
return;
|
||||
}
|
||||
const code = text.codePointAt(at)!;
|
||||
at += code > 0xffff ? 2 : 1;
|
||||
byte += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
||||
byte += code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
||||
};
|
||||
while (at < text.length) {
|
||||
if (/\s/u.test(text[at]!)) {
|
||||
while (at < len) {
|
||||
const code = text.charCodeAt(at);
|
||||
if (isWhitespaceAt(text, at, code)) {
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (text.startsWith("//", at)) {
|
||||
while (at < text.length && text[at] !== "\n") advance();
|
||||
continue;
|
||||
}
|
||||
if (text.startsWith("/*", at)) {
|
||||
advance();
|
||||
advance();
|
||||
while (at < text.length && !text.startsWith("*/", at)) advance();
|
||||
if (at === text.length) rejectGpu("Unterminated PTX comment");
|
||||
advance();
|
||||
advance();
|
||||
continue;
|
||||
if (code === 0x2f && at + 1 < len) {
|
||||
const next = text.charCodeAt(at + 1);
|
||||
if (next === 0x2f) {
|
||||
while (at < len && text.charCodeAt(at) !== 0x0a) advance();
|
||||
continue;
|
||||
}
|
||||
if (next === 0x2a) {
|
||||
at += 2;
|
||||
byte += 2;
|
||||
while (
|
||||
at < len &&
|
||||
!(text.charCodeAt(at) === 0x2a && text.charCodeAt(at + 1) === 0x2f)
|
||||
)
|
||||
advance();
|
||||
if (at === len) rejectGpu("Unterminated PTX comment");
|
||||
at += 2;
|
||||
byte += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const start = byte,
|
||||
position = at;
|
||||
if (text[at] === '"') {
|
||||
advance();
|
||||
if (code === 0x22) {
|
||||
at += 1;
|
||||
byte += 1;
|
||||
let closed = false;
|
||||
while (at < text.length) {
|
||||
if (text[at] === '"') {
|
||||
advance();
|
||||
while (at < len) {
|
||||
const ch = text.charCodeAt(at);
|
||||
if (ch === 0x22) {
|
||||
at += 1;
|
||||
byte += 1;
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
if (text[at] === "\\") {
|
||||
advance();
|
||||
if (at < text.length) advance();
|
||||
if (ch === 0x5c) {
|
||||
at += 1;
|
||||
byte += 1;
|
||||
if (at < len) advance();
|
||||
} else advance();
|
||||
}
|
||||
if (!closed) rejectGpu("Unterminated PTX string");
|
||||
yield { value: '""', start, end: byte };
|
||||
continue;
|
||||
}
|
||||
if (/[.\w$%@]/u.test(text[at]!)) {
|
||||
if (isPtxWordChar(code)) {
|
||||
// Dot-prefixed keywords remain separate when their whitespace is omitted.
|
||||
// Internal dots in names, instruction mnemonics and version literals stay intact.
|
||||
const keyword = text[at] === ".";
|
||||
advance();
|
||||
while (
|
||||
at < text.length &&
|
||||
/[.\w$%@]/u.test(text[at]!) &&
|
||||
(!keyword || text[at] !== ".")
|
||||
)
|
||||
advance();
|
||||
const keyword = code === 0x2e;
|
||||
at += 1;
|
||||
byte += 1;
|
||||
while (at < len) {
|
||||
const ch = text.charCodeAt(at);
|
||||
if (!isPtxWordChar(ch) || (keyword && ch === 0x2e)) break;
|
||||
at += 1;
|
||||
byte += 1;
|
||||
}
|
||||
} else advance();
|
||||
if (at - position > 65536)
|
||||
throw new BinaryResourceFailure(
|
||||
|
||||
Reference in new issue
Block a user