Performance optimizations - Dedicated functions for map and list diffs #28
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 👋 Thanks for an amazing library. While working on LiveVue patches, I've spotted some low-hanging fruits about performance of this package. So here I go 😄
Approach
First, I've created a small benchmark in
test/benchmark.exsusing Benchee (I've added it to mix.exs, only in dev). Then I've developed a new version of diff alongside existing one, and after being done I've replaced original code to create this PR.You can start benchmark by running
mix run test/benchmark.exs, but currently it will only show numbers for existing implementation. To compare it to the old one we'd need to copy an old implementation into a separate module.Overview of changes
My goal was to reduce the number of memory allocations. Each time I could skip creating a new object, I tried to do so.
It turns out that a dedicated function for making a diff for maps and for lists is the best way, as it allows to write a highly-optimized code that almost entirely avoids memory allocations.
Results
In my tests new implementation is 2-3x faster, and consumes around 2.5 less memory. Log from benchee:
All tests are passing, so I have a pretty high confidence everything is working in the same way as before. Hope these changes will be useful!