Skip to content

fix: replace denque with a local ring buffer queue - #4472

Merged
wellwelwel merged 6 commits into
sidorares:masterfrom
wellwelwel:denque
Aug 10, 2026
Merged

wellwelwel merged 6 commits into
sidorares:masterfrom
wellwelwel:denque

Conversation

@wellwelwel

@wellwelwel wellwelwel commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

This PR replaces the denque dependency with a small in-repo ring buffer queue.

The last denque release dates from July 2022, and the repository was archived almost five months ago (March 2026), so the package can no longer maintained or receive security fixes:

Screenshot 2026-08-09 at 07 33 53

Closes #740.


New Dependency vs. Self-Implementation

A queue is a relatively simple structure and the driver relies on a small subset of a general-purpose deque, so implementing exactly that behavior was preferred over searching for a replacement dependency.


Benchmarks ✨

Workload (driver pattern) denque ring Speedup
Command queue at depth 0-1 1.670 s ± 0.006 s 1.403 s ± 0.013 s 1.19x ± 0.01
Pipelined commands (8 deep) 2.254 s ± 0.005 s 2.025 s ± 0.017 s 1.11x ± 0.01
Pool free list (LIFO) 1.877 s ± 0.025 s 1.458 s ± 0.009 s 1.29x ± 0.02
Wait queue burst (10k waiters) 2.471 s ± 0.025 s 2.357 s ± 0.004 s 1.05x ± 0.01
Find and remove a pooled connection 1.968 s ± 0.005 s 1.441 s ± 0.018 s 1.37x ± 0.02

No workload regressed.

  • Node v26.6.0 on an Apple M2 Ultra
  • hyperfine 1.20.0
Reproducing

From the repository root, on this branch:

  1. Install the reference implementation: npm i --no-save denque
  2. Save the script below as bench-ring-queue.js
  3. Run each workload:
for workload in cmd-queue-0-1 cmd-queue-pipeline-8 pool-free-lifo wait-burst-10k scan-remove-100; do
  hyperfine -N --warmup 3 -L impl denque,ring "node bench-ring-queue.js {impl} $workload"
done
'use strict';

const implName = process.argv[2];
const workloadName = process.argv[3];

if (implName !== 'denque' && implName !== 'ring') {
  throw new Error(`unknown impl: ${implName}`);
}

const Queue =
  implName === 'denque' ? require('denque') : require('./lib/ring_queue.js');

const ITERATIONS = {
  'cmd-queue-0-1': 400_000_000,
  'cmd-queue-pipeline-8': 50_000_000,
  'pool-free-lifo': 400_000_000,
  'wait-burst-10k': 20_000,
  'scan-remove-100': 10_000_000,
};

function cmdQueue01(total) {
  const queue = new Queue();
  const item = { id: 1 };
  let sum = 0;

  for (let i = 0; i < total; i++) {
    queue.push(item);
    sum += queue.shift().id;
  }

  return sum;
}

function cmdQueuePipeline8(total) {
  const queue = new Queue();
  const items = [];
  for (let i = 0; i < 8; i++) items.push({ id: i + 1 });
  let sum = 0;

  for (let i = 0; i < total; i++) {
    for (let j = 0; j < 8; j++) queue.push(items[j]);
    for (let j = 0; j < 8; j++) sum += queue.shift().id;
  }

  return sum;
}

function poolFreeLifo(total) {
  const queue = new Queue();
  const item = { id: 1 };
  let sum = 0;

  for (let i = 0; i < total; i++) {
    queue.push(item);
    sum += queue.length;
    sum += queue.pop().id;
  }

  return sum;
}

function waitBurst10k(total) {
  const queue = new Queue();
  const batch = [];
  for (let i = 0; i < 10000; i++) batch.push({ id: i });
  let sum = 0;

  for (let i = 0; i < total; i++) {
    for (let j = 0; j < 10000; j++) queue.push(batch[j]);
    for (let j = 0; j < 10000; j++) sum += queue.shift().id;
  }

  return sum;
}

function scanRemove100(total) {
  const queue = new Queue();
  const items = [];
  for (let i = 0; i < 100; i++) items.push({ id: i });
  for (let i = 0; i < 100; i++) queue.push(items[i]);
  let cursor = 0;
  let sum = 0;

  for (let i = 0; i < total; i++) {
    cursor = (cursor * 31 + 17) % 100;
    const target = items[cursor];
    let index = 0;
    while (queue.get(index) !== target) index++;
    sum += queue.removeOne(index).id;
    queue.push(target);
  }

  return sum;
}

const workloads = {
  'cmd-queue-0-1': cmdQueue01,
  'cmd-queue-pipeline-8': cmdQueuePipeline8,
  'pool-free-lifo': poolFreeLifo,
  'wait-burst-10k': waitBurst10k,
  'scan-remove-100': scanRemove100,
};

if (!workloads[workloadName]) {
  throw new Error(`unknown workload: ${workloadName}`);
}

console.log(workloads[workloadName](ITERATIONS[workloadName]));

cc @sidorares

@wellwelwel wellwelwel added security refactor dependencies Pull requests that update a dependency file labels Aug 9, 2026
@wellwelwel
wellwelwel requested a balanced review from Copilot August 9, 2026 11:51
@codecov

codecov Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.74648% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.08%. Comparing base (361d232) to head (d875775).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
lib/ring_queue.js 97.73% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4472      +/-   ##
==========================================
+ Coverage   91.83%   92.08%   +0.25%     
==========================================
  Files          91       92       +1     
  Lines       14860    15213     +353     
  Branches     1978     2084     +106     
==========================================
+ Hits        13646    14009     +363     
+ Misses       1214     1204      -10     
Flag Coverage Δ
compression-0 91.64% <97.74%> (+0.26%) ⬆️
compression-1 92.06% <97.74%> (+0.25%) ⬆️
static-parser-0 90.84% <97.74%> (+0.28%) ⬆️
static-parser-1 91.11% <97.74%> (+0.27%) ⬆️
tls-0 91.63% <97.74%> (+0.26%) ⬆️
tls-1 92.08% <97.74%> (+0.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Replaces the archived denque dependency with an internal ring-buffer queue used by connection and pool internals.

Changes:

  • Adds a ring-buffer queue implementation.
  • Migrates connection and pool queues to it.
  • Removes denque and adds comprehensive unit tests.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/ring_queue.js Implements the internal queue.
lib/base/connection.js Uses the local queue for commands and packets.
lib/base/pool.js Uses the local queue for pool bookkeeping.
test/unit/test-ring-queue.test.mts Tests ordering, resizing, removal, and randomized operations.
package.json Removes the denque dependency.
package-lock.json Removes the locked denque package.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@wellwelwel
wellwelwel marked this pull request as ready for review August 9, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Comment thread lib/ring_queue.js Resolved
@wellwelwel

Copy link
Copy Markdown
Collaborator Author

@sidorares, if you think it makes sense to use a distinct package for this responsibility, we can migrate as well.

I didn't find any lib that reproduces the behavior in an equivalent or faithful way to the archived denque (neither in terms of performance nor functionality). I also tested the solution proposed in #1629, but it corrupted the data in some tests.

For now, I'll keep it this way (local) 🙋🏻‍♂️

@wellwelwel
wellwelwel merged commit 88f3d9e into sidorares:master Aug 10, 2026
104 checks passed
@wellwelwel
wellwelwel deleted the denque branch August 10, 2026 03:29
@sidorares

Copy link
Copy Markdown
Owner

I think its small enough to inline. If we have a good replacement we can migrate again later to use external dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file refactor security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

queue.removeOne is not a function

3 participants