mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 17:44:27 +00:00
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>
62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import {
|
|
analyzeParsedJavaScriptSemantics,
|
|
analyzeParsedJavaScriptSemanticsSteps,
|
|
} from "./javascriptSemanticAnalysis.js";
|
|
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
|
import {
|
|
traverseJavaScriptAst,
|
|
traverseJavaScriptAstSteps,
|
|
} from "./javascriptSemanticTraversal.js";
|
|
|
|
const source = `
|
|
import { readFile } from "node:fs";
|
|
const shared = { token: "TOKEN" };
|
|
export function load(path) {
|
|
return new Promise((resolve) => setTimeout(() => resolve(readFile(path)), 1));
|
|
}
|
|
export const box = { get() { return shared; } };
|
|
box.get().value = process.env.MODE ?? "default";
|
|
`;
|
|
|
|
it("yields between semantic phases and returns the synchronous result (#1462)", () => {
|
|
const parsed = parseJavaScriptSource(source, "main.js");
|
|
if (parsed === null) throw new Error("Expected parsed source");
|
|
const steps = analyzeParsedJavaScriptSemanticsSteps(parsed);
|
|
let yields = 0;
|
|
let step = steps.next();
|
|
while (step.done !== true) {
|
|
yields += 1;
|
|
step = steps.next();
|
|
}
|
|
// Every top-level phase and derived collector is a separate step.
|
|
expect(yields).toBeGreaterThanOrEqual(13);
|
|
const reparsed = parseJavaScriptSource(source, "main.js");
|
|
if (reparsed === null) throw new Error("Expected parsed source");
|
|
expect(step.value).toEqual(analyzeParsedJavaScriptSemantics(reparsed));
|
|
});
|
|
|
|
it("traverses a large tree in steps with the synchronous visit order (#1462)", () => {
|
|
const parsed = parseJavaScriptSource(
|
|
Array.from(
|
|
{ length: 400 },
|
|
(_, index) => `const v${index} = [${index}];`,
|
|
).join("\n"),
|
|
"large.js",
|
|
);
|
|
if (parsed === null) throw new Error("Expected parsed source");
|
|
const visits = (record: string[]) => ({
|
|
enter: (node: { readonly type: string }) => record.push(`+${node.type}`),
|
|
exit: (node: { readonly type: string }) => record.push(`-${node.type}`),
|
|
});
|
|
const synchronous: string[] = [];
|
|
traverseJavaScriptAst(parsed.program, visits(synchronous));
|
|
const stepped: string[] = [];
|
|
const steps = traverseJavaScriptAstSteps(parsed.program, visits(stepped));
|
|
let yields = 0;
|
|
while (steps.next().done !== true) yields += 1;
|
|
expect(yields).toBeGreaterThan(0);
|
|
expect(stepped).toEqual(synchronous);
|
|
});
|