Repository navigation
perf(javascript): prune typed-array element reference escapes - #5139
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. |
[Medium impact] The reviewed changes appear safe to merge; no actionable defect was found. SummaryThis PR stops following object references through proven numeric reads from native typed arrays.
The previous and current commits have different merge bases with the target commit, so the full PR diff was reviewed. No numbered previous review threads were supplied. Reviews (3) · Last reviewed commit: "Merge origin/main into perf/primitive-re..." · Reviewed by Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2fcfab5bf9
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (origin === "typed-array") { | ||
| // Custom properties can hold references; only canonical numeric | ||
| // indices have the native Number/BigInt/undefined result contract. | ||
| if (!numericIndex) add("opaque"); |
There was a problem hiding this comment.
Preserve projected references from typed-array element primitives
When a proven numeric read is followed by another member selection, for example const n = new Int32Array(1)[0]; consume(n.constructor), this branch leaves the read observer with no origin, so normalize(..., "escape") also prunes every descendant projection. The element itself is primitive, but property lookup boxes Number/BigInt and can return object references such as constructor, toString, or a prototype getter; those objects then escape without any mutation effect being recorded. Preserve a primitive/prototype origin for nonempty paths and prune only the element value itself.
AGENTS.md reference: AGENTS.md:L37-L39
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
TL;DR: I checked the one open bot thread against f75b17f. It was real on the first commit, 2fcfab5, and it does not reproduce on the current head. The new tests pass on Node 22, 24 and 26, and I did not re-measure the speed or memory numbers.
Edit (21 Oct): Retested on 1e075c2 after the merge of main. It still works. See the retest.
Finding and verdict
Head f75b17f. The PR changes 3 files against a80575c (+410 -10). "Reproduces" means the escape was lost.
| Thread | Sev | Verdict | What I saw |
|---|---|---|---|
| Preserve projected references from typed-array element primitives | P1 | Fixed in f75b17f | The bot's own snippet, const n = new Int32Array(1)[0]; consume(n.constructor), lost its escape on 2fcfab5 and keeps it on head, as it does on the base. On head I also tried 11 other reads off an element, and all kept the escape: n['constructor'], n?.constructor, n.toString as a value, an alias, a conditional alias, n.constructor.prototype, n[prop], n[0], a getter added to Number.prototype, a destructured constructor, and a view read through heap[index >> 2]. The snippet with BigInt64Array kept it too. The plain consume(n) is still pruned, so the speed-up path stays. On 2fcfab5 the 10 property tests the PR body mentions all fail, as the body says |
The thread still shows as open, but GitHub marks it outdated, and the fix came after it was written.
I noticed one more behaviour change. It is not in the bot thread. consume(n.toString()) on a typed-array element no longer records the call as an escape of n. A plain const n = 5 already behaved that way. It looks fine to me, since a primitive receiver is not an object reference, but I only checked that one shape.
What the PR says about speed and memory
The body gives single runs on macOS arm64 (Node 24.18.0). The pdf.js CLI run goes from 824 s to 103 s. Peak memory across the process family goes from 3.30 GiB to 1.25 GiB. I did not re-run any of it. As far as I can tell the new tests check the behaviour the change keeps (which reads are pruned and which are not). They do not check speed.
Not run
- Any speed or memory measurement, and the pdf.js file itself.
- The rest of the JavaScript domain suite,
npm run checkand lint. - This PR on top of current main. The JavaScript domain folder has not changed on main since a80575c.
- Method calls through a getter on
Number.prototype. - macOS, and the CLI path.
Thanks, galleonli, for the quick follow-up fix and the clear write-up.
Test results
| Node | Result |
|---|---|
| 24.18.0 | javascriptSemanticAnalysis.typed_array_references.test.ts: 64 passed, plus 17 of my probe tests |
| 22.23.1 | same: 64 passed, 17 probe tests passed |
| 26.11.1 | same: 64 passed, 17 probe tests passed |
On 2fcfab5 the same file had 10 failures. All were in the group references reached through typed-array element properties. 13 of my probe cases failed there too.
Test setup
Commands
npm ci --ignore-scripts; npm run build:cached; npm run test:prepare (Node 24, tree at f75b17fda)
npm run test:focused -- src/domain/javascript/javascriptSemanticAnalysis.typed_array_references.test.ts src/domain/javascript/ev5139Probe.test.ts (Node 22, 24, 26)
git diff --shortstat a80575c73 f75b17fda
git diff --shortstat a80575c73 974487f00 -- src/domain/javascript
The probe uses the PR test file's own helper and reads the escaped paths of one named variable. It checks the finding's snippet, the 11 other reads listed above, and three controls (the element itself, a view read through heap[index >> 2], and an alias) that must still be pruned. To compare, I swapped in the member-mutations source from 2fcfab5 and from a80575c and ran the same file.
🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊
Keep this branch's typed-array and typed-array-element mutation origins beside main's new function origin. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged latest What changed and why
Validation (local macOS, merged head)
|
|
TL;DR: I retested head 1e075c2, which merges main into the branch. The merge is clean. The PR's change is the same patch as on f75b17f. kaoru0822-kitauji fixed the one conflict as the note says. The PR's 64 tests, 34 related test files and the typecheck pass, and the earlier Codex P1 case still keeps its escape. FindingsI tested head 1e075c2 on main a00e4ac. I compared the PR's change before and after the merge, then ran the tests.
Test results, same on every Node:
The 1,054 matches the count kaoru0822-kitauji gave on macOS. The Codex P1 thread (link) is still the only review thread. It is outdated and has no reply. Its case does not reproduce on 1e075c2. The bot's snippet, No new bot thread has appeared since f75b17f. Codex reported that its own review of 1e075c2 failed to run, so it has no findings on this head. Not run
Test setup
CommandsThe probe uses the PR test file's own helper and reads the escaped paths of one named variable. It checks the finding's snippet and 12 other reads that must keep the escape, and 3 controls (the element itself, a view read through 🤖 Written and tested with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊 |
Summary
Addresses #2498, following the typed-array approach suggested in this comment.
The slow path here comes from treating typed-array elements as possible object references. This patch lets the mutation collector rule those out. On the reported pdf.js file, the CLI went from 824 seconds to 103 seconds, with byte-identical JSON output. Sampled peak memory across the process family went from 3.30 GiB to 1.25 GiB.
Those are single runs on macOS arm64 with Node 24.18.0, a 4 GiB worker heap and a 900-second timeout.
With the default 1 GiB heap and 300-second deadline, the base timed out with a partial observation. The patch finished in 114 seconds with the same analysis content as its larger-budget run.
Problem and expected behavior
For a store like
H[e >> 2] = b, the collector follows the possible values assigned tob. In wasm2js output, many are reads from a typed array, so it keeps tracing references through the heap even though these values cannot be objects.A numeric indexed read from a native typed array returns a number, bigint, or
undefined. The element cannot carry an object reference. Writes to the view and escapes of the view itself still need to be tracked.Change and scope
Add a typed-array origin to the existing mutation analysis. Use the existing global-identity checks to recognize native constructors, and stop reference propagation for reads with a proven Number key or canonical numeric string key. Keep ordinary objects in mixed receiver sets, and keep the existing behavior for custom or unknown properties.
Keep a separate origin for the primitive element itself. Reading a property from that value can still expose a reference:
heap[0].constructor, for example, can returnNumberorBigInt. Those property reads stay conservative, including dynamic properties and reads through aliases. This fixes the missing-reference case raised in review without tracing references through scalar heap stores again.The actual property key stays part of the projection identity, so different slots in an ordinary array stay separate. Effect sites, timing and analysis budgets are unchanged.
Constructor aliases and general numeric-value inference are outside this change.
Contract and boundary impact
observableMutationPaths, before member effects are propagated.Expected effects of the exercised workflow:
Evidence and regression coverage
The base revision is
a80575c73652d8b43d7340654a48bd20a4eb966d. The initial 54 cases cover native constructors, numeric and custom keys, aliases, reassignment, mixed object receivers, constructor changes, eval, proxies, distinct ordinary-array slots, and retained view writes/escapes. On the base revision, 29 fail and 25 pass. Another 10 cases cover references reached through Number/BigInt element properties; all 10 failed on the first PR commit,2fcfab5. All 64 pass with the current patch.The full internal semantic IR is byte-identical, with complete coverage and no omissions. The public CLI output is also byte-identical, but its graph remains partial under the existing per-file caps. Finishing the command does not remove those limits.
Reproduction and measurement details
The fixture is
pdfjs-dist@5.3.93,package/wasm/openjpeg_nowasm_fallback.js, 462,316 bytes, SHA-25653c228428a8861035a34738966af263fc9d4cee6df25a0ad942af947469a0bd9.The unrestricted internal semantic IR is 32,160,539 bytes. That run fell from 385.8 to 49.6 seconds; peak process RSS fell from 3.37 to 0.88 GiB.
The CLI comparison used the extracted wasm directory as
fixture_dir:node scripts/rea.mjs analyze-javascript-application "$fixture_dir" \ --source-path openjpeg_nowasm_fallback.js \ --max-heap-mb 4096 --analysis-timeout-ms 900000 --jsonBoth CLI outputs are 52,499,954 bytes, SHA-256
eaa210ede669a57492de9bc424e336def71049ebf5583b80df5b3c73bef913fd. Their semantic graphs remain partial under the unchanged default per-file caps: 20,000 nodes, 32,675 relations, and 20,000 unknowns retained. Completed CLI execution does not mean complete graph coverage.CLI CPU time was 871.81 seconds before and 110.20 seconds after. Process-family memory was sampled by summing descendant
psRSS every 200 ms; shared pages may be counted more than once and short peaks may be missed. The internal IR measurements use processmaxRSS, a different accounting method. Each comparison used fresh processes on the same host with no concurrent test or build job. The patched runs were repeated after the property-reference fix; the baseline measurements are unchanged.At default resource limits, the base returned
provider_timeout. The patched analysis matches its larger-budget run; only the recorded heap/timeout parameters and the derived Evidence ID differ. This checks completion under the defaults, not a speedup against a completed baseline.Validation performed
npm run check: typecheck, compilation, lint, formatting, knip, metadata, and architecture guards passed.npm run verify:test-discovery: all 1,597 test files discovered exactly once on the first PR commit; the follow-up adds cases to the same test file.git diff --check: passed.Compatibility, safety, and release
No public API, dependency, migration or target-execution change. The full repository suite, other operating systems and MCP transport were not tested. The timings above apply to this input and machine.
Author readiness
Human owner: @galleonli
Before merge
type(scope): outcome.