Skip to content

Reduce allocations in Find and ReplaceAll#16

Open
pgavlin wants to merge 1 commit intopetar-dambovaliev:masterfrom
pgavlin:find-allocations
Open

Reduce allocations in Find and ReplaceAll#16
pgavlin wants to merge 1 commit intopetar-dambovaliev:masterfrom
pgavlin:find-allocations

Conversation

@pgavlin
Copy link
Contributor

@pgavlin 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
@pgavlin
Copy link
Contributor Author

pgavlin commented May 6, 2025

benchstat output:

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

@petar-dambovaliev petar-dambovaliev self-assigned this Sep 1, 2025
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.

2 participants