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

lib: optimize priority queue #57100

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gurgunday
Copy link
Contributor

Huge credit to @lemire, I only ported the parts that we can to improve performance without introducing breaking changes

First run:

After:

util/priority-queue.js

util/priority-queue.js n=100000: 6,372,311.7447058195

Before:

util/priority-queue.js

util/priority-queue.js n=100000: 5,610,982.713123359

Second run:

After:

util/priority-queue.js

util/priority-queue.js n=100000: 6,524,784.786498602

Before:

util/priority-queue.js

util/priority-queue.js n=100000: 4,854,606.242159507

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Feb 17, 2025
@gurgunday gurgunday changed the title Optimize priority lib: optimize percolate and remove operations Feb 17, 2025
@gurgunday gurgunday changed the title lib: optimize percolate and remove operations lib: optimize priority queue Feb 17, 2025
Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.26%. Comparing base (fd45383) to head (db55a71).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #57100   +/-   ##
=======================================
  Coverage   90.26%   90.26%           
=======================================
  Files         630      630           
  Lines      184634   184645   +11     
  Branches    36137    36126   -11     
=======================================
+ Hits       166654   166675   +21     
+ Misses      11022    11017    -5     
+ Partials     6958     6953    -5     
Files with missing lines Coverage Δ
lib/internal/priority_queue.js 99.19% <100.00%> (+0.07%) ⬆️

... and 44 files with indirect coverage changes

@targos targos added the needs-benchmark-ci PR that need a benchmark CI run. label Feb 17, 2025
@gurgunday
Copy link
Contributor Author

@aduh95 would you like to take a look as well?

heap[pos] = item;
if (setPosition !== undefined)
setPosition(item, pos);
}

removeAt(pos) {
if (pos > this.#size) return;
Copy link
Contributor

@aduh95 aduh95 Feb 23, 2025

Choose a reason for hiding this comment

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

Wouldn't an assert makes more sense here? We probably have a bug if we try to remove something out of range

Suggested change
if (pos > this.#size) return;
assert(pos < this.#size);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aduh95 actually I checked again and it was going to be a breaking change this one, because of this:

removeAt(pos) {
const heap = this.#heap;
const size = --this.#size;
heap[pos] = heap[size + 1];
heap[size + 1] = undefined;
if (size > 0 && pos <= size) {
if (pos > 1 && this.#compare(heap[pos / 2 | 0], heap[pos]) > 0)
this.percolateUp(pos);
else
this.percolateDown(pos);
}
}

We'd do the mutation directly but wouldn't enter the if block, now we don't do anything, so I reverted that completely

@gurgunday gurgunday requested a review from aduh95 February 23, 2025 12:38
@aduh95
Copy link
Contributor

aduh95 commented Feb 23, 2025

Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1660/

                                confidence improvement accuracy (*)   (**)  (***)
util/priority-queue.js n=100000        ***      7.48 %       ±1.29% ±1.72% ±2.23%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 1 comparisons, you can thus
expect the following amount of false-positive results:
  0.05 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.01 false positives, when considering a   1% risk acceptance (**, ***),
  0.00 false positives, when considering a 0.1% risk acceptance (***)

@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Feb 23, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 23, 2025
@nodejs-github-bot
Copy link
Collaborator

@gurgunday
Copy link
Contributor Author

@aduh95 can we try again? I rebased maybe there was a flaky test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants