Skip to content

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

Open
@mixtur

Description

@mixtur

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions