Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade fixed queue, lint accordingly, add jsdoc #3577

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 91 additions & 49 deletions lib/dispatcher/fixed-queue.js
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -17,18 +15,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
Expand All @@ -40,12 +38,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
Expand All @@ -56,62 +54,106 @@ 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() {
this.bottom = 0;
this.top = 0;
this.list = new Array(kSize);
this.next = null;
constructor () {
/**
* @type {number}
*/
this.bottom = 0
/**
* @type {number}
*/
this.top = 0
/**
* @type {Array<T|undefined>}
*/
this.list = new Array(kSize).fill(undefined)
/**
* @type {T|null}
*/
this.next = null
}

isEmpty() {
return this.top === this.bottom;
/**
* @returns {boolean}
*/
isEmpty () {
return this.top === this.bottom
}

isFull() {
return ((this.top + 1) & kMask) === this.bottom;
/**
* @returns {boolean}
*/
isFull () {
return ((this.top + 1) & kMask) === this.bottom
}

push(data) {
this.list[this.top] = data;
this.top = (this.top + 1) & kMask;
/**
* @param {T} data
* @returns {void}
*/
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;
/**
* @returns {T|null}
*/
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
}
}

/**
* @template T
*/
module.exports = class FixedQueue {
constructor() {
this.head = this.tail = new FixedCircularBuffer();
constructor () {
/**
* @type {FixedCircularBuffer<T>}
*/
this.head = this.tail = new FixedCircularBuffer()
}

isEmpty() {
return this.head.isEmpty();
/**
* @returns {boolean}
*/
isEmpty () {
return this.head.isEmpty()
}

push(data) {
/**
* @param {T} 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();
/**
* @returns {T|null}
*/
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;
this.tail = tail.next
tail.next = null
}
return next;
return next
}
};
}
Loading