Last active
August 29, 2015 14:19
-
-
Save domenic/dab9e8da245cb4fe6027 to your computer and use it in GitHub Desktop.
Envisioned use of BYOB reader to reuse buffers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run processFn on each chunk, while only ever allocating maxChunkSize * 2 | |
// memory, and never causing GC (by reusing the same backing buffers) | |
// Is able to do a read in parallel with the async processFn operation! | |
async function processPingPong(rbs, maxChunkSize, processFn) { | |
const reader = rbs.getByobReader(); | |
const pool = [new Uint8Array(maxChunkSize), new Uint8Array(maxChunkSize)]; | |
let i = 0; | |
let toProcess; | |
await doRead(0); | |
while (!toProcess.done) { | |
await Promise.all([ | |
doProcess(i % 2), | |
doRead((i + 1) % 2) | |
]); | |
++i; | |
} | |
function doProcess(i) { | |
const view = toProcess.value; | |
await processFn(view); | |
pool[i] = view; | |
} | |
function doRead(i) { | |
toProcess = await reader.read(pool[i]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run processFn on each chunk, while only ever allocating maxChunkSize | |
// memory, and never causing GC (by reusing the same backing buffer) | |
async function processChunkwise(rbs, maxChunkSize, processFn) { | |
const reader = rbs.getByobReader(); | |
let currentView = new Uint8Array(maxChunkSize); | |
let result; | |
while (!(result = await reader.read(currentView)).done) { | |
// calling reader.read detaches currentView, but reuses its backing buffer | |
// now result.value uses the same backing buffer and is not detached | |
// result.value.byteLength contains the number of bytes read | |
// (which might be less than maxChunkSize) | |
await processChunk(result.value); | |
// reuse the same backing buffer for the next iteration | |
currentView = result.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment