perf(js/browser/uefi): index export shape coverage and AST writes, eliminate CdpRuntimeRequests re-parse and UefiLzmaWorker JSON round-trip (#13535)

* perf(js/browser/uefi): index export shape coverage and AST writes, eliminate CdpRuntimeRequests re-parse and UefiLzmaWorker JSON round-trip (#13530)

* fix(browser): detach published runtime initiator facts
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 13:26:41 -05:00
1 parent 5b63cf16ee
commit 593020442d
5 files changed
+70 -33

No files matched your search

@@ -17,7 +17,7 @@ const sources = {
};
const event = (
extraRequest: Record<string, string>,
extraInitiator: Record<string, string> = {},
extraInitiator: Record<string, unknown> = {},
) => ({
method: "Network.requestWillBeSent",
params: {
@@ -47,6 +47,26 @@ it("budgets retained request facts independently of discarded POST bodies", () =
expect(JSON.stringify(retained)).not.toContain("postData");
});
it("keeps retained initiator facts independent of a returned result", () => {
const requests = new CdpRuntimeRequests(sources);
requests.ingest(
receiveCdpEvent(event({}, { extension: { label: "producer" } })),
);
const initiator = requests.result()[0]?.reported_initiator;
const extension = initiator?.extension;
if (
typeof extension !== "object" ||
extension === null ||
Array.isArray(extension)
)
throw new Error("Missing producer extension");
extension.label = "changed by caller";
expect(requests.result()[0]?.reported_initiator).toEqual({
type: "parser",
extension: { label: "producer" },
});
});
it("preserves a typed resource refusal for oversized retained initiator facts", () => {
const requests = new CdpRuntimeRequests(sources);
requests.ingest(
+1 -1
View File
@@ -126,7 +126,7 @@ export class CdpRuntimeRequests {
const result = this.items.map((request) => ({
...request,
reported_initiator: sanitizeRuntimeInitiator(
jsonObjectSchema.parse(request.reported_initiator),
structuredClone(request.reported_initiator),
this.sources,
),
callsites: request.callsites.map((site) =>
@@ -160,7 +160,7 @@ const inventoryFor = (
source_range: shape.source_range,
properties: uniqueSorted([
...shape.fields
.filter((field) => fieldPresence(shape, field, field.path) === "present")
.filter((field) => field.presence === "present")
.map(({ path }) => path),
...shape.property_coverage.map(({ path }) => path),
]).filter((path) => path !== ""),
@@ -177,10 +177,7 @@ export const hasPartialJavaScriptExportPropertyCoverage = (
shapes.some(
(shape) =>
shape.property_coverage.some(({ status }) => status === "partial") ||
shape.fields.some(
(field) =>
fieldPresence(shape, field, field.path) === "unknown-coverage",
),
shape.fields.some((field) => field.presence === "unknown-coverage"),
);
interface DiscriminantOccurrence {
@@ -194,7 +191,7 @@ const discriminantOccurrences = (
const output = new Map<string, DiscriminantOccurrence>();
shapes.forEach((shape, index) => {
for (const field of shape.fields) {
if (fieldPresence(shape, field, field.path) !== "present") continue;
if (field.presence !== "present") continue;
if (field.state !== "literal") continue;
const discriminant = { path: field.path, value: field.value };
const key = canonicalJson(
@@ -305,19 +302,39 @@ const diffPair = (
discriminant: Discriminant,
evidenceLinks: [string, string],
): JavaScriptExportShapeComparisonChange[] => {
const leftFields = new Map(left.fields.map((field) => [field.path, field]));
const rightFields = new Map(right.fields.map((field) => [field.path, field]));
const leftFields = new Map<string, Field>();
for (const field of left.fields)
if (!leftFields.has(field.path)) leftFields.set(field.path, field);
const rightFields = new Map<string, Field>();
for (const field of right.fields)
if (!rightFields.has(field.path)) rightFields.set(field.path, field);
const leftCoverage = new Map<string, Shape["property_coverage"][number]>();
for (const entry of left.property_coverage)
if (!leftCoverage.has(entry.path)) leftCoverage.set(entry.path, entry);
const rightCoverage = new Map<string, Shape["property_coverage"][number]>();
for (const entry of right.property_coverage)
if (!rightCoverage.has(entry.path)) rightCoverage.set(entry.path, entry);
const paths = uniqueSorted([
...leftFields.keys(),
...rightFields.keys(),
...left.property_coverage.map(({ path }) => path),
...right.property_coverage.map(({ path }) => path),
...leftCoverage.keys(),
...rightCoverage.keys(),
]);
return paths.flatMap((path) => {
const leftField = fieldAtPath(left, leftFields, path);
const rightField = fieldAtPath(right, rightFields, path);
const leftPresence = fieldPresence(left, leftField, path);
const rightPresence = fieldPresence(right, rightField, path);
const leftField = fieldAtPath(leftFields, leftCoverage, path);
const rightField = fieldAtPath(rightFields, rightCoverage, path);
const leftPresence = fieldPresence(
leftFields,
leftCoverage,
leftField,
path,
);
const rightPresence = fieldPresence(
rightFields,
rightCoverage,
rightField,
path,
);
const status = fieldChangeStatus({
leftPresence,
rightPresence,
@@ -351,14 +368,13 @@ const diffPair = (
};
const fieldAtPath = (
shape: Shape,
fields: ReadonlyMap<string, Field>,
coverageByPath: ReadonlyMap<string, Shape["property_coverage"][number]>,
path: string,
): Field | undefined => {
const field = fields.get(path);
if (field !== undefined) return field;
if (!shape.property_coverage.some((coverage) => coverage.path === path))
return undefined;
if (!coverageByPath.has(path)) return undefined;
return {
path,
presence: "present",
@@ -405,7 +421,8 @@ const fieldChangeStatus = ({
};
const fieldPresence = (
shape: Shape,
fields: ReadonlyMap<string, Field>,
coverageByPath: ReadonlyMap<string, Shape["property_coverage"][number]>,
field: Field | undefined,
path: string,
): PropertyPresence => {
@@ -415,11 +432,9 @@ const fieldPresence = (
parent !== null;
parent = parentPointer(parent)
) {
const coverage = shape.property_coverage.find(
(entry) => entry.path === parent,
);
const coverage = coverageByPath.get(parent);
if (coverage?.status === "complete") return "absent";
const ancestor = shape.fields.find((entry) => entry.path === parent);
const ancestor = fields.get(parent);
if (ancestor !== undefined)
return ancestor.presence === "absent" ? "absent" : "unknown-coverage";
if (coverage !== undefined) return "unknown-coverage";
@@ -2146,6 +2146,7 @@ export const semanticBindingHasPriorValueWrite = (
): boolean => {
if (binding.definitions.some(({ kind }) => kind === "function")) return true;
const writes = state.bindingWrites.get(binding.bindingId) ?? [];
const writeSet = new Set(writes);
const read = semanticBindingAtRead(binding, site, state);
const retained =
read.status === "ordered"
@@ -2161,7 +2162,7 @@ export const semanticBindingHasPriorValueWrite = (
...binding.referenceInitializers,
]) {
let owner: t.Node | undefined = source.node;
while (owner !== undefined && !writes.includes(owner))
while (owner !== undefined && !writeSet.has(owner))
owner = state.parentsByNode.get(owner);
if (owner !== undefined) {
representedWrites.add(owner);
+9 -8
View File
@@ -201,17 +201,19 @@ const decodeStreamPayload = (
}
};
const serializeReply = (reply: UefiLzmaReply): string => {
const serializeReply = (
reply: UefiLzmaReply,
): { readonly text: string; readonly ok: boolean } => {
const encoded = JSON.stringify(reply);
if (Buffer.byteLength(encoded) <= UEFI_FIRMWARE_LIMITS.diagnosticBytes) {
return encoded;
return { text: encoded, ok: reply.ok };
}
const bounded: UefiLzmaReply = {
ok: false,
reason: reply.ok ? "decoder" : reply.reason,
message: "LZMA reply exceeds the diagnostic budget.",
};
return JSON.stringify(bounded);
return { text: JSON.stringify(bounded), ok: false };
};
class FramedByteReader {
@@ -313,11 +315,10 @@ const runStdioWorker = async (): Promise<void> => {
request.format,
request.max_output_bytes,
);
const replyText = serializeReply(reply);
const parsedReply = JSON.parse(replyText) as UefiLzmaReply;
const serialized = serializeReply(reply);
const body =
parsedReply.ok && decoded !== undefined ? decoded : Buffer.alloc(0);
await writeFramed(process.stdout, encodeLzmaFrame(replyText, body));
serialized.ok && decoded !== undefined ? decoded : Buffer.alloc(0);
await writeFramed(process.stdout, encodeLzmaFrame(serialized.text, body));
}
};
@@ -355,7 +356,7 @@ const runSingleFileWorker = async (requestPath: string): Promise<void> => {
message: errorMessage(cause),
};
}
await writeFile(request.reply_path, serializeReply(reply), {
await writeFile(request.reply_path, serializeReply(reply).text, {
flag: "wx",
mode: 0o600,
});