Reduce allocations in Find and ReplaceAll#16
Open
pgavlin wants to merge 1 commit intopetar-dambovaliev:masterfrom
Open
Reduce allocations in Find and ReplaceAll#16pgavlin wants to merge 1 commit intopetar-dambovaliev:masterfrom
pgavlin wants to merge 1 commit intopetar-dambovaliev:masterfrom
Conversation
Contributor
pgavlin
commented
May 6, 2025
- Ensure that findIter and overlappingIter do not escape to the heap in Next
- Have FindAll use a findIter directly rather than using an Iter
- Do not allocate a slice of replacements in ReplaceAll
- Right-size the output buffer in ReplaceAll
- Ensure that findIter and overlappingIter do not escape to the heap in Next - Have FindAll use a findIter directly rather than using an Iter - Do not allocate a slice of replacements in ReplaceAll - Right-size the output buffer in ReplaceAll
Contributor
Author
|
aho-corasick ❯ benchstat base.txt diff.txt
goos: darwin
goarch: arm64
pkg: github.com/petar-dambovaliev/aho-corasick
cpu: Apple M4 Max
│ base.txt │ diff.txt │
│ sec/op │ sec/op vs base │
Stdlib_StringsReplaceAll/No_matches-16 57.46µ ± 1% 57.26µ ± 1% ~ (p=0.247 n=10)
Stdlib_StringsReplaceAll/Matches-16 52.37µ ± 1% 51.17µ ± 0% -2.28% (p=0.000 n=10)
Stdlib_AhoCorasickReplaceAll/No_matches-16 45.14µ ± 1% 44.06µ ± 1% -2.40% (p=0.000 n=10)
Stdlib_AhoCorasickReplaceAll/Matches-16 55.28µ ± 1% 48.69µ ± 1% -11.92% (p=0.000 n=10)
AhoCorasick_ReplaceAllDFA-16 53.32µ ± 2% 45.31µ ± 1% -15.01% (p=0.000 n=10)
AhoCorasick_ReplaceAllNFA-16 59.46µ ± 2% 50.90µ ± 2% -14.41% (p=0.000 n=10)
AhoCorasick_LeftmostInsensitiveWholeWord-16 49.29µ ± 1% 47.03µ ± 1% -4.59% (p=0.000 n=10)
geomean 52.99µ 49.04µ -7.46%
│ base.txt │ diff.txt │
│ B/op │ B/op vs base │
Stdlib_StringsReplaceAll/No_matches-16 112.0Ki ± 0% 112.0Ki ± 0% ~ (p=1.000 n=10) ¹
Stdlib_StringsReplaceAll/Matches-16 112.0Ki ± 0% 112.0Ki ± 0% ~ (p=1.000 n=10) ¹
Stdlib_AhoCorasickReplaceAll/No_matches-16 56.11Ki ± 0% 56.05Ki ± 0% -0.11% (p=0.000 n=10)
Stdlib_AhoCorasickReplaceAll/Matches-16 180.3Ki ± 0% 112.6Ki ± 0% -37.53% (p=0.000 n=10)
AhoCorasick_ReplaceAllDFA-16 181.7Ki ± 0% 113.6Ki ± 0% -37.49% (p=0.000 n=10)
AhoCorasick_ReplaceAllNFA-16 181.5Ki ± 0% 113.4Ki ± 0% -37.51% (p=0.000 n=10)
AhoCorasick_LeftmostInsensitiveWholeWord-16 56.88Ki ± 0% 56.75Ki ± 0% -0.22% (p=0.000 n=10)
geomean 113.2Ki 92.49Ki -18.29%
¹ all samples are equal
│ base.txt │ diff.txt │
│ allocs/op │ allocs/op vs base │
Stdlib_StringsReplaceAll/No_matches-16 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹
Stdlib_StringsReplaceAll/Matches-16 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹
Stdlib_AhoCorasickReplaceAll/No_matches-16 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10)
Stdlib_AhoCorasickReplaceAll/Matches-16 23.00 ± 0% 13.00 ± 0% -43.48% (p=0.000 n=10)
AhoCorasick_ReplaceAllDFA-16 81.00 ± 0% 56.00 ± 0% -30.86% (p=0.000 n=10)
AhoCorasick_ReplaceAllNFA-16 64.00 ± 0% 39.00 ± 0% -39.06% (p=0.000 n=10)
AhoCorasick_LeftmostInsensitiveWholeWord-16 21.00 ± 0% 19.00 ± 0% -9.52% (p=0.000 n=10)
geomean 13.14 9.958 -24.21%
¹ all samples are equal |
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.