GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: use a RingBuffer for the stream/iter operation queue · nodejs/node@f177d0f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams/iter
    • utils.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/streams/iter/utils.js
+20-9Lines 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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.