mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* perf(javascript): index member-effect paths for projected reads Each projected read scanned every member-write record of its binding and tested the record path against the selection. On the #7774 reproducer only 3.7% of scanned records pass that test. Index each binding's append-only records by path in a lazily built trie that extends itself with newly appended records, and visit only records whose exact keys a selected choice names or whose keys are wildcards. Candidates come back in record order and still pass the exact predicate, now owned next to the index as semanticEffectPathMatches. The semantic IR and the complete analyze-javascript-application Evidence are byte-identical. On the full reproducer each worker phase drops from about 80 s to 29 s, and to 8 s together with #8103. Refs #7774 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(javascript): stop escape checks at the first indexed match The escape check only asks whether any matching escape is unsuperseded, so it does not need ordered candidates. Share one trie traversal between the ordered write candidates and an early-exit visitor for this check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
3.5 KiB
TypeScript
115 lines
3.5 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import type { SemanticAliasSelection } from "./javascriptSemanticAliasSelections.js";
|
|
import {
|
|
semanticEffectPathCandidates,
|
|
semanticEffectPathMatches,
|
|
semanticSomeEffectPathCandidate,
|
|
} from "./javascriptSemanticEffectPathIndex.js";
|
|
import type { JavaScriptSemanticPropertyPath } from "./javascriptSemanticPropertyPaths.js";
|
|
import type { JavaScriptSemanticMemberEffect } from "./javascriptSemanticState.js";
|
|
|
|
const effectKeys: JavaScriptSemanticPropertyPath = [
|
|
"a",
|
|
"b",
|
|
"c",
|
|
0,
|
|
"0",
|
|
1,
|
|
null,
|
|
{ excludedKeys: ["a"] },
|
|
{ excludedKeys: [], startIndex: 1 },
|
|
];
|
|
const selections: readonly SemanticAliasSelection[] = [
|
|
"a",
|
|
"b",
|
|
"d",
|
|
0,
|
|
"1",
|
|
null,
|
|
{ excludedKeys: ["b"] },
|
|
{ alternatives: ["a", "c"] },
|
|
{ alternatives: [0, "0", "b"] },
|
|
{ alternatives: ["a", null] },
|
|
];
|
|
|
|
it("selects exactly the linearly matching effects, in record order", () => {
|
|
let seed = 7774;
|
|
const next = (bound: number): number => {
|
|
seed = (seed * 1_103_515_245 + 12_345) % 2_147_483_648;
|
|
return Math.floor((seed / 2_147_483_648) * bound);
|
|
};
|
|
const effects: JavaScriptSemanticMemberEffect[] = [];
|
|
let compared = 0;
|
|
for (let round = 0; round < 400; round++) {
|
|
// Records are append-only; the index must extend across queries.
|
|
for (let added = next(4); added > 0; added--)
|
|
effects.push({
|
|
path: Array.from({ length: next(5) }, () => effectKeys[next(9)]!),
|
|
node: undefined,
|
|
});
|
|
const path = Array.from({ length: next(5) }, () => selections[next(10)]!);
|
|
const expected = effects.filter((effect) =>
|
|
semanticEffectPathMatches(effect.path, path),
|
|
);
|
|
const candidates = semanticEffectPathCandidates(effects, path);
|
|
expect(
|
|
candidates.filter((effect) =>
|
|
semanticEffectPathMatches(effect.path, path),
|
|
),
|
|
).toEqual(expected);
|
|
expect(
|
|
candidates.every(
|
|
(effect, index) =>
|
|
index === 0 ||
|
|
effects.indexOf(candidates[index - 1]!) < effects.indexOf(effect),
|
|
),
|
|
).toBe(true);
|
|
const evenDepth = (effect: JavaScriptSemanticMemberEffect) =>
|
|
semanticEffectPathMatches(effect.path, path) &&
|
|
effect.path.length % 2 === 0;
|
|
expect(semanticSomeEffectPathCandidate(effects, path, evenDepth)).toBe(
|
|
effects.some(evenDepth),
|
|
);
|
|
compared += expected.length;
|
|
}
|
|
expect(effects.length).toBeGreaterThan(400);
|
|
expect(compared).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("skips effects whose exact keys no selected choice names", () => {
|
|
const effects: JavaScriptSemanticMemberEffect[] = [
|
|
{ path: ["a", "x"], node: undefined },
|
|
{ path: ["b"], node: undefined },
|
|
{ path: [null, "x"], node: undefined },
|
|
{ path: ["a", "x", "y"], node: undefined },
|
|
{ path: [], node: undefined },
|
|
];
|
|
expect(
|
|
semanticEffectPathCandidates(effects, ["a", "x"]).map(({ path }) => path),
|
|
).toEqual([["a", "x"], [null, "x"], []]);
|
|
expect(
|
|
semanticEffectPathCandidates(effects, [{ alternatives: ["b", "c"] }]).map(
|
|
({ path }) => path,
|
|
),
|
|
).toEqual([["b"], []]);
|
|
});
|
|
|
|
it("stops visiting candidates at the first satisfied predicate", () => {
|
|
const effects: JavaScriptSemanticMemberEffect[] = Array.from(
|
|
{ length: 100 },
|
|
() => ({ path: ["a"], node: undefined }),
|
|
);
|
|
let visited = 0;
|
|
expect(
|
|
semanticSomeEffectPathCandidate(effects, ["a"], () => {
|
|
visited += 1;
|
|
return true;
|
|
}),
|
|
).toBe(true);
|
|
expect(visited).toBe(1);
|
|
expect(semanticSomeEffectPathCandidate(effects, ["b"], () => true)).toBe(
|
|
false,
|
|
);
|
|
});
|