Performance optimizations - Dedicated functions for map and list diffs#28
Merged
corka149 merged 3 commits intocorka149:masterfrom Jul 20, 2025
Merged
Conversation
Contributor
Author
|
OK found another nice improvement of 50%. It turns out checking if string exists before doing replacement is greatly beneficial for performance. Take a look: |
Owner
|
Hi @Valian thanks for all the effort. <3 I will have a look at it asap. |
corka149
previously approved these changes
Jul 16, 2025
Owner
corka149
left a comment
There was a problem hiding this comment.
Looks good to me - please fix just the credo issues.
Contributor
Author
|
@corka149 Fixed! 👍🏻 |
corka149
approved these changes
Jul 19, 2025
1760247 to
d04a22c
Compare
corka149
approved these changes
Jul 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!