Skip to content

Type stripping invalidates V8's ArrayBufferDetaching protector, slowing typed-array code for the rest of the process #67632

Open

Description

@snovader

Version

v26.13.0 (V8 14.6.202.34-node.40, amaro 1.1.11) and v27.0.0-alpha (V8 15.6.75.25-node.10, amaro 1.1.11)

Platform

Darwin arm64 (Apple M2 Pro)

Subsystem

module, typescript (deps: amaro)

What steps will reproduce the bug?

Stripping the types of a TypeScript source of more than about 2 KB invalidates V8's ArrayBufferDetaching protector. Running a .ts file does it, and so does module.stripTypeScriptTypes():

// strip.mjs
import { stripTypeScriptTypes } from 'node:module';

const count = Number(process.argv[2]);
let source = '';
for (let i = 0; i < count; i++) {
    source += `export function f${i}(a: number, b: number): number { return a + b; }\n`;
}

stripTypeScriptTypes(source);
console.log(`stripped ${source.length} bytes`);
$ node --no-warnings --trace-protector-invalidation strip.mjs 20
stripped 1350 bytes
$ node --no-warnings --trace-protector-invalidation strip.mjs 30
Invalidating protector cell ArrayBufferDetaching
stripped 2030 bytes

A 2.7 KB .ts entry point prints the same line, a 670-byte one doesn't, and the 2.7 KB file with its types removed by hand and saved as .mjs doesn't either.

Once the protector is invalid, optimized code checks typed-array accesses for detached buffers, in every module, until the process exits:

// repro.mjs
// node repro.mjs          protector intact
// node repro.mjs strip    strips 20 KB of TypeScript first
import { stripTypeScriptTypes } from 'node:module';

if (process.argv[2] === 'strip') {
    let source = '';
    for (let i = 0; i < 300; i++) {
        source += `export function f${i}(a: number, b: number): number { return a + b; }\n`;
    }
    stripTypeScriptTypes(source);
}

const values = new Float64Array(1000).map((_, i) => i);

function sum() {
    let total = 0;
    for (let i = 0; i < values.length; i++) total += values[i];
    return total;
}

let sink = 0;
for (let i = 0; i < 200_000; i++) sink += sum();

let best = Infinity;
for (let round = 0; round < 20; round++) {
    const start = process.hrtime.bigint();
    for (let i = 0; i < 100_000; i++) sink += sum();
    best = Math.min(best, Number(process.hrtime.bigint() - start) / 100_000);
}

console.log(`${process.version} ${process.argv[2] ?? 'intact'}: ${best.toFixed(0)} ns per sum`);

Fastest of 20 rounds, three processes each, on an M2 Pro:

Node intact after stripping ratio
26.13.0 1438–1456 ns 5753–5796 ns 4.0×
27.0.0-alpha 782–795 ns 854–860 ns 1.08×

In Node 27 this loop loses less, but the protector is still invalidated, and larger code loses more: a binary format library's DataView getters, loaded from .ts, took about 1.6× as long in both versions as when a module.registerHooks load hook stripped the same files with a native transform. Having amaro strip a 300-function string at startup, with that hook still in place, brought the slowdown back.

How often does it reproduce? Is there a required condition?

Always, once one stripped source is large enough to grow amaro's WebAssembly memory. Most real .ts files are.

What is the expected behavior? Why is that the expected behavior?

Running a program from .ts should leave typed-array code as fast as running the same program from .js. Type stripping is stable and on by default, so a process pays this whenever it loads a TypeScript file, whether or not it ever detaches a buffer itself.

What do you see instead?

Invalidating protector cell ArrayBufferDetaching, and slower typed-array and DataView code for the rest of the process.

Additional information

Cause. amaro is swc compiled to WebAssembly. When its memory grows, V8 detaches the ArrayBuffer that memory.buffer returned before. amaro's wasm-bindgen glue holds two views of that buffer (deps/amaro/dist/internal.js):

cachedDataViewMemory0 = new DataView(wasm.memory.buffer)
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)

Why V8 15 doesn't fix it. V8 now tracks a buffer's view, so that detaching can mark that view instead of invalidating the protector (crbug 467645277, a08ad9c, on by default since 2031231, V8 15.x). It handles a buffer with no view or with one TypedArray. A buffer with two views, or with a DataView, still invalidates the protector (JSArrayBuffer::TryDetachViews), and amaro's memory has both. With views.mjs below:

// node --trace-protector-invalidation views.mjs <none|one|two|dataview>
const mode = process.argv[2];
const memory = new WebAssembly.Memory({ initial: 1 });
const views = [];
if (mode === 'none') memory.buffer;
if (mode === 'one' || mode === 'two') views.push(new Uint8Array(memory.buffer));
if (mode === 'two') views.push(new Uint8Array(memory.buffer));
if (mode === 'dataview') views.push(new DataView(memory.buffer));
memory.grow(1);
none one two dataview
Node 26.13.0 invalidated invalidated invalidated invalidated
Node 26.13.0 --track-array-buffer-views intact intact invalidated invalidated
Node 27.0.0-alpha intact intact invalidated invalidated

Possible fixes, none of which I have tried in amaro itself:

  • Build amaro's WebAssembly module with enough initial memory that stripping ordinary files never grows it. This would help every release line, including those whose V8 doesn't track views.
  • Have the glue take its views from memory.toResizableBuffer(), which memory.grow() resizes instead of detaching. In both versions above, growing such a memory with a Uint8Array and a DataView on it left the protector intact.
  • Have the glue hold a single Uint8Array and no DataView. This helps only where V8 tracks views (15.x).

I haven't run Node 24, or main with amaro 1.3.0, whose glue still creates both views.

Other built-in paths invalidate the protector in Node 26.13.0: reading a fetch or Response body, constructing a Blob from a typed array, enqueuing into a byte stream, and ArrayBuffer.prototype.transfer(). In Node 27.0.0-alpha none of them does, so type stripping is the one built-in path I found that still invalidates it there. It also differs in kind: those are things a program does, while type stripping happens before the program's first line runs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions