Skip to content

perf(vm): share identical source buffers across node:vm scripts - #190

Merged
steipete merged 2 commits into
mainfrom
claude/vm-source-buffer-sharing
Oct 17, 2026
Merged

steipete merged 2 commits into
mainfrom
claude/vm-source-buffer-sharing

Conversation

@steipete

@steipete steipete commented Oct 17, 2026 •

Copy link
Copy Markdown

Problem

Repeated vm.Script / runInThisContext evaluations 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 2e5329f78072e17c0551ae37a40cb73ba24d2d6e on 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.

Build Final RSS JSC heapSize RSS minus heap Peak worker RSS Wall time Tests
Current main 5.766 GiB 1.750 GiB 4.015 GiB 8.488 GiB 571.25 s 279 pass, 1 existing failure
Source sharing 3.748 GiB 1.752 GiB 1.996 GiB 6.704 GiB 520.65 s 279 pass, 1 existing failure

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

  • The retained-function RSS regression passes here and fails on the baseline specifically at the RSS bound: 142,798,848 bytes growth against 115,343,360 bytes allowed. Function text, digests, filenames, and stack positions are checked.
  • Native checks cover full-byte hash collisions, character widths, a 4 KiB slice of a 1 MiB parent, separate VM pools, destruction after pool teardown on another thread, index overflow, and expired metadata.
  • Identical large source-map URIs share one allocation even across distinct complete sources with the compilation cache disabled.
  • Source/cached-data/stack/toString probe output is byte-for-byte equal to the baseline. Eight Worker lifecycles retain and check 96 functions each.
  • VM suite: 389 passing tests, with its existing three skips and 60 todos. Source-map/stack suites: 91 passing tests and one existing todo.
  • Source lints: 226 passing tests. Formatting checks pass. Independent Codex P2 review is scoped-clean.
  • Worker suite: 285 passing tests. Upstream parallel VM/source-map coverage: 100 files executed, 99 pass; test-vm-module-import-meta.js has the same existing enumerable resolve mismatch on clean main. No skips or changed expectations were added.
  • Native CI is green on 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 935bccfdbd8a54c348287d619ebb5a30c3f669e3 with 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) and test/cli/run/transpiler-cache.test.ts (32 pass), including the source-map disable-flag regressions. The merge resolution also passed Codex P2 review.

@steipete
steipete marked this pull request as ready for review October 17, 2026 12:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T12:54:05.586226Z b8fca9a New commits
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@steipete
steipete merged commit 7bd4fdf into main Oct 17, 2026
6 checks passed
@steipete
steipete deleted the claude/vm-source-buffer-sharing branch October 17, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant