lib: optimize priority queue

PR-URL: https://github.com/nodejs/node/pull/60039
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Gürgün Dayıoğlu 2025-10-04 22:08:00 +02:00 committed by GitHub
parent e6cd8a21e7
commit e978a63b01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,5 @@
'use strict'; 'use strict';
const {
Array,
} = primordials;
// The PriorityQueue is a basic implementation of a binary heap that accepts // The PriorityQueue is a basic implementation of a binary heap that accepts
// a custom sorting function via its constructor. This function is passed // a custom sorting function via its constructor. This function is passed
// the two nodes to compare, similar to the native Array#sort. Crucially // the two nodes to compare, similar to the native Array#sort. Crucially
@ -12,7 +8,7 @@ const {
module.exports = class PriorityQueue { module.exports = class PriorityQueue {
#compare = (a, b) => a - b; #compare = (a, b) => a - b;
#heap = new Array(64); #heap = [undefined, undefined];
#setPosition; #setPosition;
#size = 0; #size = 0;
@ -28,9 +24,6 @@ module.exports = class PriorityQueue {
const pos = ++this.#size; const pos = ++this.#size;
heap[pos] = value; heap[pos] = value;
if (heap.length === pos)
heap.length *= 2;
this.percolateUp(pos); this.percolateUp(pos);
} }
@ -45,6 +38,7 @@ module.exports = class PriorityQueue {
percolateDown(pos) { percolateDown(pos) {
const compare = this.#compare; const compare = this.#compare;
const setPosition = this.#setPosition; const setPosition = this.#setPosition;
const hasSetPosition = setPosition !== undefined;
const heap = this.#heap; const heap = this.#heap;
const size = this.#size; const size = this.#size;
const hsize = size >> 1; const hsize = size >> 1;
@ -62,7 +56,7 @@ module.exports = class PriorityQueue {
if (compare(item, childItem) <= 0) break; if (compare(item, childItem) <= 0) break;
if (setPosition !== undefined) if (hasSetPosition)
setPosition(childItem, pos); setPosition(childItem, pos);
heap[pos] = childItem; heap[pos] = childItem;
@ -70,7 +64,7 @@ module.exports = class PriorityQueue {
} }
heap[pos] = item; heap[pos] = item;
if (setPosition !== undefined) if (hasSetPosition)
setPosition(item, pos); setPosition(item, pos);
} }
@ -78,6 +72,7 @@ module.exports = class PriorityQueue {
const heap = this.#heap; const heap = this.#heap;
const compare = this.#compare; const compare = this.#compare;
const setPosition = this.#setPosition; const setPosition = this.#setPosition;
const hasSetPosition = setPosition !== undefined;
const item = heap[pos]; const item = heap[pos];
while (pos > 1) { while (pos > 1) {
@ -86,13 +81,13 @@ module.exports = class PriorityQueue {
if (compare(parentItem, item) <= 0) if (compare(parentItem, item) <= 0)
break; break;
heap[pos] = parentItem; heap[pos] = parentItem;
if (setPosition !== undefined) if (hasSetPosition)
setPosition(parentItem, pos); setPosition(parentItem, pos);
pos = parent; pos = parent;
} }
heap[pos] = item; heap[pos] = item;
if (setPosition !== undefined) if (hasSetPosition)
setPosition(item, pos); setPosition(item, pos);
} }