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 authored and GitHub committed 2026-10-09 02:40:21 +08:00
1 parent 1f5be750bc
commit ec6db0f063
11 files changed
+582 -117

No files matched your search

@@ -2,6 +2,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 } from "./javascriptApplicationEvidenceSchemas.js";
import {
JAVASCRIPT_SEMANTIC_RELATION_FAMILIES,
@@ -561,4 +562,28 @@ export const createJavaScriptSemanticGraph = (
return record;
};
const validatedImmutableSemanticGraphs = new WeakSet<object>();
/** Clone and validate synchronously, then return steps to seal the owned graph. */
export const createImmutableJavaScriptSemanticGraphSteps = (
input: unknown,
): Generator<void, JavaScriptSemanticGraph> =>
sealValidatedSemanticGraphSteps(createJavaScriptSemanticGraph(input));
function* sealValidatedSemanticGraphSteps(
graph: JavaScriptSemanticGraph,
): Generator<void, JavaScriptSemanticGraph> {
yield* freezeOwnedJsonSnapshotSteps(graph);
validatedImmutableSemanticGraphs.add(graph);
return graph;
}
/** Recognize the exact completely sealed graph produced by the owned factory. */
export const isValidatedImmutableJavaScriptSemanticGraph = (
value: unknown,
): value is JavaScriptSemanticGraph =>
typeof value === "object" &&
value !== null &&
validatedImmutableSemanticGraphs.has(value);
export type { JavaScriptSemanticGraphNode, JavaScriptSemanticGraphRelation };