-
-
Notifications
You must be signed in to change notification settings - Fork 8.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
Use Priority queue in chunk graph algorithm #8238
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0064fcd
Don't process chunks multiple times
MLoughry 701f0e0
Try using a priority queue to optimize processDependenciesBlocksForCh…
MLoughry 1bd8783
Fix accidentally duplicated line
MLoughry c4e9d15
Revert change to not re-queue chunks
MLoughry 9c2882e
Fix accidental regression
MLoughry d973686
Clear temp files
MLoughry c309baa
Merge branch 'master' of https://github.com/webpack/webpack into prio…
MLoughry File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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) { | ||||||
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; | ||||||
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. Calling
Suggested change
|
||||||
} | ||||||
|
||||||
_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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since
items
is optional, you must use anif
to guard the iteration