Skip to content

perf: start with 16 MiB of memory, so that most sources don't grow it - #516

Open
snovader wants to merge 1 commit into
nodejs:mainfrom
snovader:initial-memory
Open

snovader wants to merge 1 commit into
nodejs:mainfrom
snovader:initial-memory

Conversation

@snovader

@snovader snovader commented Nov 21, 2026 •

Copy link
Copy Markdown

Stripping a TypeScript source of more than 1 to 4 KB grows amaro's WebAssembly memory. Growing detaches the memory's ArrayBuffer, which invalidates V8's ArrayBufferDetaching protector. Up to Node.js 26, any detach of a buffer that was read invalidates it. In 27, only a detach of a buffer with a DataView or with two views does, and the wasm-bindgen bindings hold a Uint8Array and a DataView of the memory's. From then on, optimized code checks every typed-array and DataView access for a detached buffer, in every module of that V8 isolate, for the rest of its life. In Node.js 26.13.0, a loop that sums a Float64Array took 4 times as long after a 20 KB file had been stripped. nodejs/node#67632 has the details.

This starts the memory at 256 pages (16 MiB) instead of 24, with a linker flag. Rust's allocator uses the initial memory before it grows the memory, so no code changes.

Results

Node.js 22.23.5, 24.22.3, 26.13.0 and 27.0.0-alpha, Apple M2 Pro, built with wasm-pack on macOS (not in the builder container).

  • In all four versions, the source that I tested left the memory at 256 pages up to 384 KB, in strip-only mode, in transform mode, and in transform mode with a source map, and --trace-protector-invalidation printed nothing. At 512 KB it grew the memory to 265 pages and invalidated the protector, as every source over a few KB does today.
  • The size that fits depends on the source and on the options. Of three other shapes of source in the three modes, in 26.13.0, all left the memory at 256 pages up to 256 KB, and three of the nine grew it at 320 KB (a as T; repeated: to 319 pages in strip-only mode, and to 333 with a source map).
  • Load time, with the first strip, and resident memory after loading didn't change (7.7 ms and 58 MB in 26.13.0): the pages aren't touched until they are used.
  • Stripping took as long as before for sources of 1 KB to 384 KB, and of 1 MB to 4 MB (within 1–2%, as did two runs of main).
  • A 512 KB source took as long as before in a new process, and 13 to 19% longer in a process that had stripped smaller sources many times (13.7 against 11.4 ms in 26.13.0, 15.5 against 13.1 ms in 24.22.3). The protector is then invalidated after V8 has optimized the bindings, where it used to be invalidated before they ran much. With the protector invalidated at startup, the same strip took 11.4 ms, as on main.
  • npm test passes in all four versions.

Limits

16 MiB is a threshold, not a fix: a source too large for it still grows the memory. The size is a judgment call, and I'm happy to change it.

16 MiB also becomes the least memory that the module loads with. Under a --wasm-max-mem-pages below 256, V8 refuses to instantiate it (RangeError at require), where main loads with 24 pages or more and fails only once a source needs more than the cap.

For sources of any size, the bindings can call WebAssembly.Memory.prototype.toResizableBuffer() where the engine has it, so that growing resizes the buffer in place. That needs a declared maximum and a line added by tools/patch-wasm.mjs. It is on a branch, on top of this change, if you'd like it as a follow-up:

  • In 26.13.0 and 27.0.0-alpha, sources of up to 4 MB printed no invalidation, and the 512 KB source above took 11.7 ms.
  • Stripping sources of 1 KB to 64 KB took 2 to 5% longer in those versions (64 KB: 1470 against 1405 µs in 26.13.0), because V8's access to a resizable buffer is slower.
  • 22.23.5 and 24.22.3 don't have the method, so nothing changes there, in behavior or in time.

Refs: nodejs/node#67632

@marco-ippolito

Copy link
Copy Markdown
Member

ptal @kdy1

@kdy1

kdy1 commented Nov 21, 2026

Copy link
Copy Markdown
Member

Will do right away

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agents: Claude Opus 5.5 (claude-opus-5-5), review subagent GPT-6.2 Astra (gpt-6.2-astra), review subagent GPT-6.2 Sol (gpt-6.2-sol), review subagent Claude Opus 5.5 (claude-opus-5-5), review subagent Claude Sonnet 5.5 (claude-sonnet-5-5)

Thanks for the detailed write-up. I built the base and the head and compared them. The flag works as described. I have three inline comments and one more point below. I think the first inline comment (the new 16 MiB minimum) needs a decision before we merge, so I'm leaving this review as discussion points.

What I checked

  • I built both sides locally with the pinned toolchain (Rust 1.98.1, wasm-bindgen 0.2.100), not in the builder container. The memory section is min: 24 on main and min: 256 on this PR. Neither side has a maximum.

  • Node.js 24.22.1 (with spot checks on 26.13.1), one transformSync call in a new process:

    Input main This PR
    1 KiB – 384 KiB of typed declarations, strip-only and transform 24 → 25–237 pages, protector invalidated stays at 256 pages, no invalidation
    300 mixed calls (1–384 KiB) in one process grows, invalidated stays at 256 pages
    2 MiB grows, invalidated grows, invalidated
  • Load time and RSS after load did not change (about 56 MB on both sides).

  • npm test passes on the head build (126/126).

  • tools/config.toml does take effect. wasm-pack runs cargo in crates/amaro, so --config ../../tools/config.toml resolves. Neither wasm-pack nor the wasm-builder image sets RUSTFLAGS, so the target.*.rustflags setting is not overridden.

Regression test (not in the diff)

No test checks the initial memory size. If a later build sets RUSTFLAGS, or stops passing --config ../../tools/config.toml, the module goes back to 24 pages. The build and all tests still pass in that case. A small test would catch this, for example test/wasm-memory.test.js:

const { test } = require("node:test");
const assert = require("node:assert");
const { __wasm } = require("../lib/wasm.js");

test("wasm memory starts at 16 MiB", () => {
	assert.ok(__wasm.memory.buffer.byteLength >= 16 * 1024 * 1024);
});

This test passed on the head build and failed on the main build.

I did not reproduce the 13–19% slowdown for a 512 KB source in a warm process. I did not test Node.js 22 or 27, Windows, macOS, or 32-bit platforms.

Comment thread tools/config.toml Resolved
Comment thread tools/config.toml Resolved
Comment thread tools/config.toml Resolved
@snovader

Copy link
Copy Markdown
Author

On the regression test: added as test/wasm-memory.test.js, with a second test that a 64 KB source leaves the memory's size unchanged. Both fail on a build of main.

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agents: Claude Opus 5.5 (claude-opus-5-5), review subagent GPT-6.2 Astra (gpt-6.2-astra), review subagent GPT-6.2 Sol (gpt-6.2-sol), review subagent Claude Opus 5.5 (claude-opus-5-5), review subagent Claude Sonnet 5.5 (claude-sonnet-5-5)

Thanks for the updates. The size figures and the isolate wording now match what I measured, and both new tests fail on a main build and pass on this branch.

I accept the 16 MiB load floor under --wasm-max-mem-pages that is documented in tools/config.toml.

I have two small inline comments.

Comment thread tools/config.toml Resolved
Comment thread test/wasm-memory.test.js Resolved

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agents: Claude Opus 5.5 (claude-opus-5-5), review subagent GPT-6.2 Astra (gpt-6.2-astra), review subagent GPT-6.2 Sol (gpt-6.2-sol), review subagent Claude Opus 5.5 (claude-opus-5-5), review subagent Claude Sonnet 5.5 (claude-sonnet-5-5)

Thanks for the updates. LGTM.

I checked the current head (d54f41a) against main:

  • With the CI builds of both sides, sources of 1–256 KiB no longer grow the memory or invalidate the ArrayBufferDetaching protector, on Node.js 24 and 26. Larger sources still grow it, as the comment in tools/config.toml says.
  • Both tests in test/wasm-memory.test.js fail on a main build and pass on this branch. npm test passes (128/128).
  • The 16 MiB load floor under --wasm-max-mem-pages behaves as documented: a RangeError at 255 pages, and the module loads at 256.

I have one optional wording suggestion inline.

Comment thread tools/config.toml Resolved
@marco-ippolito

Copy link
Copy Markdown
Member

Needs a rebase

Growing the memory detaches its ArrayBuffer, which invalidates V8's
ArrayBufferDetaching protector. Up to Node.js 26, any detach of a buffer
that was read invalidates it. In 27, only a detach of a buffer with a
DataView or with two views does, and the bindings hold a Uint8Array and
a DataView of the memory's. Optimized code then checks every typed-array
and DataView access for a detached buffer, in that V8 isolate, for the
rest of its life. In Node.js 26.13.0, a loop that sums a Float64Array
took 4 times as long after a 20 KB file had been stripped.

The memory started at 24 pages (1.5 MiB) and grew for any source of more
than 1 to 4 KB. Rust's allocator uses the initial memory before it grows
the memory, so a linker flag is enough to give it more.

With 256 pages, the source that I tested left the memory at its initial
size up to 384 KB in strip-only mode, in transform mode, and in
transform mode with a source map, and V8 printed no invalidation with
--trace-protector-invalidation, in Node.js 22.23.5, 24.22.3, 26.13.0 and
27.0.0-alpha. At 512 KB it grew the memory to 265 pages. The size that
fits depends on the source and on the options: of three other shapes of
source, all left the memory at 256 pages up to 256 KB in every mode, and
some grew it at 320 KB.

Load time and resident memory didn't change, nor did the time to strip
sources of 1 KB to 384 KB and of 1 MB to 4 MB. A 512 KB source took as
long as before in a new process, and 13 to 19% longer (13.7 against
11.4 ms in 26.13.0) in a process that had stripped smaller sources many
times. The protector is then invalidated after V8 has optimized the
bindings, where it used to be invalidated before; with the protector
invalidated at startup, the same strip took 11.4 ms.

16 MiB also becomes the least memory that the module loads with: V8
refuses to instantiate it under a lower --wasm-max-mem-pages than 256.

A build that doesn't read tools/config.toml drops the linker flag
without failing, so test/wasm-memory.test.js checks that the memory
starts at exactly 16 MiB, and that a 64 KB source doesn't grow it.

Refs: nodejs/node#67632
Signed-off-by: snovader <marcus.ording@hotmail.com>
@snovader

Copy link
Copy Markdown
Author

Squashed into one commit, 1f30991

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.

3 participants