perf(query-core): Improve mutationCache implementation performance #8451
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.
This work is stacked on #8450
In the prior change I modeled unscoped mutations as a separate data store in a way that would speed up methods operating on the internal state of the cache when using unscoped mutations. In this change I further refatored the internal implementation to make removals faster (O(1) generally) and to make the aggregate APIs like
getAll
andfind
faster by avoiding multiple concatenations and flattening.Implementation Details
Scopes are more like scheduling metadata than an intrinsic structure to the set of mutations. In this change I updated the storage format of mutations so that they can be cleared more efficiently. Using a Set allows us to have O(1) removals. We can also construct the Array for getAll() more efficiently by not having to flatten the inner scope arrays. Additionally the scope array is now mutated both on add and remove to avoid allocating extra arrays and doing additional copies.
I rewrote clear to not use this.remove which avoids many intermediate operations against the underlyign data structures.
I considered using an Array again for the primary storage format and this may actually be better if the expected number of mutations isn't excessively large but since I don't know enough about mutation usage I stuck with a Set for the better deletion performance at larger mutation size.