- From: juj <notifications@github.com>
- Date: Tue, 21 Jul 2020 03:42:45 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1057@github.com>
Looking at the BYOB reading example at https://streams.spec.whatwg.org/#example-manual-read-bytes , the code looks awkward because of the large number of JS garbage that is generated. High performance animation-heavy WebAssembly applications strive to operate garbage-free, because JS GC pressure is known to generate microstuttering in WebGL rendering/animation. Would it be possible to create an API that would enable garbage-free streaming into a(n) (Shared)ArrayBuffer? The BYOB example on the page looks like ```js const reader = readableStream.getReader({ mode: "byob" }); let startingAB = new ArrayBuffer(1024); readInto(startingAB) .then(buffer => console.log("The first 1024 bytes:", buffer)) .catch(e => console.error("Something went wrong!", e)); function readInto(buffer, offset = 0) { if (offset === buffer.byteLength) { return Promise.resolve(buffer); } const view = new Uint8Array(buffer, offset, buffer.byteLength - offset); // (* garbage *) return reader.read(view).then(newView => { // ( * also garbage? *) return readInto(newView.buffer, offset + newView.byteLength); }); } ``` The stream avoids extra copy, but generates garbage. Comparing to a non-BYOB variant for a fetch: ```js function downloadFile(url) { return fetch(url) .then(response => { return [response.body, new Uint8Array(response.headers.get('Content-Length'))]; }).then(body => { var reader = body[0].getReader(); var buf = body[1]; var totalDownloaded = 0; function onChunkDownloaded(data) { if (data.value) { buf.set(data.value, totalDownloaded); // (* excess memcpy *) totalDownloaded += data.value.length; } if (data.done) { return buf; } else { return reader.read().then(onChunkDownloaded); } } return reader.read().then(onChunkDownloaded); }); } downloadFile('myLargeFile.dat').then(buf => { console.log('Downloaded file:'); console.dir(buf); }); ``` This code does not generate excessive typed array views, but it has an extra memory copy. I wonder if there would be a way to get to a JS GC free + zero copy way of downloading files? -- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/whatwg/streams/issues/1057
Received on Tuesday, 21 July 2020 10:42:58 UTC