-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
base: main
Are you sure you want to change the base?
lib: optimize priority queue #57100
Conversation
695af3e
to
8c8dc2a
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
|
@aduh95 would you like to take a look as well? |
lib/internal/priority_queue.js
Outdated
heap[pos] = item; | ||
if (setPosition !== undefined) | ||
setPosition(item, pos); | ||
} | ||
|
||
removeAt(pos) { | ||
if (pos > this.#size) return; |
There was a problem hiding this comment.
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
if (pos > this.#size) return; | |
assert(pos < this.#size); |
There was a problem hiding this comment.
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:
node/lib/internal/priority_queue.js
Lines 89 to 101 in 25dd206
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
Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1660/
|
61fed01
to
db55a71
Compare
@aduh95 can we try again? I rebased maybe there was a flaky test |
Huge credit to @lemire, I only ported the parts that we can to improve performance without introducing breaking changes
First run:
After:
Before:
Second run:
After:
Before: