Description
What version of Go are you using (go version
)?
1.12ish, but seems common
Does this issue reproduce with the latest release?
Yup.
What operating system and processor architecture are you using (go env
)?
Mostly amd64, but hardly relevant.
What did you do?
Hang out in #performance on gopher slack, notice someone commenting about string comparison being faster than bytes.Equal. Got curious.
https://play.golang.org/p/iywf6uwmyHE
Perhaps more informative:
https://godbolt.org/z/59xE8E
What did you expect to see?
Not a factor of two performance penalty for using bytes.Equal
What did you see instead?
Reported timing of 7ns vs 17ns for an op that relied heavily on equality tests of small byte slices.
Observations: string comparison can inline the test for equal length, which resolves a large number of tests without actually doing the comparisons. Calling bytes.Equal requires passing two slice headers, and has to do all the function call setup and overhead unconditionally even in the trivial cases.
Not sure this is actually fixable, or possibly even worth fixing, but I was thinking about writing up a thing on how you should do this, and then thinking about what happens if a lot of programmers get in the habit of always converting []byte to string to work on it, and all the pitfalls that could ensue, and I thought "hmm maybe this is worth a look".