diff --git a/test/wasm-memory.test.js b/test/wasm-memory.test.js new file mode 100644 index 000000000..fbefd6c82 --- /dev/null +++ b/test/wasm-memory.test.js @@ -0,0 +1,20 @@ +const { test } = require("node:test"); +const assert = require("node:assert"); +const { __wasm, transformSync } = require("../lib/wasm.js"); + +const INITIAL_MEMORY_BYTES = 16 * 1024 * 1024; + +// tools/config.toml sets the initial memory with a linker flag, which a build +// that doesn't read that file drops without failing. +test("should start with 16 MiB of memory", () => { + assert.strictEqual(__wasm.memory.buffer.byteLength, INITIAL_MEMORY_BYTES); +}); + +test("should not grow the memory for a 64 KB source", () => { + const source = "const a: number = 1;\n".repeat(3200); + const before = __wasm.memory.buffer.byteLength; + + transformSync(source, { mode: "strip-only" }); + + assert.strictEqual(__wasm.memory.buffer.byteLength, before); +}); diff --git a/tools/config.toml b/tools/config.toml index eba19d85b..49d8d50a6 100644 --- a/tools/config.toml +++ b/tools/config.toml @@ -1,3 +1,21 @@ # Override swc's config for alpine linux build image. [target.aarch64-unknown-linux-musl] linker = "aarch64-alpine-linux-musl-gcc" + +# Growing a WebAssembly memory detaches its ArrayBuffer, which invalidates +# V8's ArrayBufferDetaching protector, and that slows typed-array code in that +# V8 isolate for the rest of its life (nodejs/node#67632). 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 both. +# +# The allocator uses the initial memory before it grows the memory. With +# 16 MiB of it, most sources don't grow the memory. The size limit depends on +# the source and on the transform options: sources of up to 256 KB didn't grow +# it in any mode, and some of 320 KB did. +# +# 16 MiB is also the least memory that the module loads with: V8 refuses to +# instantiate it under a lower `--wasm-max-mem-pages` than 256. +# +# test/wasm-memory.test.js checks that the build has applied this. +[target.wasm32-unknown-unknown] +rustflags = ["-C", "link-arg=--initial-memory=16777216"]