Re: [whatwg/streams] Why does the `read` method of `ReadableStreamBYOBReader` class, need to _transfer_ the buffer it is provided? (Issue #1348)

eqrion left a comment (whatwg/streams#1348)

@amn The Wasm/JS specifications explicitly forbid a non-shared array buffer's contents from being mutated concurrently to the execution of the Wasm/JS code. If we didn't detach the ArrayBuffer when passing it to the host, and the host starts writing to the ArrayBuffer from a different thread while Wasm/JS is running, it will see results that violate the languages memory models for non-shared memory.

The reason this is important for 'safety' is that JS JITs and Web API's can make assumption that rely on this memory model. For example a JS JIT can reorder accesses to non-shared memory more aggressively than if it knows another thread may be modifying it. And a Web API (like for example wasm compilation) that takes a non-shared array buffer can read it's contents directly without a copy and not worry that it will be mutated from underneath it.

None of that applies to shared array buffers, which opt-in to the non-determinism. I definitely support loosening this restriction for shared array buffers, and think that would be a great way to reduce copies using this API.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1348#issuecomment-3133952133
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/issues/1348/3133952133@github.com>

Received on Tuesday, 29 July 2025 20:26:35 UTC