]> Cypherpunks repositories - gostls13.git/commitdiff
bytes, strings: micro-optimize EqualFold
authorJulien Cretel <jub0bsinthecloud@gmail.com>
Mon, 12 May 2025 12:40:36 +0000 (12:40 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 12 May 2025 14:41:07 +0000 (07:41 -0700)
The first loop leaves the lengths of the two arguments unchanged.

Take advantage of this invariant in the loop's condition. Here are some
benchmark results (no change to allocations):

goos: darwin
goarch: amd64
pkg: strings
cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
                          │     old     │                new                 │
                          │   sec/op    │   sec/op     vs base               │
EqualFold/Tests-8           240.0n ± 4%   245.1n ± 5%       ~ (p=0.516 n=20)
EqualFold/ASCII-8           11.50n ± 1%   11.04n ± 0%  -3.96% (p=0.000 n=20)
EqualFold/UnicodePrefix-8   102.1n ± 0%   102.2n ± 0%       ~ (p=0.455 n=20)
EqualFold/UnicodeSuffix-8   90.14n ± 0%   89.80n ± 1%       ~ (p=0.113 n=20)
geomean                     71.00n        70.60n       -0.56%

Change-Id: I1f6d1df8a0398f9493692f59d7369c3f0fbba436
GitHub-Last-Rev: 9508ee26ad3cadcbb5e532a731b2553ba900f2b1
GitHub-Pull-Request: golang/go#73672
Reviewed-on: https://go-review.googlesource.com/c/go/+/671756
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/bytes/bytes.go
src/strings/strings.go

index 8198415c3e193849aa9c9ca56ef990bdd9116511..a0a8fa0b2986e1e3e937de239a0da216352b9627 100644 (file)
@@ -1228,7 +1228,7 @@ func ReplaceAll(s, old, new []byte) []byte {
 func EqualFold(s, t []byte) bool {
        // ASCII fast path
        i := 0
-       for ; i < len(s) && i < len(t); i++ {
+       for n := min(len(s), len(t)); i < n; i++ {
                sr := s[i]
                tr := t[i]
                if sr|tr >= utf8.RuneSelf {
index d07a064228978f52fbea3baae0c0b9e256b49c49..d2cda74f68752e4f9fe252044601e0b07a3d65c2 100644 (file)
@@ -1194,7 +1194,7 @@ func ReplaceAll(s, old, new string) string {
 func EqualFold(s, t string) bool {
        // ASCII fast path
        i := 0
-       for ; i < len(s) && i < len(t); i++ {
+       for n := min(len(s), len(t)); i < n; i++ {
                sr := s[i]
                tr := t[i]
                if sr|tr >= utf8.RuneSelf {