Repository navigation
perf(javascript): reuse semantic member-mutation lookups - #2941
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
034a93d to
9f46d5f
Compare
The semantic member-mutation collector revisits the same nodes while it closes alias chains, and each visit re-derived results that cannot change within the phase: - resolveSemanticBindingState re-walked the scope chain for the same identifier node. Definition collection completes before this phase, and addBinding is reachable only from it, so a scope's bindings no longer change. - directInitializerPosition, directMutationPosition and referenceInitializerPosition are pure functions of a node and the analysis parent map, but were recomputed for every mutation and initializer. - markValue re-encoded the same capture tuple with JSON.stringify and built a fresh long key for every Set/Map lookup, so each lookup re-hashed the whole key. The capture key is now cached per binding, origin and owner, and the lifetime key is derived from it. On pdfjs-dist 5.3.93 wasm/openjpeg_nowasm_fallback.js (462316 bytes, sha256 53c228428a8861035a34738966af263fc9d4cee6df25a0ad942af947469a0bd9) the semantic phase takes 177.8 s instead of 272.9 s in-process, with a byte-identical semantic IR. With --max-heap-mb 3072 and the default heap the base revision exhausts the worker; with the default 300 s per-source deadline raised, the completed CLI run takes 240.6 s instead of 396.7 s and its output is identical apart from volatile identifiers. Refs morluto#2498
9f46d5f to
aacf970
Compare
|
Thanks for profiling the repeated binding and key lookups and removing that repeated work, @gongfpp. The concrete bundle workload makes the optimization easier to assess. Welcome to REA! |
Summary
The semantic member-mutation collector visits the same nodes many times while it closes alias chains, and each visit re-derived three things that cannot change within the phase. On the public input from #2498 this patch keeps the produced semantic IR byte-identical and cuts semantic-phase work measurably.
Refs #2498. This addresses the measured per-iteration costs only; the reporter's own instrumentation shows the explored alias/origin space is the larger problem, and the issue leaves the lattice bound to maintainers. The memory ceiling is therefore not claimed as resolved here.
Problem and expected behavior
For
openjpeg_nowasm_fallback.js(462,316 bytes, SHA-25653c228428a8861035a34738966af263fc9d4cee6df25a0ad942af947469a0bd9, from[email protected]), an in-process--cpu-profof the semantic phase attributes self time tomarkValue38.6%,resolveSemanticBindingState15.7%, the initializer position helpers ~15% and GC 8%.Three of those costs come from recomputation that the phase cannot invalidate:
resolveSemanticBindingStatere-walked the scope chain for every visit of the same identifier node.collectDefinitionsStepscompletes beforecollectSemanticMemberMutationsSteps, andaddBindingis reachable only from definition collection (it also returns early for a scope that is not complete), so no binding can appear while the mutation phase runs.directInitializerPosition,directMutationPositionandreferenceInitializerPositionare pure functions of a node and the analysis's parent map, but were recomputed for every mutation and every initializer.markValuere-encoded the same capture tuple withJSON.stringifyand built a fresh long string key perSet/Maplookup, so each lookup re-hashed the whole key.Expected behavior: identical semantic output, less time and allocation in the semantic phase.
Change and scope
javascriptSemanticMemberMutations.ts: a phase-local memo resolves an identifier node to its binding once (addBindingcannot run during the phase); a capture key is cached per (binding, origin, owner) and the lifetime key is derived from it, which also lets V8 reuse the cached string hash on lookup.javascriptSemanticMutationInitializers.ts: the three position lookups are memoized per analysis parent map. The map is built once per analysis and never changes, and using it as the cache key bounds entry lifetime to the analysis that owns it.Intentionally not included: the retained worklist and alias-chain state that sets the peak memory and the default-heap ceiling. The patch does not change that ceiling (see measurements).
Follow the contribution scope. Unresolved scope, authority, side effects, or required verification block integration. This is an ordinary fix inside existing boundaries; no new provider, capability, execution, injection, mutation or protection recovery. Maintainer scope decision: not applicable.
Contract and boundary impact
collectSemanticMemberMutationsStepsand its origin selection injavascriptSemanticMutationInitializers.ts.docs/public/product-catalog.json), package, or installation impact: none.Expected effects (check known effects; use documentation-only on its own).
Evidence and regression coverage
--cpu-profprofile of the semantic phase on the base revision. The input is a public npm artifact ([email protected]); it is not committed.cmpreports no difference), and the two completed CLI envelopes compare equal across 979,087 flattened paths once volatile identifiers and captured worker logs are excluded. At default settings both revisions return the sameresource_constraintenvelope for this input on this host.--max-heap-mb 3072. Peak RSS did not move measurably, and the default-heap ceiling is unchanged.For evidence-bearing changes:
Validation performed
Host: macOS 26 (Apple silicon), Node 24.19.0, revision
3e7c6302plus this patch; the machine carried unrelated load (load average 5.0–6.5, swap in use), so these are medians/estimates from one host.analyze-javascript-application app --json, default heap, median of 3At the default worker heap (
--max-heap-mb 1024) both revisions fail this input on this host withresource_constraint: "Owned JavaScript worker exhausted its heap while analyzing openjpeg_nowasm_fallback.js during semantic". At--max-heap-mb 3072the base revision also exceeds the default 300 s per-source deadline while this revision finishes inside it. With--max-heap-mb 3072andcoverage.status: partial(the per-file semantic node budget is reached), the completed run takes 240.6 s patched and 396.7 s on base.npm run test:focused -- src/domain/javascript/javascriptSemanticMutationInitializers.test.ts src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts— 188 tests passednpm run test:focused -- <29 javascriptSemantic*.test.ts / javascriptFeatureSeed*.test.ts>— 717 tests passednpm run check:fast— 2 tasks successful (typecheck, lint)npx oxfmt --check <changed files>— correct formatcmpof the two IR serializations — no difference/usr/bin/time -l node scripts/rea.mjs analyze-javascript-application /tmp/rea-2498/app --json [--max-heap-mb 3072]— table aboveCompatibility, safety, and release
Before merge
type(scope): outcome.