-
Notifications
You must be signed in to change notification settings - Fork 393
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
Refactor assets slice to fix performance problems #3530
Conversation
As Redux was having serious performance problems when we were using an array to store all assets let's refactor that slice so it is now storing assets as an object that is indexed by assets' symbols. Seems like this is working great with redux and we are able to reduce CPU usage as we were updating assets very often.
Looks like we have two competing approaches here with #3526 . I directionally prefer the approach in 3526, because the question of “what do we use as the key for an asset” is one that seems easy and then inevitably gets complicated, even though we're trivializing it right now with With that said, I'm interested in seeing how the two perform relative to each other, and, if this performs better, developing a little bit of a deeper understanding of why to guide future decisions for how redux is structured. My intuition would expect this approach to have the same performance characteristics of the current flat list, because I would expect any change of the Obviously you're seeing performance improvements, so I would love to understand what isn't triggering here that is in the current approach. Maybe I can do some digging on my own there :) |
I'll dig further today but I think #3526 should work fine with this PR - my current theory is that keeping assets as a flat list is making redux work really hard while merging state. Obviously, I have to confirm it to be sure. |
The reason I didn't expect that to be the case is that if you look at the package of updates that comes across the wire it's a diff, so should be relatively few changes to the list, and I believe the changes should be in place. That might not be how it's happening in practice though! |
Ok so I've investigated it a little bit more and the problem is actually in the The second problem is that we are dispatching the Seems like changing the structure of the redux slice is already making it easier to calculate diff but I'm looking for other options on how to handle it best. The last part of solving this problem would be to make sure react is not triggered unnecessarily by these assets slice changes. |
I… Have an idea here. We've looked at auto-batching actions and subscribers before and it's seemed very complicated and had some potential unintended consequences (see #1296). But what I just realized is what want here isn't to batch actions, it's to batch store updates, because that's the expensive thing here. I think we can (ab)use the patch function to debounce… #3535 has a quick draft attempt. |
As assets slice state structure is changing from array to object let's clear currently cached state.
Ok, migration was added, this is good to go to QA. Problems with debouncing state updates I think can be done in another PR as they are not that simple - let's see if this thing alone is making a visible difference. |
Ran a few tests on this, noted a few things: The following tests were ran using tiny-bench and benchmarkjs, the later was dropped due to needing manual patching to work.
From all these tests the more important to us is the one that says
We're also keeping assets from all networks in state, which is not ideal. These tests can be run with this patch: |
Closed until reprioritization |
Resolves #3466
What
As Redux was having serious performance problems when we were using an array to store all assets let's refactor that slice so it is now storing assets as an object that is indexed by assets' symbols. Seems like this is working great with redux and we are able to reduce CPU usage as we were updating assets very often.
Testing
main
, add ccounts, activate networks, add some custom networks, add custom assets etcload-assets-perf-fix
, reload extensionLatest build: extension-builds-3530 (as of Tue, 18 Jul 2023 16:17:42 GMT).