fetch icon indicating copy to clipboard operation
fetch copied to clipboard

Allow pre-allocating array buffer for .arrayBuffer(), to allow zero-copy loads

Open mixtur opened this issue 2 years ago • 2 comments

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.

mixtur avatar Mar 06 '23 12:03 mixtur

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.

annevk avatar Mar 06 '23 13:03 annevk

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.

mixtur avatar Mar 07 '23 10:03 mixtur