mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 21:53:51 +00:00
fix(javascript): keep array presence snapshots query-owned (#12511)
Reuse the capture-lifetime approach from #7716 for ordinary array-read proof. Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>
This commit is contained in:
1 parent
e46ace9744
commit
83192e9750
4 files changed
+142
-20
No files matched your search
@@ -10,7 +10,8 @@ import {
|
||||
} from "./javascriptSemanticEffectLog.js";
|
||||
import { semanticBindingPrecedesCapture } from "./javascriptSemanticMutationInitializers.js";
|
||||
import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js";
|
||||
import { semanticNativeArrayIterationValueAt } from "./javascriptSemanticReferenceSources.js";
|
||||
import { semanticKnownArrayIteration } from "./javascriptSemanticReferenceSources.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
|
||||
/** Prove a direct data slot in its source at the time of the read. */
|
||||
@@ -44,8 +45,17 @@ export const createSemanticReadPresenceQuery = (
|
||||
const value = unwrapJavaScriptExpression(initializer.node).node;
|
||||
if (source === "array-iteration") {
|
||||
if (!t.isArrayExpression(value)) return undefined;
|
||||
const array = semanticNativeArrayIterationValueAt(capture, state);
|
||||
if (array === undefined || array.unknownItems) return undefined;
|
||||
const array = evaluateSemanticExpression(
|
||||
capture,
|
||||
state,
|
||||
capture,
|
||||
"query",
|
||||
);
|
||||
if (
|
||||
!semanticKnownArrayIteration(array, state, capture) ||
|
||||
array.unknownItems
|
||||
)
|
||||
return undefined;
|
||||
return (path: readonly string[]): boolean => {
|
||||
const [key] = path;
|
||||
if (key === undefined || path.length !== 1) return false;
|
||||
|
||||
@@ -53,23 +53,16 @@ export const semanticKnownArrayIteration = (
|
||||
!ambientPathMayObserveAt(state, read, arrayIteratorNextPath, "contents") &&
|
||||
value.items.every((item) => semanticArrayIndex(item.name) !== null);
|
||||
|
||||
/**
|
||||
* Return the array that admits native iteration at this read. A plain array's
|
||||
* numeric properties are the values consumed by its native iterator.
|
||||
*/
|
||||
export const semanticNativeArrayIterationValueAt = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
) => {
|
||||
const value = evaluateSemanticExpression(node, state, node);
|
||||
return semanticKnownArrayIteration(value, state, node) ? value : undefined;
|
||||
};
|
||||
|
||||
/** Invocation spreads use the same native-iteration proof at their source read. */
|
||||
export const semanticKnownArrayIterationAt = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean => semanticNativeArrayIterationValueAt(node, state) !== undefined;
|
||||
): boolean =>
|
||||
semanticKnownArrayIteration(
|
||||
evaluateSemanticExpression(node, state, node),
|
||||
state,
|
||||
node,
|
||||
);
|
||||
|
||||
/** Numeric destructuring projections do not select slots of custom iterators. */
|
||||
export const semanticReferenceIterationUnknown = (
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { expect, it, vi } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
analyzeJavaScriptSemantics,
|
||||
indexJavaScriptBindingsWithReceiversSteps,
|
||||
} from "./javascriptSemanticAnalysis.js";
|
||||
import { collectJavaScriptSemanticObjects } from "./javascriptSemanticObjects.js";
|
||||
import {
|
||||
completeSemanticSteps,
|
||||
traverseJavaScriptAst,
|
||||
} from "./javascriptSemanticTraversal.js";
|
||||
import { indexJavaScriptBindingsSteps } from "../../../tests/fixtures/javascriptSemanticIndex.js";
|
||||
import { SemanticDependencyProofs } from "./javascriptSemanticDependencies.js";
|
||||
import { JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS } from "./javascriptSemanticPrecision.js";
|
||||
@@ -8,6 +16,7 @@ import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js"
|
||||
import {
|
||||
clearSemanticValueCaches,
|
||||
evaluateSemanticBinding,
|
||||
evaluateSemanticExpression,
|
||||
evaluateSemanticProvenance,
|
||||
} from "./javascriptSemanticValues.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
@@ -495,3 +504,98 @@ it("releases query union ownership when state-owned values are invalidated", ()
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"keeps array read-presence snapshots query-owned (prewarmed: %s)",
|
||||
(prewarmed) => {
|
||||
const reads = 16;
|
||||
const items = Array.from({ length: 128 }, (_, index) => index).join(",");
|
||||
const parsed = parseJavaScriptSource(
|
||||
`const config=[${items}]; let selected;${"[selected]=config;".repeat(reads)} config[0]=999;`,
|
||||
"array-presence.js",
|
||||
);
|
||||
if (parsed === null) throw new Error("Expected parsed source");
|
||||
const index = completeSemanticSteps(
|
||||
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
|
||||
);
|
||||
completeSemanticSteps(index.indexMemberMutationFactsSteps());
|
||||
const state = index.bindings;
|
||||
const binding = [...state.bindingsById.values()].find(
|
||||
({ name }) => name === "config",
|
||||
);
|
||||
if (binding === undefined) throw new Error("Missing source binding");
|
||||
|
||||
// Track aggregate values only in snapshots owned by the analysis state.
|
||||
const stateCaptures = new WeakSet<object>();
|
||||
const stateSnapshots = new WeakSet<object>();
|
||||
let aggregateAccesses = 0;
|
||||
const observeOwner = (owner: object, key: unknown, value: unknown) => {
|
||||
if (key === state && value instanceof WeakMap) stateCaptures.add(value);
|
||||
if (stateCaptures.has(owner) && value instanceof Map)
|
||||
stateSnapshots.add(value);
|
||||
};
|
||||
const observeAggregate = (owner: object, key: unknown, value: unknown) => {
|
||||
if (
|
||||
stateSnapshots.has(owner) &&
|
||||
key === binding.bindingId &&
|
||||
value !== null &&
|
||||
typeof value === "object" &&
|
||||
"status" in value &&
|
||||
value.status === "array"
|
||||
)
|
||||
aggregateAccesses += 1;
|
||||
};
|
||||
const weakGet = WeakMap.prototype.get;
|
||||
const weakSet = WeakMap.prototype.set;
|
||||
const mapGet = Map.prototype.get;
|
||||
const mapSet = Map.prototype.set;
|
||||
try {
|
||||
WeakMap.prototype.get = function (key) {
|
||||
const value = weakGet.call(this, key);
|
||||
observeOwner(this, key, value);
|
||||
return value;
|
||||
};
|
||||
WeakMap.prototype.set = function (key, value) {
|
||||
observeOwner(this, key, value);
|
||||
return weakSet.call(this, key, value);
|
||||
};
|
||||
Map.prototype.get = function (key) {
|
||||
const value = mapGet.call(this, key);
|
||||
observeAggregate(this, key, value);
|
||||
return value;
|
||||
};
|
||||
Map.prototype.set = function (key, value) {
|
||||
observeAggregate(this, key, value);
|
||||
return mapSet.call(this, key, value);
|
||||
};
|
||||
if (prewarmed) {
|
||||
traverseJavaScriptAst(parsed.program, {
|
||||
enter: (node) => {
|
||||
if (
|
||||
node.type === "AssignmentExpression" &&
|
||||
node.left.type === "ArrayPattern"
|
||||
)
|
||||
evaluateSemanticExpression(node.right, state, node.right);
|
||||
},
|
||||
});
|
||||
expect(aggregateAccesses).toBeGreaterThan(0);
|
||||
aggregateAccesses = 0;
|
||||
}
|
||||
const operations = collectJavaScriptSemanticObjects(
|
||||
parsed.program,
|
||||
state,
|
||||
[],
|
||||
).filter(({ kind }) => kind === "destructure");
|
||||
expect(operations).toHaveLength(reads);
|
||||
expect(
|
||||
operations.every(({ sourcePresence }) => sourcePresence === "present"),
|
||||
).toBe(true);
|
||||
expect(aggregateAccesses).toBe(0);
|
||||
} finally {
|
||||
WeakMap.prototype.get = weakGet;
|
||||
WeakMap.prototype.set = weakSet;
|
||||
Map.prototype.get = mapGet;
|
||||
Map.prototype.set = mapSet;
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -95,8 +95,15 @@ type ReadSnapshotValues = WeakMap<
|
||||
Map<string, Map<number, ReadSnapshotValue>>
|
||||
>;
|
||||
|
||||
type CapturedBindingValues = WeakMap<
|
||||
t.Node,
|
||||
Map<string, JavaScriptSemanticValue>
|
||||
>;
|
||||
|
||||
/** Read/provenance memos live for one query; whole values follow state effects. */
|
||||
interface EvaluationMemo {
|
||||
/** Capture snapshots scoped to this evaluation instead of the source state. */
|
||||
readonly capturedBindings?: CapturedBindingValues;
|
||||
snapshotValues?: ReadSnapshotValues;
|
||||
capturedSnapshotValues?: WeakMap<t.Node, ReadSnapshotValues>;
|
||||
readonly provenance: Map<
|
||||
@@ -116,6 +123,7 @@ const dependencyProofsByState = new WeakMap<
|
||||
|
||||
const createEvaluationMemo = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
capturedBindings?: CapturedBindingValues,
|
||||
): EvaluationMemo => {
|
||||
let dependencies = dependencyProofsByState.get(state);
|
||||
if (dependencies === undefined) {
|
||||
@@ -125,6 +133,7 @@ const createEvaluationMemo = (
|
||||
dependencyProofsByState.set(state, dependencies);
|
||||
}
|
||||
return {
|
||||
...(capturedBindings === undefined ? {} : { capturedBindings }),
|
||||
provenance: new Map(),
|
||||
dependencyFrames: [],
|
||||
dependencies: dependencies.forQuery(),
|
||||
@@ -178,7 +187,7 @@ const primitiveExpressionValuesByState = new WeakMap<
|
||||
|
||||
const capturedBindingValuesByState = new WeakMap<
|
||||
JavaScriptSemanticAnalysisState,
|
||||
WeakMap<t.Node, Map<string, JavaScriptSemanticValue>>
|
||||
CapturedBindingValues
|
||||
>();
|
||||
|
||||
/** Discard every state-owned value derived before the effects boundary changed. */
|
||||
@@ -244,6 +253,7 @@ export const evaluateSemanticExpression = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
capturePoint?: t.Node,
|
||||
captureScope: "state" | "query" = "state",
|
||||
): JavaScriptSemanticValue => {
|
||||
const context: EvaluationContext = {
|
||||
state,
|
||||
@@ -251,7 +261,10 @@ export const evaluateSemanticExpression = (
|
||||
expressionDepth: 0,
|
||||
primitiveBindingValues: primitiveBindingValuesFor(state),
|
||||
dependence: { lowest: CALLER_INDEPENDENT },
|
||||
memo: createEvaluationMemo(state),
|
||||
memo: createEvaluationMemo(
|
||||
state,
|
||||
captureScope === "query" ? new WeakMap() : undefined,
|
||||
),
|
||||
};
|
||||
return capturePoint === undefined
|
||||
? evaluateCapturedExpression(node, context)
|
||||
@@ -1458,7 +1471,9 @@ const captureContextAt = (
|
||||
semanticCapturePosition(node, context.state.parentsByNode) === null
|
||||
)
|
||||
return undefined;
|
||||
let captures = capturedBindingValuesByState.get(context.state);
|
||||
let captures =
|
||||
context.memo.capturedBindings ??
|
||||
capturedBindingValuesByState.get(context.state);
|
||||
if (captures === undefined) {
|
||||
captures = new WeakMap();
|
||||
capturedBindingValuesByState.set(context.state, captures);
|
||||
|
||||
Reference in new issue
Block a user