perf(javascript): index widened summary coverage by root (#13007)

* perf(javascript): reuse lexical returned-reference facts across ambient writes

* test(javascript): count returned-reference dependency checks by binding

* perf(javascript): collect return dependency facts once per binding

* ci: run required verification for stacked pull requests

* perf(javascript): index widened summary coverage by root

* test(javascript): count canonical keys during widened effect merging

* perf(javascript): reuse canonical keys from widened effect summaries
This commit is contained in:
Kaoru0822 authored and GitHub committed 2026-11-20 23:12:23 -08:00
1 parent d43fc10511
commit f2d5cd6337
2 files changed
+156 -15

No files matched your search

@@ -1,5 +1,5 @@
import * as t from "@babel/types";
import { expect, it } from "vitest";
import { expect, it, vi } from "vitest";
import { analyzeJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js";
import { indexJavaScriptBindingsWithReceiversSteps } from "./javascriptSemanticAnalysis.js";
@@ -344,6 +344,103 @@ it.each([
expect(work(64)).toBeLessThan(3 * small);
});
it("merges widened effects with their existing canonical path keys", () => {
const work = (count: number): number => {
const source =
widenedStoreCycle +
Array.from(
{ length: count },
(_, index) => `a[k[0]][k[1]].p${index}=witness${index};`,
).join("") +
"consume(a);".repeat(16);
const parsed = parseJavaScriptSource(source);
if (parsed === null) throw new Error("Expected widened path fixture");
const index = completeSemanticSteps(
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
);
const stringify = vi.spyOn(JSON, "stringify");
let keys: number;
try {
completeSemanticSteps(index.indexAmbientWritesSteps());
keys = stringify.mock.calls.filter(
([path]) =>
Array.isArray(path) &&
path[0] === "global" &&
typeof path[1] === "string" &&
path[1].startsWith("witness"),
).length;
} finally {
stringify.mockRestore();
}
const last = parsed.program.body.at(-1);
if (!t.isExpressionStatement(last))
throw new Error("Expected final escape site");
expect(
new Set(
index.bindings.ambientEscapeEffects
.filter(({ node }) => node === last.expression)
.map(({ path }) => path[1]),
),
).toEqual(new Set(Array.from({ length: count }, (_, i) => `witness${i}`)));
return keys;
};
const small = work(64);
expect(small).toBeGreaterThan(0);
expect(work(128)).toBeLessThan(3 * small);
});
it("queries settled widening coverage without scanning unrelated groups", () => {
const work = (count: number): number => {
const groups = Array.from({ length: count }, (_, index) => ({
a: `a${index}`,
b: `b${index}`,
value: index === 0 ? "window" : index === count - 1 ? "module" : "{}",
}));
const source =
"var k=[];" +
groups
.map(
({ a, b, value }) =>
`var ${a}=[],${b}=[];${a}[k[0]]=${b};${b}[k[0]]=${a};${a}[k[0]][k[1]].slot=${value};`,
)
.join("") +
`consume([${groups.map(({ a }) => a).join(",")}]);`;
const parsed = parseJavaScriptSource(source);
if (parsed === null) throw new Error("Expected widened groups fixture");
const index = completeSemanticSteps(
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
);
// The old resolver used one WeakMap membership probe per earlier group
// for each root reached by the escape query. Count those probes without
// wall-clock thresholds, and check that both distant groups contribute.
const original = WeakMap.prototype.has;
let probes = 0;
WeakMap.prototype.has = function (key) {
probes += 1;
return original.call(this, key);
};
try {
completeSemanticSteps(index.indexAmbientWritesSteps());
} finally {
WeakMap.prototype.has = original;
}
const call = parsed.program.body.at(-1);
const escape = t.isExpressionStatement(call) ? call.expression : undefined;
expect(
index.bindings.ambientEscapeEffects
.filter(({ node }) => node === escape)
.map(({ path }) => path[0])
.sort(),
).toEqual(["commonjs-module", "global"]);
return probes;
};
const small = work(24);
expect(work(48)).toBeLessThanOrEqual(3 * small + 96);
});
it("propagates widened values through reference stores discovered later", () => {
const source =
widenedStoreCycle +
@@ -281,10 +281,45 @@ interface AmbientReferenceFacts {
readonly arrayIteratorReceiver: (node: t.Node) => boolean;
}
interface WidenedSummarySlot {
readonly word: number;
readonly bit: number;
}
/** Index covered roots in the direction queried by the live walk. */
class WidenedSummaryCoverage {
readonly #roots = new WeakMap<t.Node, Map<number, number>>();
#nextSlot = 0;
register(roots: Iterable<t.Node>): WidenedSummarySlot {
const index = this.#nextSlot++;
const word = Math.floor(index / 32);
const bit = 1 << (index % 32);
for (const root of roots) {
let memberships = this.#roots.get(root);
if (memberships === undefined) {
memberships = new Map();
this.#roots.set(root, memberships);
}
memberships.set(word, (memberships.get(word) ?? 0) | bit);
}
return { word, bit };
}
covered(root: t.Node, active: ReadonlyMap<number, number>): boolean {
if (active.size === 0) return false;
const memberships = this.#roots.get(root);
if (memberships === undefined) return false;
for (const [word, bits] of memberships)
if ((bits & (active.get(word) ?? 0)) !== 0) return true;
return false;
}
}
interface WidenedStoreEffects {
readonly frontier: ReadonlySet<t.Node>;
readonly roots: WeakMap<t.Node, AmbientPath | null>;
readonly paths: readonly AmbientPath[];
readonly slot: WidenedSummarySlot;
readonly paths: ReadonlyMap<string, AmbientPath>;
readonly unknownReasons: readonly AmbientIdentityUnknownReason[];
readonly projectedSpread: boolean;
}
@@ -340,6 +375,7 @@ export const createAmbientPathResolver = (
const returnedReferences = createSemanticReturnedReferences(state);
// Group values are read at their own sites. Once reference stores settle,
// their any-depth effects are independent of the outer capture and suffix.
const coverage = new WidenedSummaryCoverage();
const widenedEffects = new WeakMap<WidenedStoreGroup, WidenedStoreEffects>();
const widenedEscapes = new WeakMap<WidenedStoreGroup, WidenedStoreEffects>();
const resolve = (
@@ -360,7 +396,14 @@ export const createAmbientPathResolver = (
): AmbientResolution => {
const prior = possibleWrites ? undefined : cached.get(node);
if (prior !== undefined) return prior;
const frontier = summary === undefined ? undefined : new Set<t.Node>();
const summaryWalk =
summary === undefined
? undefined
: {
frontier: new Set<t.Node>(),
roots: new Map<t.Node, AmbientPath | null>(),
};
const frontier = summaryWalk?.frontier;
const result = new Map<string, AmbientPath>();
const callerPaths = new Map<string, AmbientPath>();
let hasLocalIdentity = false;
@@ -413,7 +456,7 @@ export const createAmbientPathResolver = (
const expanded =
receiver?.kind === "widened"
? receiver.expanded
: new WeakMap<t.Node, AmbientPath | null>();
: (summaryWalk?.roots ?? new WeakMap<t.Node, AmbientPath | null>());
const expandedAtCapture =
receiver?.kind === "widened" && capture !== undefined
? new WeakMap<t.Node, AmbientPath | null>()
@@ -421,7 +464,7 @@ export const createAmbientPathResolver = (
const queuedGroups = new Set<WidenedStoreGroup>();
// Apply summaries in worklist order. A completed summary dominates later
// visits to its roots just as expanding them at any depth would.
const summarizedRoots: WeakMap<t.Node, AmbientPath | null>[] = [];
const activeSummaries = new Map<number, number>();
// Array-receiver reads filter stores at their capture. Tentative spread
// reads can restore a finite prototype path from an any-depth projection.
const summarizeGroups =
@@ -448,7 +491,7 @@ export const createAmbientPathResolver = (
if (
root !== undefined &&
((readsAtSelf ? expanded : expandedAtCapture).get(root) === null ||
summarizedRoots.some((roots) => roots.has(root)))
coverage.covered(root, activeSummaries))
)
return;
}
@@ -507,13 +550,13 @@ export const createAmbientPathResolver = (
);
}
if (effects !== undefined) {
for (const path of effects.paths)
result.set(JSON.stringify(path), path);
for (const [key, path] of effects.paths) result.set(key, path);
for (const reason of effects.unknownReasons)
unknownReasons.add(reason);
if (observes === "ambient-effects" && effects.projectedSpread)
projectedSpread = true;
summarizedRoots.push(effects.roots);
const { word, bit } = effects.slot;
activeSummaries.set(word, (activeSummaries.get(word) ?? 0) | bit);
for (const next of effects.frontier) queue(next, undefined, true);
}
continue;
@@ -561,7 +604,7 @@ export const createAmbientPathResolver = (
]
: current.capturedSelections;
const expansions = current.readsAtSelf ? expanded : expandedAtCapture;
const previous = summarizedRoots.some((roots) => roots.has(root))
const previous = coverage.covered(root, activeSummaries)
? null
: expansions.get(root);
if (previous !== undefined) {
@@ -1113,11 +1156,12 @@ export const createAmbientPathResolver = (
const paths = [...result.values()];
// A separate walk cannot let a tentative selection mask an ordinary one
// in the identity cache, or publish unrelated tentative ambient paths.
if (frontier !== undefined)
if (summaryWalk !== undefined)
summary?.retain({
frontier,
roots: expanded,
paths,
frontier: summaryWalk.frontier,
slot: coverage.register(summaryWalk.roots.keys()),
// The summary owns the canonical keys produced by retain().
paths: result,
unknownReasons: [...unknownReasons],
projectedSpread,
});