Skip to content

perf(javascript): queue binding initializers once per ambient alias query - #10681

Merged
morluto merged 3 commits into
morluto:mainfrom
Davmunrey:perf/ambient-write-index
Nov 19, 2026
Merged

morluto merged 3 commits into
morluto:mainfrom
Davmunrey:perf/ambient-write-index

Conversation

@Davmunrey

Copy link
Copy Markdown
Contributor

Summary

Refs #2498 (partial). In createAmbientPathResolver (src/domain/javascript/javascriptSemanticAmbientWrites.ts), every identifier read that one query reached requeued all of its binding's initializers. In wasm2js output, locals copy each other inside loops, so one escape query reaches hundreds of reads of bindings with up to 775 initializers. Work per query therefore grew quadratically. Before this change, the first 8,000 escape queries made 150 M queue pops for 5.4 M unique items.

Reads that select the same initializers for the same projection now share one queue entry. Its items are taken in the original last-in-first-out order, and items already taken are not requeued. Every expansion that does real work runs in the same order as before, so results are unchanged.

Evidence and regression coverage

  • New work-count test in javascriptSemanticAmbientWrites.test.ts: a looping a=b; b=a cycle with one escape, counting reads of the copied values.
    • On the base it fails: 5,551 reads for 32 copies and 20,319 for 64 ("expected 20319 to be less than 16653").
    • After the change: 1,087 and 2,175 reads, with the same retained escape.
  • [email protected] openjpeg_nowasm_fallback.js (the sha256 matches the issue). One cold run per configuration on a 4-vCPU host; warm runs were not measured.
Measurement Before After
Ambient indexing, in process 137.4 s, CPU 154.0 s, 863 MB 26.4 s, CPU 31.0 s, 388 MB
CLI, default 300 s deadline timed out at 301 s, CPU 359 s exited 0 at 246 s, CPU 331 s; source interrupted (heap exhausted in static phase)

Validation performed

  • npm run test:local over src/domain/javascript (187 files, 5,817 tests) and src/application/javascript (31 files): passed.
  • npm run build:cached and npm run check:fast: passed.

Author readiness

Human owner: @Davmunrey

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

…uery

Ambient-write indexing resolves every escaped call argument and method
receiver through the ambient path resolver. Within one query, each
identifier read it reached requeued every selected initializer of its
binding, although an initializer already expanded with the same
projection only revisits its expanded root. Where locals copy each other
inside loops, as in wasm2js output, one query reaches hundreds of reads of
bindings with hundreds of initializers, so the work per query grew
quadratically. On pdf.js's openjpeg_nowasm_fallback.js, ambient indexing
alone took 137 s in-process.

Reads that select the same initializers for the same projection now share
one queue entry, whose remaining items are taken in the original
last-in-first-out order. Items already taken are not requeued. Every
expansion that is not a revisit happens in the same order as before, so
resolutions are unchanged.

Validation:
- New work-count regression: during the single escape query of a looping
  a=b/b=a cycle, reads of the copied values are 1087 and 2175 for 32 and
  64 copies (base: 5551 and 20319). The test fails on the base revision.
- npm run test:local over src/domain/javascript (187 files, 5817 tests)
  and src/application/javascript (31 files, 695 tests): pass.
- Ambient write and escape effects, retained global call receivers and
  escaping reference stores are byte-identical before and after on
  openjpeg_nowasm_fallback.js. Static facts and member-mutation state are
  byte-identical on 12 other bundles.
- openjpeg_nowasm_fallback.js, ambient indexing in-process from the built
  dist, one cold run each on a 4 vCPU host: 137.4 s -> 26.4 s; process
  CPU 154.0 s -> 31.0 s; peak RSS 863 MB -> 388 MB.
- npm run check:fast: pass.

The CLI still does not complete that file: its worker now exhausts the
1024 MiB heap in member-mutation traversal after 246 s, rather than
reaching the 300 s per-source deadline.

Refs morluto#2498
@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-14T23:23:13.482005Z
We couldn’t complete this review. Comment @codex review to try again.
9dae322 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

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; no blocking issues were found.

Summary

This PR queues each binding's selected initializers once per projection within an ambient alias query.

  • Ambient path queries reuse initializer work across matching reads.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read a binding] --> B[Select initializers at this read]
  B --> C{Binding and projection already queued?}
  C -->|No| D[Build initializer items]
  C -->|Yes| E[Reuse shared cursor]
  D --> E
  E --> F[Take last remaining item]
  F --> G{Root needs expansion?}
  G -->|Yes| H[Follow references and retain paths]
  G -->|No| I[Skip repeated work]
Loading

Reviews (2) · Last reviewed commit: "Merge morluto/rea main into ambient-writ..." · Reviewed by Greptile

@morluto
morluto merged commit 80af53b into morluto:main Nov 19, 2026
38 checks passed
Davmunrey added a commit to Davmunrey/Davmunrey that referenced this pull request Nov 19, 2026
@morluto morluto added performance Performance regressions, scaling limits, and bounded-resource work area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Nov 22, 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.

2 participants