golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.1k stars 17.24k forks source link

bytes, strings: optimize Cut for single-byte separators #67125

Open aimuz opened 2 weeks ago

aimuz commented 2 weeks ago

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 commented 2 weeks ago

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

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

Important tips:

gopherbot commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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!

gopherbot commented 2 weeks ago

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

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

Important tips:

gopherbot commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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!