Skip to content

perf(javascript): share one lexical index between static and semantic analysis - #14148

Merged
morluto merged 3 commits into
mainfrom
perf/javascript-share-lexical-index
Nov 23, 2026
Merged

morluto merged 3 commits into
mainfrom
perf/javascript-share-lexical-index

Conversation

@morluto

@morluto morluto commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Summary

Refs #14147 (step 1 of 2; traversal fusion is a follow-up).

The JavaScript analysis worker ran its static and semantic phases on two private semantic states for the same parsed program. Each built definitions, ambient writes and member-mutation facts separately, which was about 22% of analysis time on @babel/parser (profile in #14147). This PR builds one lexical index per parsed file and has the semantic phase continue from it.

Design

Sharing the state object alone is wrong. A naive prototype changed semantic output on openpgp.min.js, where static-phase queries leaked two returned-reference widenings into the semantic IR. The state held three kinds of data with different owners, and this PR separates them (javascriptSemanticConsumer.ts):

Kind Owner Change
Index facts (scopes, bindings, retained effects) the index steps shared, unchanged
Query memos (15 module-level WeakMaps keyed by the state) the consumer that derived them all declared through semanticConsumerMemo(), so a new consumer or index step starts without them
Observations (valueResourceLimits, getterResourceLimits, returnedReferenceWidenings) one derivation grouped as state.observations; index steps record into state.indexObservations, and each consumer starts from that log
  • semanticIndexStep wraps the three index steps (definitions, ambient writes, member-mutation facts). A step reads no consumer memo and records into the index's log, so its facts do not depend on which consumer requested it or what that consumer queried first.
  • analyzeParsedJavaScriptSemanticsSteps accepts a lexicalIndex and fails closed. The index must come from indexJavaScriptBindingsWithReceiversSteps with no host context and the same program, resolution mode, precision and allowance; anything else throws TypeError.
  • The worker shares the index only when the semantic resolution mode is "import", the mode the static phase indexes under. .cjs/.cts and .ts/.tsx files keep private states, because their mode changes the definitions themselves.

Validation

  • Corpus differential (outputs unchanged): 1,443 real JavaScript files (35.7 MB) sampled from node_modules, plus repo fixtures. For each file I compared SHA-256 of the static facts and of the semantic IR against main. 0 differences with private indexes (the refactor changes nothing by itself) and 0 differences with the shared index (1,352 files took the shared path; the rest have another resolution mode). 64 files hit resource limits and 185 report widenings, so the observation paths are exercised. Running main twice gave identical hashes. The harness is a local script, not part of this PR.
  • New regression tests (javascriptSemanticSharedIndex.test.ts): shared-index results equal private-index results; an earlier consumer's observations stay out of the semantic phase; index-step observations reach the semantic phase; mismatched indexes are rejected. Working control: the isolation test fails with the consumer handoff disabled.
  • src/domain/javascript/*.test.ts and src/javascript/analysis/*.test.ts: 209 files, 6,235 tests passed.
  • End to end through the real worker (CLI and MCP): javascriptReturnedReferences and javascriptAnalysisLimits acceptance tests passed.
  • oxfmt --check; check:fast (typecheck, lint, architecture guards).

Measurement

In-process, both phases, on a 360-file (10.7 MB) sample of the corpus; two sequential runs each on macOS arm64, Node 24.22.1:

run 1 run 2
main 39.1 s 41.2 s
this PR (shared index) 35.7 s 34.5 s

That is 9–16% less wall time on this sample. Not measured: CPU time, peak memory, and the end-to-end worker. Agent tokens are unaffected, since outputs are identical.

Remaining

… analysis

The static and semantic phases each built a private semantic state for
the same parsed program, so definitions, ambient writes and
member-mutation facts were derived twice per file.

Sharing the state requires separating what it holds. Index facts are
shared. Memoized query results are declared through one owner so a new
consumer or index step starts without them. Observations (resource
limits and returned-reference widenings) are recorded per derivation:
index steps record into the index's own log and each consumer starts
from that log, so the semantic phase reports exactly what a private
index would have.

The worker builds one index per parsed file and the semantic phase
continues from it when its resolution mode matches the static phase.

Refs #14147
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-23T20:38:33.733268Z
We couldn’t complete this review. Comment @codex review to try again.
8c8ceb4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects remain from this review.

Summary

The worker reuses one lexical index between static and semantic analysis when their resolution modes match.

  • The JavaScript worker reuses one lexical index when both analysis phases can share it.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  P[Parsed program] --> I[Build lexical index]
  I --> S[Static analysis]
  S --> M{Semantic mode is import?}
  M -->|Yes| R[Check and consume shared index]
  M -->|No| N[Build private semantic index]
  R --> C[Reset query memos and seed index observations]
  C --> A[Semantic analysis]
  N --> A
Loading

Reviews (2) · Last reviewed commit: "fix(javascript): let a shared lexical in..." · Reviewed by Greptile

@morluto

morluto commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

Integration note with #14153. See the note there: the two merge cleanly but need a one-line adjustment to compile together, because this PR moves the limit sets under state.observations and #14153 adds one new use of the old name. I verified the combined stack with that adjustment.

…c phase

The semantic phase appends its module links and callable facts to the
state it continues from, so a second phase from the same index reported
each import link twice. Admission is now consumed by the first phase and
a second is rejected like any other index that does not match.
@morluto
morluto enabled auto-merge (squash) November 23, 2026 23:00
@morluto
morluto merged commit 24d2c4f into main Nov 23, 2026
44 checks passed
@morluto
morluto deleted the perf/javascript-share-lexical-index branch November 23, 2026 23:20
morluto added a commit that referenced this pull request Nov 24, 2026
…tions (#14220)

#14153 added a collapsed member-mutation query that recorded its
resource limit on state.valueResourceLimits. #14148, merged after it,
moved that set under state.observations, so the line failed typecheck
and threw at runtime when a query exceeded its state budget.
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior performance Performance regressions, scaling limits, and bounded-resource work labels Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant