Skip to content

Commit 62ff98d

Browse files
jasnelladuh95
authored andcommitted
stream: use a RingBuffer for the stream/iter operation queue
The hand-written stream/iter iterators queue calls made while another one is in progress, as async generators do. They were queued in an array taken off the front with ArrayPrototypeShift(), which copies the rest of the queue, so calling next() many times without waiting was quadratic: 160,000 concurrent next() calls on a from() iterator took 7 seconds. Use a RingBuffer, kept once created, of QueuedOperation objects. The same case takes 330 ms. 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 fd13964 commit 62ff98d

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

‎lib/internal/streams/iter/utils.js‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
ArrayBufferPrototypeGetDetached,
77
ArrayBufferPrototypeGetResizable,
88
ArrayPrototypePush,
9-
ArrayPrototypeShift,
109
ArrayPrototypeSlice,
1110
FunctionPrototypeCall,
1211
ObjectDefineProperty,
@@ -39,6 +38,7 @@ const {
3938

4039
const { isSharedArrayBuffer, isUint8Array } = require('internal/util/types');
4140
const { kWeakHandler } = require('internal/event_target');
41+
const { RingBuffer } = require('internal/streams/iter/ringbuffer');
4242

4343
const {
4444
validateInteger,
@@ -473,6 +473,15 @@ const kStart = 0;
473473
const kActive = 1;
474474
const kDone = 2;
475475

476+
class QueuedOperation {
477+
constructor(method, arg, resolve, reject) {
478+
this.method = method;
479+
this.arg = arg;
480+
this.resolve = resolve;
481+
this.reject = reject;
482+
}
483+
}
484+
476485
/**
477486
* Serializes the operations of a hand-written async iterator the way an
478487
* async generator queues its requests: an operation started while another
@@ -481,13 +490,13 @@ const kDone = 2;
481490
*/
482491
function createOperationQueue() {
483492
let busy = false;
493+
// Created on first use, then kept: queued operations, a RingBuffer of
494+
// QueuedOperation, since as many can be queued as calls are made.
484495
let queue = null;
485496

486497
function drain() {
487-
if (busy || queue === null) return;
488-
const { 0: method, 1: arg, 2: resolve, 3: reject } =
489-
ArrayPrototypeShift(queue);
490-
if (queue.length === 0) queue = null;
498+
if (busy || queue === null || queue.length === 0) return;
499+
const { method, arg, resolve, reject } = queue.shift();
491500
busy = true;
492501
PromisePrototypeThen(method(arg), resolve, reject);
493502
}
@@ -497,18 +506,20 @@ function createOperationQueue() {
497506
// Run `method(arg)`, which returns a promise and must call settled()
498507
// once its result is known, now or after the operations before it.
499508
run(method, arg) {
500-
if (busy || queue !== null) {
509+
if (busy || (queue !== null && queue.length !== 0)) {
501510
const { promise, resolve, reject } = PromiseWithResolvers();
502-
queue ??= [];
503-
ArrayPrototypePush(queue, [method, arg, resolve, reject]);
511+
queue ??= new RingBuffer();
512+
queue.push(new QueuedOperation(method, arg, resolve, reject));
504513
return promise;
505514
}
506515
busy = true;
507516
return method(arg);
508517
},
509518
settled() {
510519
busy = false;
511-
if (queue !== null) PromisePrototypeThen(kResolvedPromise, drain);
520+
if (queue !== null && queue.length !== 0) {
521+
PromisePrototypeThen(kResolvedPromise, drain);
522+
}
512523
},
513524
// Like settled(), but start the next queued operation now, as an async
514525
// generator does once the await of a yield or return completes.

0 commit comments

Comments
 (0)