-
Notifications
You must be signed in to change notification settings - Fork 18k
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
cmp: optimize Compare #63360
cmp: optimize Compare #63360
Conversation
Compare provides three-way comparison for Ordered types, which certain algorithms benefit from. Float comparisons require NaN handling that should be optimized away for all other types. String comparisons benefit from trying == first and short-circuiting on different length strings. Ideally the compiler would recognize the pattern and specialize on strings, but this improves the status quo. Benchmark shows: - no change for ints, - 20% improvement for floats, and - 25% improvement for strings. Bug: #61725
This PR (HEAD: e776e0e) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/531997. Important tips:
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Nuno Cruces: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Ian Lance Taylor: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Dave Cheney: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
This PR (HEAD: d74b2a9) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/531997. Important tips:
|
Message from Nuno Cruces: Patch Set 3: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Nuno Cruces: Patch Set 4: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
This PR (HEAD: d74b2a9) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/531997. Important tips:
|
Message from Ian Lance Taylor: Patch Set 5: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Go LUCI: Patch Set 5: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2023-10-08T14:59:37Z","revision":"fc0504b8aaaabdb68448a8d09a52ba3ddc6789c8"} Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Ian Lance Taylor: Patch Set 5: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Go LUCI: Patch Set 5: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Go LUCI: Patch Set 5: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Ian Lance Taylor: Patch Set 5: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Nuno Cruces: Patch Set 5: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
This PR (HEAD: b3bfbdf) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/531997. Important tips:
|
Message from Nuno Cruces: Patch Set 6: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Ian Lance Taylor: Patch Set 7: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Message from Nuno Cruces: Patch Set 7: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/531997. |
Compare provides three-way comparison for Ordered types, which certain
algorithms benefit from.
Float comparisons require NaN handling that should be optimized away for
all other types.
String comparisons benefit from using != which short-circuits on
different length strings.
Ideally the compiler would recognize the pattern and specialize on
strings, but this tries to improve the status quo.
goos: darwin
goarch: amd64
pkg: cmp
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
│ base.txt │ patch.txt │
│ sec/op │ sec/op vs base │
Compare_int-12 1.638n ± 1% 1.635n ± 2% ~ (p=0.698 n=10)
Compare_float64-12 2.397n ± 1% 2.206n ± 0% -7.99% (p=0.000 n=10)
Compare_strings-12 8.054n ± 1% 6.705n ± 2% -16.75% (p=0.000 n=10)
geomean 3.162n 2.892n -8.56%
Updates #31187
Updates #61725