Allow pre-allocating array buffer for .arrayBuffer(), to allow zero-copy loads
We have a binary format that we would like to parse with WebAssembly. But the problem is that we have to copy the file's contents to WebAssembly.Memory before doing anything. Which is slow.
So currently the code would look roughly like this:
const response = await fetch("file.bin");
const fileBytes = new Uint8Array(await response.arrayBuffer());
const wasmMemory = new WebAssembly.Memory({
initial: pagesCountRequredForWasmModule(fileBytes.length)
});
const wasmMemoryBytes = new Uint8Array(wasmMemory);
wasmMemoryBytes.set(fileBytes);
const {instance} = await WebAssembly.instantiateStreaming(fetch(wasmUrl), { env: { memory: wasmMemory } });
instance.exports.parse();
But it could be more like:
const response = await fetch("file.bin");
const wasmMemory = new WebAssembly.Memory({
initial: pagesCountRequredForWasmModule(response.headers.get('Content-Length'))
});
await response.preAllocatedArrayBuffer(wasmMemory.buffer, offset);
const {instance} = await WebAssembly.instantiateStreaming(fetch(wasmUrl), { env: { memory: wasmMemory } });
instance.exports.parse();
The same approach would probably help with WebGPU's mapped memory.
Do BYOB streams not help here?
I suppose this could be similar to https://encoding.spec.whatwg.org/#dom-textencoder-encodeinto. We cannot rely on Content-Length being present so we need to be able to deal with overflow. Perhaps we can allow repeated calls, though that would be a bit of a departure from existing Body mixin methods.
Related: #897.
Do BYOB streams not help here?
Never heard of it before! But it looks exactly like what I need. My previous experience with streams was rather negative, though. So I guess we'll try this approach, and continue from there.
P.S. BYOB is a really cryptic name for that tbh. I would never suspect this to be what I want while scanning through the docs.