perf(javascript): reuse validated immutable graphs (#1112)

* perf(javascript): reuse validated immutable graphs

* fix(ci): follow the moved process CLI boundary test
This commit is contained in:
Ysopwnable
2026-10-09 02:40:21 +08:00
committed by GitHub
parent 1f5be750bc
commit ec6db0f063
11 changed files with 582 additions and 117 deletions
@@ -1,6 +1,7 @@
import { z } from "zod";
import { canonicalDigest, canonicalJson } from "../comparisonSemantics.js";
import { compareCodePoints, uniqueSorted } from "../canonicalOrdering.js";
import { freezeOwnedJsonSnapshotSteps } from "../immutableJson.js";
import type {
ApplicationGraphEvidence,
@@ -450,6 +451,30 @@ export const createJavaScriptApplicationGraph = (
});
};
const validatedImmutableApplicationGraphs = new WeakSet<object>();
/** Clone and validate synchronously, then return steps to seal the owned graph. */
export const createImmutableJavaScriptApplicationGraphSteps = (
input: unknown,
): Generator<void, JavaScriptApplicationGraph> =>
sealValidatedApplicationGraphSteps(createJavaScriptApplicationGraph(input));
function* sealValidatedApplicationGraphSteps(
graph: JavaScriptApplicationGraph,
): Generator<void, JavaScriptApplicationGraph> {
yield* freezeOwnedJsonSnapshotSteps(graph);
validatedImmutableApplicationGraphs.add(graph);
return graph;
}
/** Recognize the exact completely sealed graph produced by the owned factory. */
export const isValidatedImmutableJavaScriptApplicationGraph = (
value: unknown,
): value is JavaScriptApplicationGraph =>
typeof value === "object" &&
value !== null &&
validatedImmutableApplicationGraphs.has(value);
/** Parse a stored graph and reject stale IDs. */
export const parseJavaScriptApplicationGraph = (
input: unknown,