From 466ab08890601ab8f8489b8273761cdc65f3725a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 10 Sep 2024 02:38:22 +0200 Subject: [PATCH 1/3] upgrade fixed-queue --- lib/dispatcher/fixed-queue.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/dispatcher/fixed-queue.js b/lib/dispatcher/fixed-queue.js index 35726819e92..9c9d7271cbb 100644 --- a/lib/dispatcher/fixed-queue.js +++ b/lib/dispatcher/fixed-queue.js @@ -17,18 +17,18 @@ const kMask = kSize - 1; // +-----------+ <-----\ +-----------+ <------\ +-----------+ // | [null] | \----- | next | \------- | next | // +-----------+ +-----------+ +-----------+ -// | item | <-- bottom | item | <-- bottom | [empty] | -// | item | | item | | [empty] | -// | item | | item | | [empty] | -// | item | | item | | [empty] | +// | item | <-- bottom | item | <-- bottom | undefined | +// | item | | item | | undefined | +// | item | | item | | undefined | +// | item | | item | | undefined | // | item | | item | bottom --> | item | // | item | | item | | item | // | ... | | ... | | ... | // | item | | item | | item | // | item | | item | | item | -// | [empty] | <-- top | item | | item | -// | [empty] | | item | | item | -// | [empty] | | [empty] | <-- top top --> | [empty] | +// | undefined | <-- top | item | | item | +// | undefined | | item | | item | +// | undefined | | undefined | <-- top top --> | undefined | // +-----------+ +-----------+ +-----------+ // // Or, if there is only one circular buffer, it looks something @@ -40,12 +40,12 @@ const kMask = kSize - 1; // +-----------+ +-----------+ // | [null] | | [null] | // +-----------+ +-----------+ -// | [empty] | | item | -// | [empty] | | item | -// | item | <-- bottom top --> | [empty] | -// | item | | [empty] | -// | [empty] | <-- top bottom --> | item | -// | [empty] | | item | +// | undefined | | item | +// | undefined | | item | +// | item | <-- bottom top --> | undefined | +// | item | | undefined | +// | undefined | <-- top bottom --> | item | +// | undefined | | item | // +-----------+ +-----------+ // // Adding a value means moving `top` forward by one, removing means @@ -60,7 +60,7 @@ class FixedCircularBuffer { constructor() { this.bottom = 0; this.top = 0; - this.list = new Array(kSize); + this.list = new Array(kSize).fill(undefined); this.next = null; } @@ -111,6 +111,7 @@ module.exports = class FixedQueue { if (tail.isEmpty() && tail.next !== null) { // If there is another queue, it forms the new tail. this.tail = tail.next; + tail.next = null; } return next; } From 5bddab64610c167b664df80449f1d400fcf9945e Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 10 Sep 2024 02:38:45 +0200 Subject: [PATCH 2/3] lint --- lib/dispatcher/fixed-queue.js | 71 +++++++++++++++++------------------ 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/lib/dispatcher/fixed-queue.js b/lib/dispatcher/fixed-queue.js index 9c9d7271cbb..df7131c57dc 100644 --- a/lib/dispatcher/fixed-queue.js +++ b/lib/dispatcher/fixed-queue.js @@ -1,12 +1,10 @@ -/* eslint-disable */ - 'use strict' // Extracted from node/lib/internal/fixed_queue.js // Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two. -const kSize = 2048; -const kMask = kSize - 1; +const kSize = 2048 +const kMask = kSize - 1 // The FixedQueue is implemented as a singly-linked list of fixed-size // circular buffers. It looks something like this: @@ -57,62 +55,61 @@ const kMask = kSize - 1; // but allows much quicker checks. class FixedCircularBuffer { - constructor() { - this.bottom = 0; - this.top = 0; - this.list = new Array(kSize).fill(undefined); - this.next = null; + constructor () { + this.bottom = 0 + this.top = 0 + this.list = new Array(kSize).fill(undefined) + this.next = null } - isEmpty() { - return this.top === this.bottom; + isEmpty () { + return this.top === this.bottom } - isFull() { - return ((this.top + 1) & kMask) === this.bottom; + isFull () { + return ((this.top + 1) & kMask) === this.bottom } - push(data) { - this.list[this.top] = data; - this.top = (this.top + 1) & kMask; + push (data) { + this.list[this.top] = data + this.top = (this.top + 1) & kMask } - shift() { - const nextItem = this.list[this.bottom]; - if (nextItem === undefined) - return null; - this.list[this.bottom] = undefined; - this.bottom = (this.bottom + 1) & kMask; - return nextItem; + shift () { + const nextItem = this.list[this.bottom] + if (nextItem === undefined) { return null } + this.list[this.bottom] = undefined + this.bottom = (this.bottom + 1) & kMask + return nextItem } } module.exports = class FixedQueue { - constructor() { - this.head = this.tail = new FixedCircularBuffer(); + constructor () { + this.head = this.tail = new FixedCircularBuffer() } - isEmpty() { - return this.head.isEmpty(); + isEmpty () { + return this.head.isEmpty() } - push(data) { + push (data) { if (this.head.isFull()) { // Head is full: Creates a new queue, sets the old queue's `.next` to it, // and sets it as the new main queue. - this.head = this.head.next = new FixedCircularBuffer(); + this.head = this.head.next = new FixedCircularBuffer() } - this.head.push(data); + this.head.push(data) } - shift() { - const tail = this.tail; - const next = tail.shift(); + shift () { + const tail = this.tail + const next = tail.shift() if (tail.isEmpty() && tail.next !== null) { // If there is another queue, it forms the new tail. - this.tail = tail.next; - tail.next = null; + this.tail = tail.next + tail.next = null } - return next; + return next } -}; +} From 530f0610607578f9338e2c89495067c91efeb709 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 10 Sep 2024 02:59:37 +0200 Subject: [PATCH 3/3] add jsdoc --- lib/dispatcher/fixed-queue.js | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/dispatcher/fixed-queue.js b/lib/dispatcher/fixed-queue.js index df7131c57dc..5f7a08bc47f 100644 --- a/lib/dispatcher/fixed-queue.js +++ b/lib/dispatcher/fixed-queue.js @@ -54,27 +54,56 @@ const kMask = kSize - 1 // `top + 1 === bottom` it's full. This wastes a single space of storage // but allows much quicker checks. +/** + * @type {FixedCircularBuffer} + * @template T + */ class FixedCircularBuffer { constructor () { + /** + * @type {number} + */ this.bottom = 0 + /** + * @type {number} + */ this.top = 0 + /** + * @type {Array} + */ this.list = new Array(kSize).fill(undefined) + /** + * @type {T|null} + */ this.next = null } + /** + * @returns {boolean} + */ isEmpty () { return this.top === this.bottom } + /** + * @returns {boolean} + */ isFull () { return ((this.top + 1) & kMask) === this.bottom } + /** + * @param {T} data + * @returns {void} + */ push (data) { this.list[this.top] = data this.top = (this.top + 1) & kMask } + /** + * @returns {T|null} + */ shift () { const nextItem = this.list[this.bottom] if (nextItem === undefined) { return null } @@ -84,15 +113,27 @@ class FixedCircularBuffer { } } +/** + * @template T + */ module.exports = class FixedQueue { constructor () { + /** + * @type {FixedCircularBuffer} + */ this.head = this.tail = new FixedCircularBuffer() } + /** + * @returns {boolean} + */ isEmpty () { return this.head.isEmpty() } + /** + * @param {T} data + */ push (data) { if (this.head.isFull()) { // Head is full: Creates a new queue, sets the old queue's `.next` to it, @@ -102,6 +143,9 @@ module.exports = class FixedQueue { this.head.push(data) } + /** + * @returns {T|null} + */ shift () { const tail = this.tail const next = tail.shift()