stream: speed up async iteration of Readable · nodejs/node@5edffb5 · GitHub
Skip to content

Commit 5edffb5

Browse files
mcollinaaduh95
authored andcommitted
stream: speed up async iteration of Readable
Replace the async generator backing Symbol.asyncIterator with a hand-rolled iterator. The generator machinery costs several extra promise allocations and microtask hops per chunk: yield awaits the yielded value and resolves the pending request through separate promises. Buffered chunks are now delivered as an already-resolved promise, one microtask sooner than before. Thenable chunks are still awaited before delivery, requests received while a next() is outstanding are queued, and return()/throw() before the first next() complete the iterator without touching the stream. The earlier delivery is observable by code racing an abort against the first chunk. The flatMap AbortSignal test relied on such a race; it is reworked to abort deterministically while two mappers are in flight, asserting the concurrency limit, in-flight cancellation and rejection, without depending on delivery timing or timers. streams/readable-async-iterator.js sync='yes': +32.59% (***) streams/readable-async-iterator.js sync='no': +9.84% (***) Assisted-by: Claude Fable 5 Signed-off-by: Matteo Collina <matteo.collina@gmail.com> PR-URL: #64447 Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 6302168 commit 5edffb5

3 files changed

Lines changed: 351 additions & 35 deletions

File tree

lib/internal/streams/readable.js

Lines changed: 215 additions & 28 deletions

test/parallel/test-stream-flatMap.js

Lines changed: 16 additions & 7 deletions

0 commit comments

Comments
 (0)