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

Use Priority queue in chunk graph algorithm #8238

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Compilation.js
Expand Up @@ -31,7 +31,7 @@ const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialCh
const Stats = require("./Stats");
const Semaphore = require("./util/Semaphore");
const createHash = require("./util/createHash");
const Queue = require("./util/Queue");
const PriorityQueue = require("./util/PriorityQueue");
const SortableSet = require("./util/SortableSet");
const GraphHelpers = require("./GraphHelpers");
const ModuleDependency = require("./dependencies/ModuleDependency");
Expand Down Expand Up @@ -1782,8 +1782,9 @@ class Compilation extends Tapable {
let availableModules;
/** @type {Set<Module>} */
let newAvailableModules;
/** @type {Queue<AvailableModulesChunkGroupMapping>} */
const queue2 = new Queue(
/** @type {PriorityQueue<AvailableModulesChunkGroupMapping>} */
const queue2 = new PriorityQueue(
mapping => mapping.availableModules.size,
inputChunkGroups.map(chunkGroup => ({
chunkGroup,
availableModules: new Set(),
Expand Down
107 changes: 107 additions & 0 deletions lib/util/PriorityQueue.js
@@ -0,0 +1,107 @@
"use strict";

/**
* @template T
*/
class PriorityQueue {
/**
* @param {function(T):number} priorityFn The function to calculate the item's priority. Lower values will be pushed to the front of the queue
* @param {Iterable<T>=} items The initial elements.
*/
constructor(priorityFn, items) {
/** @type {{priority: number, item: T}[]} */
this.heap = [];
/** @type {function(T):number}*/
this.priorityFn = priorityFn;
for (let item of items) {
Copy link
Member

Choose a reason for hiding this comment

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

Since items is optional, you must use an if to guard the iteration

this.enqueue(item);
}
}

/**
* Returns the number of elements in this queue.
* @returns {number} The number of elements in this queue.
*/
get length() {
return this.heap.length;
}

/**
* Appends the specified element to this queue.
* @param {T} item The element to add.
* @returns {void}
*/
enqueue(item) {
this.heap.push({ priority: this.priorityFn(item), item });
this._siftUp();
}

/**
* Retrieves and removes the head of this queue.
* @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
*/
dequeue() {
const lastIndex = this.length - 1;
if (lastIndex > 0) {
this._swap(0, lastIndex);
}
const poppedValue = this.heap.pop();
this._siftDown();
return poppedValue.item;
Copy link
Member

Choose a reason for hiding this comment

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

Calling dequeue on an empty PriorityQueue would result in a TypeError. It would be better to change this line to:

Suggested change
return poppedValue.item;
return poppedValue ? poppedValue.item : undefined;

}

_getNodePriority(index) {
return this.heap[index].priority;
}

_getParentIndex(index) {
return ((index + 1) >>> 1) - 1;
}

_getLeftIndex(index) {
return (index << 1) + 1;
}

_getRightIndex(index) {
return (index + 1) << 1;
}

_swap(i, j) {
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
}

_siftUp() {
let node = this.length - 1;
while (
node > 0 &&
this._getNodePriority(node) <
this._getNodePriority(this._getParentIndex(node))
) {
this._swap(node, this._getParentIndex(node));
node = this._getParentIndex(node);
}
}

_siftDown() {
let node = 0;
while (
(this._getLeftIndex(node) < this.length &&
this._getNodePriority(this._getLeftIndex(node)) <
this._getNodePriority(node)) ||
(this._getRightIndex(node) < this.length &&
this._getNodePriority(this._getRightIndex(node)) <
this._getNodePriority(node))
) {
let minChild =
this._getRightIndex(node) < this.length &&
this._getNodePriority(this._getRightIndex(node)) <
this._getNodePriority(this._getLeftIndex(node))
? this._getRightIndex(node)
: this._getLeftIndex(node);
this._swap(node, minChild);
node = minChild;
}
}
}

module.exports = PriorityQueue;