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, strings: optimize Cut for single-byte separators #67125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aimuz
Copy link
Contributor

@aimuz aimuz commented May 1, 2024

Optimize the Cut function in both the bytes and strings packages
to immediately return slices when the separator is a single byte (or
character), avoiding more complex index searching logic. This change
can significantly reduce the execution time for these specific cases,
as benchmark tests added to each package demonstrate improvements.

The optimization checks if the length of the separator is one before
proceeding with the existing search strategy. If so, it uses IndexByte
for a faster lookup of the separator's position.

Additionally, benchmark tests have been added for both packages to
demonstrate the performance benefits of this optimization across
various scenarios.

goos: darwin
goarch: arm64
pkg: strings
cpu: Apple M2 Max
│ old-cut.txt │ new-cut.txt │
│ sec/op │ sec/op vs base │
Cut/Cut-One/2-12 4.107n ± 2% 3.431n ± 2% -16.44% (p=0.000 n=10)
Cut/Cut-Two/2-12 8.123n ± 2% 8.460n ± 1% +4.15% (p=0.000 n=10)
Cut/Cut-One-Nil/2-12 2.720µ ± 1% 2.751µ ± 0% +1.14% (p=0.000 n=10)
Cut/Cut-Two-Nil/2-12 2.724µ ± 0% 2.771µ ± 0% +1.69% (p=0.000 n=10)
Cut/Cut-One/4-12 4.091n ± 1% 3.487n ± 2% -14.76% (p=0.000 n=10)
Cut/Cut-Two/4-12 8.211n ± 0% 8.593n ± 1% +4.64% (p=0.000 n=10)
Cut/Cut-One-Nil/4-12 2.289µ ± 1% 2.340µ ± 1% +2.23% (p=0.000 n=10)
Cut/Cut-Two-Nil/4-12 2.306µ ± 0% 2.333µ ± 0% +1.15% (p=0.000 n=10)
Cut/Cut-One/8-12 4.090n ± 1% 3.582n ± 3% -12.41% (p=0.000 n=10)
Cut/Cut-Two/8-12 8.270n ± 1% 8.610n ± 0% +4.11% (p=0.000 n=10)
Cut/Cut-One-Nil/8-12 2.089µ ± 1% 2.119µ ± 0% +1.44% (p=0.001 n=10)
Cut/Cut-Two-Nil/8-12 2.101µ ± 1% 2.119µ ± 0% +0.88% (p=0.019 n=10)
Cut/Cut-One/16-12 4.095n ± 1% 3.481n ± 3% -14.98% (p=0.000 n=10)
Cut/Cut-Two/16-12 8.193n ± 1% 8.601n ± 0% +4.98% (p=0.000 n=10)
Cut/Cut-One-Nil/16-12 1.966µ ± 2% 1.999µ ± 0% ~ (p=0.118 n=10)
Cut/Cut-Two-Nil/16-12 2.001µ ± 1% 2.000µ ± 0% ~ (p=0.954 n=10)
Cut/Cut-One/32-12 4.449n ± 1% 3.825n ± 2% -14.03% (p=0.000 n=10)
Cut/Cut-Two/32-12 8.903n ± 1% 9.211n ± 1% +3.46% (p=0.000 n=10)
Cut/Cut-One-Nil/32-12 1.938µ ± 1% 1.939µ ± 0% ~ (p=0.926 n=10)
Cut/Cut-Two-Nil/32-12 1.920µ ± 0% 1.943µ ± 0% +1.17% (p=0.000 n=10)
Cut/Cut-One/64-12 4.712n ± 0% 4.064n ± 1% -13.75% (p=0.000 n=10)
Cut/Cut-Two/64-12 9.123n ± 0% 9.438n ± 1% +3.45% (p=0.000 n=10)
Cut/Cut-One-Nil/64-12 1.882µ ± 1% 1.900µ ± 0% +0.93% (p=0.000 n=10)
Cut/Cut-Two-Nil/64-12 1.886µ ± 0% 1.903µ ± 0% +0.90% (p=0.000 n=10)
geomean 113.1n 110.5n -2.30%

For #67101

@gopherbot
Copy link

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

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

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

Message from qiulaidongfeng:

Patch Set 1: Commit-Queue+1


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

@gopherbot
Copy link

Message from Go LUCI:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2024-05-01T13:50:56Z","revision":"cc0187165171154acd67e50e0de2b536fb1bcbda"}


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

@gopherbot
Copy link

Message from qiulaidongfeng:

Patch Set 1: -Commit-Queue


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

@gopherbot
Copy link

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/582655.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link

Message from Go LUCI:

Patch Set 1: LUCI-TryBot-Result+1


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

@gopherbot
Copy link

Message from Ian Lance Taylor:

Patch Set 1:

(3 comments)


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

Optimize the Cut function in both the bytes and strings packages
to immediately return slices when the separator is a single byte (or
character), avoiding more complex index searching logic. This change
can significantly reduce the execution time for these specific cases,
as benchmark tests added to each package demonstrate improvements.

The optimization checks if the length of the separator is one before
proceeding with the existing search strategy. If so, it uses IndexByte
for a faster lookup of the separator's position.

Additionally, benchmark tests have been added for both packages to
demonstrate the performance benefits of this optimization across
various scenarios.

goos: darwin
goarch: arm64
pkg: strings
cpu: Apple M2 Max
                  │ old-cut.txt │             new-cut.txt             │
                  │   sec/op    │   sec/op     vs base                │
Cut/Cut-One/2-12    4.026n ± 2%   3.274n ± 2%  -18.68% (p=0.000 n=10)
Cut/Cut-Two/2-12    8.093n ± 0%   8.357n ± 0%   +3.27% (p=0.000 n=10)
Cut/Cut-One/4-12    4.048n ± 1%   3.324n ± 2%  -17.91% (p=0.000 n=10)
Cut/Cut-Two/4-12    8.105n ± 0%   8.377n ± 1%   +3.35% (p=0.000 n=10)
Cut/Cut-One/8-12    4.089n ± 1%   3.290n ± 1%  -19.53% (p=0.000 n=10)
Cut/Cut-Two/8-12    8.107n ± 1%   8.359n ± 1%   +3.10% (p=0.000 n=10)
Cut/Cut-One/16-12   4.127n ± 1%   3.328n ± 1%  -19.35% (p=0.000 n=10)
Cut/Cut-Two/16-12   8.119n ± 1%   8.374n ± 1%   +3.15% (p=0.000 n=10)
Cut/Cut-One/32-12   4.545n ± 2%   3.675n ± 1%  -19.14% (p=0.000 n=10)
Cut/Cut-Two/32-12   8.708n ± 1%   8.963n ± 1%   +2.92% (p=0.000 n=10)
Cut/Cut-One/64-12   4.825n ± 2%   4.146n ± 1%  -14.08% (p=0.000 n=10)
Cut/Cut-Two/64-12   9.286n ± 0%   9.315n ± 1%        ~ (p=0.105 n=10)
geomean             5.983n        5.486n        -8.32%

                  │ old-cut.txt  │             new-cut.txt             │
                  │     B/op     │    B/op     vs base                 │
Cut/Cut-One/2-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/2-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/4-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/4-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/8-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/8-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/16-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/16-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/32-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/32-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/64-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/64-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                  │ old-cut.txt  │             new-cut.txt             │
                  │  allocs/op   │ allocs/op   vs base                 │
Cut/Cut-One/2-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/2-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/4-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/4-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/8-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/8-12    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/16-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/16-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/32-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/32-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-One/64-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Cut/Cut-Two/64-12   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

For golang#67101
@gopherbot
Copy link

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

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

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

Message from Ingo Oeser:

Patch Set 3:

(1 comment)


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

@gopherbot
Copy link

Message from Egon Elbre:

Patch Set 3:

(1 comment)


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

@gopherbot
Copy link

Message from Zhen Li:

Patch Set 3:

(3 comments)


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

@gopherbot
Copy link

Message from Zhen Li:

Patch Set 3:

(1 comment)


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

@gopherbot
Copy link

Message from Egon Elbre:

Patch Set 3:

(1 comment)


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

@gopherbot
Copy link

Message from Zhen Li:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/582655.
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.

None yet

2 participants