Skip to content

Commit 6843a59

Browse files
jasnelladuh95
authored andcommitted
stream: read stream/iter share() consumers without a chain per read
Every next() of a share() consumer chained on the consumer's previous next() and ran an async function, also when the batch was already buffered, as it is for every consumer but the first to read it. Every read of the source ran an async function, and each consumer waiting for it queued a promise of its own. Recomputing the slowest consumer's cursor, once per batch with several consumers, allocated an object. A next() that finds its batch buffered, or the end, now settles at once, without waiting for anything. Only a next() that waits for the source is waited for by the next one; when there is room in the buffer it waits for the read with one reaction, without an async function. Consumers waiting for the same read of the source share one promise, which a cancellation resolves at once, and the read is handled by functions created once per share. getMinCursor() reuses its result. With two consumers of a share of 16-byte chunks one per batch, reading a sync generator is about 1.8 times faster, and an async generator about 1.7 times faster, with 60% less allocated per chunk. 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 a9ad2cd commit 6843a59

2 files changed

Lines changed: 227 additions & 150 deletions

File tree

0 commit comments

Comments
 (0)