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

bytes: speed up Replace #72967

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

jub0bs
Copy link

@jub0bs jub0bs commented Mar 20, 2025

This CL is to package bytes what CL 657935 was to package strings.

Add fuzz test and benchmarks for Replace.

The length of parameter old does not change. Move the corresponding length
check outside the loop. Use range-over-int loops where possible.

Some benchmark results (no changes to allocations):

goos: darwin
goarch: amd64
pkg: bytes
cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
│ old │ new │
│ sec/op │ sec/op vs base │
Replace/"hello""l""L"0-8 27.83n ± 2% 27.22n ± 1% -2.17% (p=0.000 n=20)
Replace/"hello"
"l""L"-1-8 60.64n ± 0% 57.97n ± 1% -4.40% (p=0.000 n=20)
Replace/"hello""x""X"-1-8 33.99n ± 0% 33.88n ± 0% ~ (p=0.140 n=20)
Replace/""
"x""X"-1-8 10.40n ± 1% 10.57n ± 0% +1.64% (p=0.000 n=20)
Replace/"radar""r"""-1-8 62.63n ± 0% 61.39n ± 0% -1.98% (p=0.000 n=20)
Replace/""
"""<>"-1-8 29.76n ± 1% 24.18n ± 1% -18.75% (p=0.000 n=20)
Replace/"banana""a""<>"-1-8 77.00n ± 0% 77.10n ± 1% ~ (p=0.525 n=20)
Replace/"banana"
"a""<>"1-8 44.24n ± 0% 43.57n ± 1% -1.54% (p=0.000 n=20)
Replace/"banana"
"a"
"<>"1000-8 78.23n ± 0% 77.16n ± 1% -1.36% (p=0.000 n=20)
Replace/"banana"
"an""<>"-1-8 72.78n ± 1% 69.97n ± 1% -3.85% (p=0.000 n=20)
Replace/"banana""ana""<>"-1-8 54.41n ± 0% 54.04n ± 1% -0.67% (p=0.033 n=20)
Replace/"banana"
"""<>"-1-8 116.8n ± 1% 103.5n ± 1% -11.42% (p=0.000 n=20)
Replace/"banana""""<>"10-8 117.2n ± 1% 103.6n ± 0% -11.60% (p=0.000 n=20)
Replace/"banana"
"""<>"6-8 105.30n ± 0% 92.50n ± 0% -12.16% (p=0.000 n=20)
Replace/"banana"
""
"<>"5-8 91.81n ± 0% 79.87n ± 1% -13.01% (p=0.000 n=20)
Replace/"banana"
"""<>"1-8 35.87n ± 1% 30.33n ± 1% -15.43% (p=0.000 n=20)
Replace/"banana"
"a"
"a"-1-8 70.84n ± 0% 68.83n ± 0% -2.84% (p=0.000 n=20)
Replace/"banana"
"a""a"1-8 44.27n ± 1% 43.47n ± 1% -1.80% (p=0.000 n=20)
Replace/"☺☻☹"
""
"<>"_-1-8 104.25n ± 1% 93.33n ± 0% -10.48% (p=0.000 n=20)
geomean 56.31n 52.88n -6.09%

@gopherbot
Copy link
Contributor

This PR (HEAD: ca56e09) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/659515.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from jub0bs:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Jorropo:

Patch Set 1: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2025-03-20T15:04:30Z","revision":"fa97c61fc9da668b2702c88d2087cb9c56538350"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Jorropo:

Patch Set 1: -Commit-Queue


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

Julien Cretel added 2 commits March 20, 2025 21:35
This CL is to package bytes what CL 657935 was to package strings.

The length of parameter old does not change. Move the corresponding length
check outside the loop. Use range-over-int loops where possible.

Some benchmark results (no changes to allocations):

goos: darwin
goarch: amd64
pkg: bytes
cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
                                 │     old      │                 new                 │
                                 │    sec/op    │   sec/op     vs base                │
Replace/"hello"_"l"_"L"_0-8         27.83n ± 2%   27.22n ± 1%   -2.17% (p=0.000 n=20)
Replace/"hello"_"l"_"L"_-1-8        60.64n ± 0%   57.97n ± 1%   -4.40% (p=0.000 n=20)
Replace/"hello"_"x"_"X"_-1-8        33.99n ± 0%   33.88n ± 0%        ~ (p=0.140 n=20)
Replace/""_"x"_"X"_-1-8             10.40n ± 1%   10.57n ± 0%   +1.64% (p=0.000 n=20)
Replace/"radar"_"r"_"<r>"_-1-8      62.63n ± 0%   61.39n ± 0%   -1.98% (p=0.000 n=20)
Replace/""_""_"<>"_-1-8             29.76n ± 1%   24.18n ± 1%  -18.75% (p=0.000 n=20)
Replace/"banana"_"a"_"<>"_-1-8      77.00n ± 0%   77.10n ± 1%        ~ (p=0.525 n=20)
Replace/"banana"_"a"_"<>"_1-8       44.24n ± 0%   43.57n ± 1%   -1.54% (p=0.000 n=20)
Replace/"banana"_"a"_"<>"_1000-8    78.23n ± 0%   77.16n ± 1%   -1.36% (p=0.000 n=20)
Replace/"banana"_"an"_"<>"_-1-8     72.78n ± 1%   69.97n ± 1%   -3.85% (p=0.000 n=20)
Replace/"banana"_"ana"_"<>"_-1-8    54.41n ± 0%   54.04n ± 1%   -0.67% (p=0.033 n=20)
Replace/"banana"_""_"<>"_-1-8       116.8n ± 1%   103.5n ± 1%  -11.42% (p=0.000 n=20)
Replace/"banana"_""_"<>"_10-8       117.2n ± 1%   103.6n ± 0%  -11.60% (p=0.000 n=20)
Replace/"banana"_""_"<>"_6-8       105.30n ± 0%   92.50n ± 0%  -12.16% (p=0.000 n=20)
Replace/"banana"_""_"<>"_5-8        91.81n ± 0%   79.87n ± 1%  -13.01% (p=0.000 n=20)
Replace/"banana"_""_"<>"_1-8        35.87n ± 1%   30.33n ± 1%  -15.43% (p=0.000 n=20)
Replace/"banana"_"a"_"a"_-1-8       70.84n ± 0%   68.83n ± 0%   -2.84% (p=0.000 n=20)
Replace/"banana"_"a"_"a"_1-8        44.27n ± 1%   43.47n ± 1%   -1.80% (p=0.000 n=20)
Replace/"☺☻☹"_""_"<>"_-1-8         104.25n ± 1%   93.33n ± 0%  -10.48% (p=0.000 n=20)
geomean                             56.31n        52.88n        -6.09%
@jub0bs jub0bs force-pushed the speedup-bytes-replace branch from ca56e09 to d1caf1f Compare March 20, 2025 20:36
@gopherbot
Copy link
Contributor

This PR (HEAD: d1caf1f) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/659515.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from jub0bs:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/659515.
After addressing review feedback, remember to publish your drafts!

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