Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf: Rely on more efficient Map for Priority Queue internal state (#…
…47156) The priority queue has been maintaining an array of enqueued contexts and a `WeakMap` of enqueued callbacks for dispatching idle updates. Priority ordering of context is maintained through the inherent order of the array, but this comes at the cost of calling `.shift()` on every dispatch, which rewrites the ordering of all the remaining keys or indices of the array. When the priority queue gets large (such as in store updates) this can incur a measurable cost in time. Additionally, the use of the array for ordering relies on `WeakMap` semantics to loosely hold references to the contexts, which might be removed. In this patch we're relying solely on a single `Map`. By spec, `Map` iteration follows the order of first insertion of its keys, which exactly matches the needs of the priority queue iteration. We're able to avoid using the `Array` altogether, and while not totally free, `Map` deletion is measurably faster than `Array.prototype.shift()` and will involve at least one fewer references to the context memory than having it in the array _and_ the `WeakMap` did. The goal of this patch is small and likely hard to measure. It aims to lower memory allocation and pressure on the garbage collector during the critical hot-patch of code which runs on every keypress or change in the editor, so being a little better here will hopefully add up to macro-level improvements in editor responsiveness.
- Loading branch information
14e8390
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.
Flaky tests detected in 14e8390.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4002887620
📝 Reported issues:
specs/editor/various/block-deletion.test.js
specs/editor/various/multi-block-selection.test.js