Closed
Description
Reading file contents into user supplied array avoids unnecessary copies. For example you can just get a pointer to an area in a WASM memory and directly read the file instead of first reading it into an useless ArrayBuffer and then doing a slow copy:
// file is a File or Blob
// wasm is a WebAssembly module instance wrapper with some additional methods like malloc added
const reader = new FileReader();
const ptr = wasm.malloc(file.size);
const array = new Uint8Array(wasm.memory.buffer, ptr, file.size);
try {
let bytesRead = await reader.readIntoUint8Array(file, array);
// Done
} catch (e) {
// Error
}
It also allows reading files while using minimal memory and allocations:
const reader = new FileReader();
const bufferPtr = wasm.malloc(8192);
const buffer = new Uint8Array(wasm.memory.buffer, bufferPtr, 8192);
let totalBytesRead = 0;
while (totalBytesRead < file.size) {
const start = totalBytesRead;
const end = Math.min(start + 8192, file.size);
const bytesRead = await readIntoUint8Array(file.slice(start, end), buffer);
wasm.process(bufferPtr, bytesRead);
totalBytesRead += bytesRead;
}
Doing the same with current APIs would allocate a ton of small ArrayBuffers...