Skip to content
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

Reduce the number of allocations on the LevenshteinDistance function #38

Merged
merged 1 commit into from
May 3, 2022
Merged

Reduce the number of allocations on the LevenshteinDistance function #38

merged 1 commit into from
May 3, 2022

Conversation

harrison3000
Copy link
Contributor

This PR makes the LevenshteinDistance function a bit faster for small strings, but most importantly reduces the number of allocations by allocating the column slice on the stack
Somehow for bigger strings it also reduced the allocation count by 1, I think the compiler recognizes this pattern of appending in a loop and optimizes it

name                            old time/op    new time/op    delta
LevenshteinDistance-16             111ns ± 5%      57ns ± 1%   -48.26%  (p=0.008 n=5+5)
LevenshteinDistanceBigLate-16     12.3µs ± 2%    13.4µs ± 2%    +9.02%  (p=0.008 n=5+5)
LevenshteinDistanceBigEarly-16    11.1µs ± 3%    11.5µs ± 0%    +3.10%  (p=0.016 n=5+4)

name                            old alloc/op   new alloc/op   delta
LevenshteinDistance-16             40.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
LevenshteinDistanceBigLate-16     6.58kB ± 0%    6.53kB ± 0%    -0.73%  (p=0.008 n=5+5)
LevenshteinDistanceBigEarly-16    6.58kB ± 0%    6.53kB ± 0%    -0.73%  (p=0.008 n=5+5)

name                            old allocs/op  new allocs/op  delta
LevenshteinDistance-16              2.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
LevenshteinDistanceBigLate-16       2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.008 n=5+5)
LevenshteinDistanceBigEarly-16      2.00 ± 0%      1.00 ± 0%   -50.00%  (p=0.008 n=5+5)

For strings a bit bigger (but smaller than 64 runes) the speed improvement vanishes but the 0 alocation remains

name                    old time/op    new time/op    delta
LevenshteinDistance-16    1.35µs ± 1%    1.31µs ± 2%    -3.20%  (p=0.029 n=4+4)

name                    old alloc/op   new alloc/op   delta
LevenshteinDistance-16      272B ± 0%        0B       -100.00%  (p=0.029 n=4+4)

name                    old allocs/op  new allocs/op  delta
LevenshteinDistance-16      2.00 ± 0%      0.00       -100.00%  (p=0.029 n=4+4)

@lithammer
Copy link
Owner

Very nice work! Simple, yet effective. Thanks!

@lithammer lithammer merged commit 61128a1 into lithammer:master May 3, 2022
@lithammer
Copy link
Owner

lithammer commented May 3, 2022

Released v1.1.5 with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants