golang / golang/go

cmd/compile: intrinsify cmp.Compare on common types such as strings

Open
#71,270 12 comments 33 reactions 0 assignees View on GitHub
compiler/runtime NeedsInvestigation Performance
Dominant language
Go
Stars
139k
Forks
19.4k
PR merge metrics
PR metrics pending

Description

https://github.com/golang/go/issues/61725 optimized `strings.Compare`, which is great, but it did not optimize `cmp.Compare[string]`, which is otherwise equivalent.

This is leading to users learning that they should avoid `cmp.Compare[string]` for the sake of performance, even writing linters for it like https://github.com/tklauser/lintcomparestrings, which in my opinion is really unfortunate. For the same reason, I find changes like https://go-review.googlesource.com/c/go/+/642038 unfortunate and unnecessary.

The compiler should be clever enough to optimize the generic `cmp.Compare` function just as well as specialized functions such as `strings.Compare` or any others that might exist for common comparable types such as integers. Then the developers don't have to remember facts about which one of them is faster.

Personally, I also find it pretty nice to consistently use `cmp.Compare`. Needing to mix different compare functions in an expression for the sake of performance is a bit odd.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.