perf(javascript): skip settled widened alias expansions (#12224)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 05:52:21 +00:00
1 parent c34a00c70a
commit 14c6af9c27
1 file changed
+36 -28
@@ -249,7 +249,7 @@ export const createAmbientPathResolver = (
type Pending = {
node: t.Node;
suffix: AmbientProjection;
readsAtSelf?: true;
readsAtSelf: true | undefined;
};
// A binding read queues its selected initializers for one projection,
// taken from the end as if queued one by one. Reads selecting the same
@@ -262,7 +262,7 @@ export const createAmbientPathResolver = (
remaining: number;
}
const pending: (Pending | QueuedInitializers)[] = [
{ node, suffix: projection },
{ node, suffix: projection, readsAtSelf: undefined },
];
const queuedInitializers = new Map<
JavaScriptSemanticBindingState,
@@ -282,15 +282,29 @@ export const createAmbientPathResolver = (
// projections that still end at the written key. Once a step consumes that
// key, the item denotes the value read from the written slot. Replacing
// the slot does not mutate that value or the containers it was read from.
const admitted = (item: Pending): boolean =>
receiver === undefined || item.suffix?.length !== 0;
const queue = (item: Pending): void => {
if (admitted(item)) pending.push(item);
};
const admitted = (suffix: AmbientProjection): boolean =>
receiver === undefined || suffix?.length !== 0;
// Join projections at their producing expression, rather than enumerating
// alias histories. Each key can only widen to a wildcard, and conflicting
// depths widen to unknown, so diamonds and cycles have finite state.
const expanded = new WeakMap<t.Node, AmbientProjection>();
// Null marks a fully widened root. An absent entry has not been visited.
const expanded = new WeakMap<t.Node, AmbientPath | null>();
let continued = false;
const queue = (
node: t.Node,
suffix: AmbientProjection,
readsAtSelf?: true,
): void => {
if (!admitted(suffix)) return;
continued = true;
// A fully widened root cannot gain a new fact from another widened read.
// Keep the continuation for coverage even when no expansion is needed.
if (suffix === undefined) {
const root = memberPaths.get(node)?.root;
if (root !== undefined && expanded.get(root) === null) return;
}
pending.push({ node, suffix, readsAtSelf });
};
const retain = (path: AmbientPath) => {
let canonical = canonicalAmbientPath(path);
for (;;) {
@@ -318,7 +332,7 @@ export const createAmbientPathResolver = (
for (;;) {
const current = take();
if (current === undefined) break;
const queuedBefore = pending.length;
continued = false;
const pathsBefore = result.size;
let resolvedIdentity = false;
try {
@@ -332,9 +346,9 @@ export const createAmbientPathResolver = (
current.suffix === undefined || members.length === 0
? current.suffix
: [...members, ...current.suffix];
if (expanded.has(root)) {
const previous = expanded.get(root);
if (previous === undefined) {
const previous = expanded.get(root);
if (previous !== undefined) {
if (previous === null) {
resolvedIdentity = true;
continue;
}
@@ -357,17 +371,13 @@ export const createAmbientPathResolver = (
return unresolved;
}
}
expanded.set(root, suffix);
expanded.set(root, suffix ?? null);
if (suffix === undefined || suffix.includes(null))
unknownReasons.add("widened-projection");
// A widened projection is a possible write, never proof of an intrinsic.
if (suffix === undefined && !possibleWrites) continue;
const continueWith = (node: t.Node, path: AmbientProjection = suffix) =>
queue({
node,
suffix: path,
...(current.readsAtSelf && { readsAtSelf: true }),
});
queue(node, path, current.readsAtSelf);
const continueReturned = (callee: t.Node, path: AmbientProjection) => {
const returned = returnedReferences(callee);
// Returned references retain possible source-local values, not a
@@ -403,7 +413,7 @@ export const createAmbientPathResolver = (
escapedContents,
);
if (stored === null) continue;
queue({ node: source.node, readsAtSelf: true, suffix: stored });
queue(source.node, stored, true);
}
}
if (
@@ -466,8 +476,7 @@ export const createAmbientPathResolver = (
const argument = possibleWrites
? semanticDirectInvocationArgument(root, binding, state)
: undefined;
if (argument !== undefined)
queue({ node: argument, readsAtSelf: true, suffix });
if (argument !== undefined) queue(argument, suffix, true);
const bySuffix =
queuedInitializers.get(selected) ??
new Map<string | undefined, QueuedInitializers>();
@@ -508,14 +517,17 @@ export const createAmbientPathResolver = (
: String(key),
),
};
if (admitted(item)) items.push(item);
if (admitted(item.suffix)) items.push(item);
}
queued = { items, remaining: items.length };
bySuffix.set(suffixKey, queued);
}
// Taken items still count as queued for this read: requeued, they
// would have been the same no-op revisits.
if (queued.items.length > 0) pending.push(queued);
if (queued.items.length > 0) {
continued = true;
pending.push(queued);
}
continue;
}
if (
@@ -676,11 +688,7 @@ export const createAmbientPathResolver = (
}
}
} finally {
if (
!resolvedIdentity &&
pending.length === queuedBefore &&
result.size === pathsBefore
)
if (!resolvedIdentity && !continued && result.size === pathsBefore)
unknownReasons.add("unresolved-value");
}
}