fix(javascript): yield during the largest AST traversals (#1545)

After #1542 the longest event-loop blocks on large bundles were single AST
traversals inside one analyzer: member-mutation collection with its
fixpoint, static source analysis, and function fingerprints, each about
one second on Obsidian's 3.7 MB app.js.

- traverseJavaScriptAstSteps yields every 1,024 visited nodes with the
  same visit order; traverseJavaScriptAst drains it, so every existing
  caller is unchanged.
- Member mutations, static analysis and fingerprints run as steps. The
  member-mutation fixpoint yields every 64 references, and fingerprints
  yield between callables.
- The now-unused synchronous member-mutation and fingerprint wrappers are
  removed.

Graph IDs and complete result digests match main on three inputs,
including obsidian.asar. Over two runs there, the longest block falls
from about 920 ms to 740 ms, the second from 860 to 595 ms, and blocks
over 250 ms from 27 to 18, with total time within 1.5%.

Refs #1462

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822andClaude Opus 5.5 authored and GitHub committed 2026-10-10 20:27:41 +08:00
1 parent e964a06b83
commit 5d6eeb22fb
8 files changed
+166 -97

No files matched your search

@@ -12,15 +12,28 @@ import * as t from "@babel/types";
*/
export const traverseJavaScriptAst = (
root: t.Node,
visitor: {
readonly enter: (
node: t.Node,
parent: t.Node | null,
readAncestors: () => readonly t.Node[],
) => void;
readonly exit?: (node: t.Node, parent: t.Node | null) => void;
},
): void => {
visitor: JavaScriptAstVisitor,
): void => completeSemanticSteps(traverseJavaScriptAstSteps(root, visitor));
/** Callbacks for one deterministic AST traversal. */
export interface JavaScriptAstVisitor {
readonly enter: (
node: t.Node,
parent: t.Node | null,
readAncestors: () => readonly t.Node[],
) => void;
readonly exit?: (node: t.Node, parent: t.Node | null) => void;
}
/**
* {@link traverseJavaScriptAst} with a yield every 1,024 visited nodes, so a
* caller can serve control messages during one large traversal. Visit order
* and callbacks are identical.
*/
export function* traverseJavaScriptAstSteps(
root: t.Node,
visitor: JavaScriptAstVisitor,
): Generator<void, void> {
const ancestors: t.Node[] = [];
const enter = (node: t.Node, parent: t.Node | null): TraversalFrame => {
visitor.enter(node, parent, () => [...ancestors]);
@@ -28,12 +41,15 @@ export const traverseJavaScriptAst = (
return { node, parent, children: childNodes(node), nextIndex: 0 };
};
const pending = [enter(root, null)];
let visited = 1;
while (pending.length > 0) {
const current = pending.at(-1);
if (current === undefined) break;
const child = current.children[current.nextIndex];
if (child !== undefined) {
current.nextIndex += 1;
if (visited % 1024 === 0) yield;
visited += 1;
pending.push(enter(child, current.node));
} else {
visitor.exit?.(current.node, current.parent);
@@ -41,6 +57,16 @@ export const traverseJavaScriptAst = (
pending.pop();
}
}
}
/** Run cooperative analysis steps to completion without yielding control. */
export const completeSemanticSteps = <Value>(
steps: Iterator<void, Value>,
): Value => {
for (;;) {
const step = steps.next();
if (step.done === true) return step.value;
}
};
interface TraversalFrame {