Improving merge strategy (3х performance increase, and 5x memory allocate decrease) #346
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.
Hi all, let me introduce my idea and improvement of the current merge logic.
Reason:
I noticed in my application had a performance problem and memory leaks. It was because the merge logic was doing double work, long story short on each iteration of the loop, we created a model in mst and then converted the newly created model into the graphql response structure.
For example:
We requested 100 entities, and we got this as an array response:
Then we would create each such entity in the
mst.map
structure, and from there convert it to an array.Which is the bottleneck of mst as we have created a copy of the reactive data (array of entites in our case)
it starts to be noticeable when we have an array with 1000 entities in the response
It seems that the author of this issue also has the same problem and also works with mst-gql.
My solution
I used Proxy to get the mobx-model while reading and not while writing (merge), this saves a lot of memory and is the main idea. (it's apply only for arrays, everything else computed as before.)
I rewrote the recursive merge logic to avoid frequent recalculations of reactive models, and left it only for root data, which also makes sense since root data if linked to mst-tree, will be automaticle reactive.
This has been working for a few days in my project and seems to be working fine, but please test it on your projects and post here any issues, I think it can significantly increase the performance of your applications. I would be happy to get feedback.
PS: I will prepare a comparison of the two approaches later.