Skip to content

Add method to read file into user given Uint8Array #83

Description

@petkaantonov

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...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions