Repository navigation
perf(vm): share identical source buffers across node:vm scripts - #190
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bb25784f9c
ℹ️ 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".
| }); | ||
|
|
||
| it("shares exact source buffers without sharing provider metadata", async () => { | ||
| const { bunEnv, bunExe } = await import("harness"); |
There was a problem hiding this comment.
Import the harness helpers at module scope
These tests are not testing dynamic import behavior, and harness is already imported at module scope, so dynamically importing bunEnv and bunExe here—and again in the following test—violates the test-suite requirement to use static imports for ordinary dependencies. Add both helpers to the existing top-level harness import instead.
AGENTS.md reference: test/AGENTS.md:L218-L220
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b8fca9afe5
ℹ️ 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".
| }); | ||
| }); | ||
|
|
||
| it("shares exact source buffers without sharing provider metadata", async () => { |
There was a problem hiding this comment.
Run the new subprocess tests concurrently
When this file runs, this test and the second new subprocess test at line 94 are serialized because both use plain it, even though they are independent and perform expensive memory/GC workloads in spawned Bun processes. Use it.concurrent (or group them under describe.concurrent) as required for multiple subprocess-spawning tests in one file.
AGENTS.md reference: test/AGENTS.md:L21-L22
Useful? React with 👍 / 👎.
Problem
Repeated
vm.Script/runInThisContextevaluations can retain separate copies of identical source text behind their functions. Inline source maps also produce separately retained URI strings. The 256 MiB compilation-cache budget bounds its own entries, not source buffers kept alive by executions after eviction.Design
Share immutable exact-content buffers through a per-VM weak index, independently of compilation-cache admission, cached-data options, and eviction. Providers and compilation-cache entries hold strong leases; the index holds only weak references. Hash, length, character width, and complete bytes must match. Substrings and externally owned buffers are copied into exact owned slices.
Each provider retains its own identity, origin, filename, offsets, directives, and dynamic-import referrer. The source-map URI is canonicalized in both the provider and parsed program block. Weak metadata is bounded to 2,048 buckets of at most 32 slots and pruned incrementally. Lease destruction is thread-safe and never calls back into the VM. Workers have independent indexes. Compilation-cache accounting remains conservative and unchanged.
Measurements
The earlier JavaScript input-sharing experiment reduced the 108-file gateway-server shard from 15.73 to 11.23 GiB final RSS and from 17.2 to 13.6 GiB peak, with the same results. Its paired 30-file slice fell from 5.822 to 4.345 GiB. Those figures describe the diagnostic experiment, not this implementation.
At
bb25784f9ced22f9f8212e816f7ccbb2d786d6c7, the native implementation reduces the same built-ins-only reduction, with the compilation cache disabled, from 185,315,328 to 67,534,848 bytes RSS. All 128 retained functions produce the same digest,02439e85a97300233c445410833a414b546ec819753d0c3376319913e4e77e3b.Twelve interleaved pairs for each of six unique-source compilation cases cover 32, 4,096, and 262,144 code units in Latin-1 and UTF-16. The largest measured throughput regression is 0.83%, below the 2% gate. The 4 KiB cases improve by 2.27% and 3.97%.
The paired 30-file application slice uses OpenClaw
2e5329f78072e17c0551ae37a40cb73ba24d2d6eon Linux x64, 64 GiB RAM, one worker, explicit GC, identical probes and file order, and no JavaScript interner. The isolated checkout’s expected Bun revision was adjusted identically for both custom binaries; test sources and limits were unchanged.Final RSS falls by 2,166,358,016 bytes (35.0%), from 5.766 to 3.748 GiB. The reported main-VM heap differs by only 1.87 MiB, while RSS minus heap falls from 4.015 to 1.996 GiB. The candidate is below the earlier 4.345 GiB JavaScript-interner reference; these are separate runs on different Bun revisions. Wall time is observational from one ordered pair, not an isolated throughput claim.
Both arms reproduce the same existing sessionless-media failure; all other 279 tests pass. The same failure was present in the earlier diagnostic. Peak figures are for the measured worker process.
Verification
test-vm-module-import-meta.jshas the same existing enumerableresolvemismatch on clean main. No skips or changed expectations were added.b8fca9afe5704ad39445d3fecca248e70735424b: Linux x64 and Darwin arm64. Source lints, formatting, and JavaScript lint also pass.No matching upstream source-buffer sharing fix was found in the targeted search.
Merged main
935bccfdbd8a54c348287d619ebb5a30c3f669e3with a normal two-parent merge, preserving both changelog entries and both documentation changes. The source-buffer implementation is unchanged. A fresh Linux release rebuild of the merged head passed the full VM directory (389 pass, 3 existing skips, 60 existing todos) andtest/cli/run/transpiler-cache.test.ts(32 pass), including the source-map disable-flag regressions. The merge resolution also passed Codex P2 review.