- From: Petka Antonov <notifications@github.com>
- Date: Mon, 19 Jun 2017 13:41:52 -0700
- To: w3c/FileAPI <FileAPI@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/FileAPI/issues/83@github.com>
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: ```js // 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: ```js 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... -- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/w3c/FileAPI/issues/83
Received on Monday, 19 June 2017 20:42:33 UTC