perf(javascript): reuse cycle sets during semantic evaluation
Track active bindings with per-evaluation push/pop instead of copying ancestor sets on every alias. Restore membership in finally so cycles and sibling branches keep their existing semantics. Validation: 173 domain tests, compiled CLI/stdio MCP returned-reference acceptance, and check:fast pass. A randomized six-block Flameox experiment retained identical complete semantic output and reduced the bounded 112-alias analyzer median from 36.33 ms to 17.00 ms.
This commit is contained in:
@@ -53,7 +53,7 @@ import {
|
|||||||
|
|
||||||
interface EvaluationContext {
|
interface EvaluationContext {
|
||||||
readonly state: JavaScriptSemanticAnalysisState;
|
readonly state: JavaScriptSemanticAnalysisState;
|
||||||
readonly bindings: ReadonlySet<string>;
|
readonly bindings: Set<string>;
|
||||||
readonly expressionDepth: number;
|
readonly expressionDepth: number;
|
||||||
readonly capturePoint?: t.Node;
|
readonly capturePoint?: t.Node;
|
||||||
readonly primitiveBindingValues: Map<string, JavaScriptSemanticValue>;
|
readonly primitiveBindingValues: Map<string, JavaScriptSemanticValue>;
|
||||||
@@ -196,20 +196,26 @@ const evaluateBinding = (
|
|||||||
const initializer = binding.initializers[0];
|
const initializer = binding.initializers[0];
|
||||||
if (initializer === undefined)
|
if (initializer === undefined)
|
||||||
return { status: "unknown", reason: "Missing binding initializer." };
|
return { status: "unknown", reason: "Missing binding initializer." };
|
||||||
const nested = nestedContext(context, binding.bindingId);
|
context.bindings.add(binding.bindingId);
|
||||||
let value = projectValue(
|
let value: JavaScriptSemanticValue;
|
||||||
evaluateExpression(initializer.node, nested),
|
try {
|
||||||
initializer.projection,
|
const nested = nestedContext(context);
|
||||||
);
|
value = projectValue(
|
||||||
if (!isPrimitive(value)) {
|
evaluateExpression(initializer.node, nested),
|
||||||
const captureContext = primitiveCaptureContext(initializer.node, nested);
|
initializer.projection,
|
||||||
if (captureContext !== undefined) {
|
);
|
||||||
const captured = projectValue(
|
if (!isPrimitive(value)) {
|
||||||
evaluateExpression(initializer.node, captureContext),
|
const captureContext = primitiveCaptureContext(initializer.node, nested);
|
||||||
initializer.projection,
|
if (captureContext !== undefined) {
|
||||||
);
|
const captured = projectValue(
|
||||||
if (isPrimitive(captured)) value = captured;
|
evaluateExpression(initializer.node, captureContext),
|
||||||
|
initializer.projection,
|
||||||
|
);
|
||||||
|
if (isPrimitive(captured)) value = captured;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
context.bindings.delete(binding.bindingId);
|
||||||
}
|
}
|
||||||
let projected = invalidateSemanticMutationPaths(value, binding.mutatedPaths);
|
let projected = invalidateSemanticMutationPaths(value, binding.mutatedPaths);
|
||||||
for (const escape of binding.escapedPaths) {
|
for (const escape of binding.escapedPaths) {
|
||||||
@@ -586,10 +592,16 @@ const provenanceForBinding = (
|
|||||||
"unknown",
|
"unknown",
|
||||||
"Dynamic provenance projection.",
|
"Dynamic provenance projection.",
|
||||||
);
|
);
|
||||||
const resolved = provenanceForExpression(
|
context.bindings.add(binding.bindingId);
|
||||||
initializer.node,
|
let resolved: JavaScriptBindingProvenance;
|
||||||
nestedContext(context, binding.bindingId),
|
try {
|
||||||
);
|
resolved = provenanceForExpression(
|
||||||
|
initializer.node,
|
||||||
|
nestedContext(context),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
context.bindings.delete(binding.bindingId);
|
||||||
|
}
|
||||||
if (resolved.status !== "module" || initializer.projection.length === 0)
|
if (resolved.status !== "module" || initializer.projection.length === 0)
|
||||||
return resolved;
|
return resolved;
|
||||||
return semanticOriginsProvenance(
|
return semanticOriginsProvenance(
|
||||||
@@ -718,20 +730,14 @@ const mergeValues = (
|
|||||||
: { status: "ambiguous", reason: "Branches have incompatible values." };
|
: { status: "ambiguous", reason: "Branches have incompatible values." };
|
||||||
};
|
};
|
||||||
|
|
||||||
const nestedContext = (
|
const nestedContext = (context: EvaluationContext): EvaluationContext => ({
|
||||||
context: EvaluationContext,
|
|
||||||
bindingId?: string,
|
|
||||||
): EvaluationContext => ({
|
|
||||||
state: context.state,
|
state: context.state,
|
||||||
primitiveBindingValues: context.primitiveBindingValues,
|
primitiveBindingValues: context.primitiveBindingValues,
|
||||||
...(context.capturePoint === undefined
|
...(context.capturePoint === undefined
|
||||||
? {}
|
? {}
|
||||||
: { capturePoint: context.capturePoint }),
|
: { capturePoint: context.capturePoint }),
|
||||||
expressionDepth: context.expressionDepth + 1,
|
expressionDepth: context.expressionDepth + 1,
|
||||||
bindings:
|
bindings: context.bindings,
|
||||||
bindingId === undefined
|
|
||||||
? context.bindings
|
|
||||||
: new Set([...context.bindings, bindingId]),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const isPrimitive = (value: JavaScriptSemanticValue): boolean =>
|
const isPrimitive = (value: JavaScriptSemanticValue): boolean =>
|
||||||
|
|||||||
Reference in New Issue
Block a user