-
Notifications
You must be signed in to change notification settings - Fork 642
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
Replacing large array very slow (possibly due to reconciliation) #2237
Comments
Hey @yossivainshtein - thanks for the bug report! I agree that's pretty bad. You wrote this:
What code are you referring to specifically? I can take a look and see if there's a chance to clean things up. I will mark this as a bug and hopefully get around to it in the coming months! Happy to review any PRs that you or other observers write. |
Hi @coolsoftwaretyler , Thanks for the quick response! I was referring to the function
So the reconciliation process makes 2 nested loops, taking a long time. I'd love to write a PR for this but i'm not familiar with the project, I can try. Would appreciate any pointers on contributing if you can. |
Thanks for the specifics, @yossivainshtein. We have a contributing guide in https://github.com/mobxjs/mobx-state-tree/blob/master/CONTRIBUTING.md that will help you get started. Happy to answer any specific questions you have. I'm going to assign this issue to you for now. Feel free to drop any dev questions you have in this thread, or send me an email. If you decide not to work on it, please let me know and I'll remove your assignment and we'll add it to the backlog. |
Note that if you change the array to |
Bug report
I have a simple model containing an array of ~10K randomly generated points (points are a very simple model with an
identifier
and a string field).I'm replacing the array with another similar-sized array of randomly-generated points. (In the real app this would be data coming from the server)
The replace itself takes a very, very long time - about 16 seconds on my machine.
I tried to understand why, I think this is due to the attempts of entity reconciliation in the array. It has 2 nested for loops, resulting in O(n^2) iterations.
I'm not sure why this is needed though, as the entities contain IDs and can be looked up quickly - Is there a reason for this? Am I doing something wrong?
if I insert the line
store.setPoints([]);
before assigning the new array, there's no performance issue. This is a workaround though, I don't want to always be conscious of this issue when replacing arrays.
Sandbox link or minimal reproduction code
https://codesandbox.io/p/sandbox/check-mst-repalce-performance-forked-2jvqwj?workspaceId=ws_UCwiGtoQYRJP5RUk9PpyQL
** What should happen? **
setPoints
should take a reasonable amount of time** What happens instead? **
setPoints
is agonizingly slow, takes 16sec on my machineThe text was updated successfully, but these errors were encountered: