From 14c6af9c270cdb91cfaadc4e273433c135607d13 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:52:21 -0500 Subject: [PATCH] perf(javascript): skip settled widened alias expansions (#12224) --- .../javascriptSemanticAmbientWrites.ts | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index 04696eee9b..addb1dc447 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -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(); + // Null marks a fully widened root. An absent entry has not been visited. + const expanded = new WeakMap(); + 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(); @@ -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"); } }