mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
stream: use a RingBuffer for stream/iter merge()'s ready queue
merge() queued every batch from its sources in an array, taking each off the front with ArrayPrototypeShift(), which copies the rest of the queue: up to one entry per source. Use a RingBuffer. Merging async generators yielding 16-byte chunks, one per batch, is about 6% faster with 2 sources, 9% with 8 and 36% with 64. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
1 parent
3eac066a5f
commit
9c6caaf0da
1 file changed
+5
-6
@@ -14,7 +14,6 @@ const {
|
||||
ArrayBufferPrototypeSlice,
|
||||
ArrayPrototypeMap,
|
||||
ArrayPrototypePush,
|
||||
ArrayPrototypeShift,
|
||||
ArrayPrototypeSlice,
|
||||
FunctionPrototypeCall,
|
||||
ObjectFreeze,
|
||||
@@ -39,6 +38,7 @@ const {
|
||||
},
|
||||
} = require('internal/errors');
|
||||
const { TextDecoder } = require('internal/encoding');
|
||||
const { RingBuffer } = require('internal/streams/iter/ringbuffer');
|
||||
const {
|
||||
validateFunction,
|
||||
} = require('internal/validators');
|
||||
@@ -516,7 +516,7 @@ function merge(...args) {
|
||||
// between consumer pulls are drained synchronously without an extra
|
||||
// async tick per batch. Each source has at most one pending .next()
|
||||
// at a time. Every batch from every source is preserved.
|
||||
const ready = [];
|
||||
const ready = new RingBuffer();
|
||||
const pendingPulls = new SafeSet();
|
||||
let activeCount = normalized.length;
|
||||
let waitResolve = null;
|
||||
@@ -541,8 +541,7 @@ function merge(...args) {
|
||||
if (result.done) {
|
||||
activeCount--;
|
||||
} else {
|
||||
ArrayPrototypePush(ready,
|
||||
new MergeEntry(iterator, result.value, undefined));
|
||||
ready.push(new MergeEntry(iterator, result.value, undefined));
|
||||
}
|
||||
if (waitResolve) {
|
||||
waitResolve();
|
||||
@@ -553,7 +552,7 @@ function merge(...args) {
|
||||
const onRejected = (iterator, reason) => {
|
||||
pendingPulls.delete(iterator);
|
||||
if (stopped) return;
|
||||
ArrayPrototypePush(ready, new MergeEntry(undefined, undefined, reason));
|
||||
ready.push(new MergeEntry(undefined, undefined, reason));
|
||||
if (waitResolve) {
|
||||
waitResolve();
|
||||
waitResolve = null;
|
||||
@@ -580,7 +579,7 @@ function merge(...args) {
|
||||
|
||||
// Drain ready queue synchronously
|
||||
while (ready.length > 0) {
|
||||
const item = ArrayPrototypeShift(ready);
|
||||
const item = ready.shift();
|
||||
if (item.iterator === undefined) {
|
||||
throw item.reason;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user