-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Perf: Rely on a single store listener only #51568
Conversation
Size Change: +19 B (0%) Total Size: 1.44 MB
ℹ️ View Unchanged
|
Flaky tests detected in dbc7e699a8f10fa619fc73af9f2d0e3cc6dd6d9d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5379211194
|
@@ -155,6 +155,7 @@ export default function createReduxStore( key, options ) { | |||
Object.assign( privateSelectors, selectors ); | |||
}, | |||
}; | |||
const privateListeners = new Set(); |
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.
This should be a property of a store instance, not of the entire store "class". If you register the same store into multiple registries (Gutenberg does that in practice for the block-editor
store), it will have multiple instances and we want each instance to have its own set of listeners.
Moving this declaration a few lines down, into the instantiate
function, should fix this.
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.
good catch. I moved it in 88b2e1931b32fdb4f6e401a91b3356fc06a4b22e
also renamed to listeners
since it doesn't seem necessary to call it "private" and it doesn't conflict with any existing listeners
value
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.
guessing that this passed all the tests because it is unlikely that changes only occurred in one store, so the paths that are tested both produced situations where a combined array happened to work. plausibly there could be situations where it isn't fine.
or maybe the fact that these are all hooks and extra calls are/should be benign is fine.
listener(); | ||
} | ||
} | ||
} ); |
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.
So, we are keeping the per-store subscriptions, and only optimize the prevState === nextState
check? Doing it just once instead of X times?
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.
yeah, it seems like this could be the key piece of this change, since Redux itself is doing basically the same think with a list of listeners.
I don't fully know why this is effective, but it seems to measure that way.
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.
In an active editor, there are 1000 listeners, and every keystroke, when typing, generates 1000 calls. It's plausible that even such a small operation, on code that doesn't do anything expensive (getter call + ===
compare), has a visible impact when removed.
By the way, do you have raw timing data, i.e., all the individual measurements instead of only averages, for these 180 rounds you did? It would be interesting to calculate the variances, how the errors are big and how they are distributed.
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.
I don't have that information, and while it's possible to gather it, it can be meticulous. The min
and max
values give us some first-order approximation of the variance.
At some point I'd like to save all the values by default. Running multiple --rounds
is much easier.
@youknowriad should we merge this and try it out? tests pass, and the editor appears to work fine. |
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.
👍
f9527ce
to
b3de631
Compare
Currently when a component calls useSelect or any of a number of other hooks, the store creates a new listener through Redux's subscribe method. In this patch we're storing a custom list of listeners before calling into Redux and relying on a single Redux listener to call all of the registered functions. This reduces the number of comparisons to `getState()` when updtaing the store and measured a reduction in several key metrics by between 4% to 9%; specifically the `type`, `focus`, and `inserter` methods. Co-authored-by: Jarda Snajdr <[email protected]>
b3de631
to
d9c53e1
Compare
Just want to confirm that it's clear from codevitals graphs that this improved performance (block select, inserter hover, first block loaded) 👏 |
Status
The first draft of this work apparently has a significant positive impact on the performance of a few different things we measure. I also noticed what felt like an improvement in local testing. It also isn't breaking any tests.
Right now I'm scaling back the scope and will re-run the perf tests. Getting 180 rounds of results took 40 hours, and I will do that again just to make sure we only have to run them once.Not yet have I looked into the memory overhead or garbage-collector runtime, but I would be interested to know if this has any measurable impact on that side of the equation.
What?
Currently when a component calls
useSelect
or any of a number of other hooks, the store creates a new listener through Redux'ssubscribe
method. In this patch we're storing a custom list of listeners before calling into Redux and relying on a single Redux listener to call all of the registered functions.Why?
We have major performance issues related to over-allocation and this patch is one of many avenues to try and find out the source of the issues and to resolve them.
How?
At first it seemed like we might be creating actual event listeners for the Redux store, but after running some experiments that seems like it may not be the case, as Redux internally does what this patch does.
Upon more inspection it could be that the important change is extracting the call togetState()
out of the individual listener closures, or possibly in avoiding the creation of those closures at all.Coming next will be an attempt to reduce the scope of the PR only togetState
changes.After more inspection, it doesn't seem clear to me how to extract the call to
getState
without also trapping all the listeners. This change thus is more atomic in nature. It may still be that thegetState
shuffling is where the performance improvement comes, but that isn't easily separable from the closure change.Results
With
--rounds 180
, taking 40 hoursThis seems like it's measuring a significant reduction in:
type
~ 4% (post) ~ 6% (site)focus
~ 6%inserterSearch
~ 9%inserterHover
~ 9%