Skip to content

Commit 17299c9

Browse files
jasnelladuh95
authored andcommitted
stream: normalize sync sources without an async generator in from()
from() normalized a sync iterable source with an async generator, which costs several promises and an async frame for every batch, even though the source is read synchronously. Read the source with a sync generator instead, which collects chunks into batches as before, and normalize the values that need it, such as promises, in an async iterator written out by hand around it. The sync generator's for...of reads and closes the source as before: return() and throw() are passed to it, after closing the value being normalized, and an error normalizing a value is thrown into it, closing the source as for an error in the loop body. As an async generator does, the iterator stays busy until the tick after a result, so that calls made synchronously after one are queued behind it. With 16-byte chunks, one per batch, this allocates about 150 bytes less per chunk; iterating from() is about 15% faster and pipeTo() about 11% faster. Sources of single chunks, which are batched, are unchanged. The queue of operations is now shared with the iterator for async sources, and uses ArrayPrototypeShift(). New tests cover batching, errors, closing and queuing for sync sources. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 767b184 commit 17299c9

2 files changed

Lines changed: 377 additions & 90 deletions

File tree

0 commit comments

Comments
 (0)