-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
timers: use only a single TimerWrap instance #20555
Changes from all commits
3449f04
71140ed
c75a6b2
ccd3506
5a0f49e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6] | ||
}, { flags: ['--expose-internals'] }); | ||
|
||
function main({ n, type }) { | ||
const PriorityQueue = require('internal/priority_queue'); | ||
const queue = new PriorityQueue(); | ||
bench.start(); | ||
for (var i = 0; i < n; i++) | ||
queue.insert(Math.random() * 1e7 | 0); | ||
for (i = 0; i < n; i++) | ||
queue.shift(); | ||
bench.end(n); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
|
||
const kCompare = Symbol('compare'); | ||
const kHeap = Symbol('heap'); | ||
const kSetPosition = Symbol('setPosition'); | ||
const kSize = Symbol('size'); | ||
|
||
// The PriorityQueue is a basic implementation of a binary heap that accepts | ||
// a custom sorting function via its constructor. This function is passed | ||
// the two nodes to compare, similar to the native Array#sort. Crucially | ||
// this enables priority queues that are based on a comparison of more than | ||
// just a single criteria. | ||
|
||
module.exports = class PriorityQueue { | ||
constructor(comparator, setPosition) { | ||
if (comparator !== undefined) | ||
this[kCompare] = comparator; | ||
if (setPosition !== undefined) | ||
this[kSetPosition] = setPosition; | ||
|
||
this[kHeap] = new Array(64); | ||
this[kSize] = 0; | ||
} | ||
|
||
[kCompare](a, b) { | ||
return a - b; | ||
} | ||
|
||
insert(value) { | ||
const heap = this[kHeap]; | ||
let pos = ++this[kSize]; | ||
|
||
if (heap.length === pos) | ||
heap.length *= 2; | ||
|
||
const compare = this[kCompare]; | ||
const setPosition = this[kSetPosition]; | ||
while (pos > 1) { | ||
const parent = heap[pos / 2 | 0]; | ||
if (compare(parent, value) <= 0) | ||
break; | ||
heap[pos] = parent; | ||
if (setPosition !== undefined) | ||
setPosition(parent, pos); | ||
pos = pos / 2 | 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new position is actually calculated twice. I guess it does not change much but it could be better to cache the entry above and to set it down here. |
||
} | ||
heap[pos] = value; | ||
if (setPosition !== undefined) | ||
setPosition(value, pos); | ||
} | ||
|
||
peek() { | ||
return this[kHeap][1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes the math slightly more complicated to start binary heaps at 0, so it's unused. |
||
} | ||
|
||
percolateDown(pos) { | ||
const compare = this[kCompare]; | ||
const setPosition = this[kSetPosition]; | ||
const heap = this[kHeap]; | ||
const size = this[kSize]; | ||
const item = heap[pos]; | ||
|
||
while (pos * 2 <= size) { | ||
let childIndex = pos * 2 + 1; | ||
if (childIndex > size || compare(heap[pos * 2], heap[childIndex]) < 0) | ||
childIndex = pos * 2; | ||
const child = heap[childIndex]; | ||
if (compare(item, child) <= 0) | ||
break; | ||
if (setPosition !== undefined) | ||
setPosition(child, pos); | ||
heap[pos] = child; | ||
pos = childIndex; | ||
} | ||
heap[pos] = item; | ||
if (setPosition !== undefined) | ||
setPosition(item, pos); | ||
} | ||
|
||
removeAt(pos) { | ||
const heap = this[kHeap]; | ||
const size = --this[kSize]; | ||
heap[pos] = heap[size + 1]; | ||
heap[size + 1] = undefined; | ||
|
||
if (size > 0) | ||
this.percolateDown(1); | ||
} | ||
|
||
remove(value) { | ||
const heap = this[kHeap]; | ||
const pos = heap.indexOf(value); | ||
if (pos < 1) | ||
return false; | ||
|
||
this.removeAt(pos); | ||
|
||
return true; | ||
} | ||
|
||
shift() { | ||
const heap = this[kHeap]; | ||
const value = heap[1]; | ||
if (value === undefined) | ||
return; | ||
|
||
this.removeAt(1); | ||
|
||
return value; | ||
} | ||
}; |
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong.