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

Performant queue #51

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/queue.js
@@ -0,0 +1,37 @@
'use strict';

class Queue {
constructor() {
this.elements = [];
this.offset = 0;
}

push(...element) {
this.elements.push(...element);
return this;
}

shift() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to use enqueue/dequeue methods and under the hood add elements with array.unshift, and remove elements with offset/slice. Need to check performance and compare

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach was to match current array methods (push/shift) to minimize refactoring, but I agree that enqueue/dequeue could be more convinient.

Copy link
Author

@JaoodxD JaoodxD Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here is test/queue-performance.js performance comparison test to compare queues of 100k elements. It shows ~25x execution time difference. This factor is growing with queue size e.g. ~800x at 500k elements.

if (this.length === 0) return null;
const first = this.elements[this.offset];
this.offset++;

if (this.offset * 2 < this.elements.length) return first;

this.elements = this.elements.slice(this.offset);
this.offset = 0;
return first;
}

get length() {
return this.elements.length - this.offset;
}

*[Symbol.iterator]() {
for (let i = this.offset; i < this.length; i++) {
yield this.elements[i];
}
}
}

module.exports = Queue;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -46,7 +46,7 @@
"email": "timur.shemsedinov@gmail.com"
},
"main": "web-locks.js",
"files": [],
"files": ["lib/queue.js"],
"engines": {
"node": ">=11.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions test.js
Expand Up @@ -8,6 +8,7 @@ const tests = [
'deadlock',
'recursive-deadlock',
'thread-main',
'queue-performance',
];

(async () => {
Expand Down
36 changes: 36 additions & 0 deletions test/queue-performance.js
@@ -0,0 +1,36 @@
'use strict';

const assert = require('node:assert/strict');
const { performance } = require('node:perf_hooks');
const Queue = require('../lib/queue.js');

const simpleQueueBench = (QueueClass) => {
const queue = new QueueClass();
const iterations = 100000;
let sum = 0;

const timeStart = performance.now();
for (let i = 0; i < iterations; i++) {
queue.push(() => i);
}
for (let i = 0; i < iterations; i++) {
sum += queue.shift()();
}
const timeEnd = performance.now();

const execTime = timeEnd - timeStart;
return { sum, execTime };
};

module.exports = async () => {
const testedClasses = [Array, Queue];

const [arrayResult, queueResult] = testedClasses.map(simpleQueueBench);
const { sum: arraySum, execTime: arrayExecTime } = arrayResult;
const { sum: queueSum, execTime: queueExecTime } = queueResult;

const isQueueFaster = arrayExecTime > queueExecTime;

assert.strictEqual(queueSum, arraySum);
assert.strictEqual(isQueueFaster, true);
};
7 changes: 4 additions & 3 deletions web-locks.js
@@ -1,6 +1,7 @@
'use strict';

const { EventEmitter } = require('events');
const Queue = require('./lib/queue.js');
const threads = require('worker_threads');
const { isMainThread, parentPort } = threads;
const isWorkerThread = !isMainThread;
Expand All @@ -14,7 +15,7 @@ class Lock {
constructor(name, mode = 'exclusive', buffer = null) {
this.name = name;
this.mode = mode; // 'exclusive' or 'shared'
this.queue = [];
this.queue = new Queue();
this.owner = false;
this.trying = false;
this.buffer = buffer ? buffer : new SharedArrayBuffer(4);
Expand Down Expand Up @@ -57,8 +58,8 @@ class Lock {

class LockManagerSnapshot {
constructor(resources) {
const held = [];
const pending = [];
const held = new Queue();
const pending = new Queue();
this.held = held;
this.pending = pending;

Expand Down