-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: efficient way to compare a byte slice and a string #11777
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
Comments
Go 1.5 has what you want. It was implemented in CL #3120. |
@nuss-justin Now I see why |
I don't believe this is true. If you disassemble
you'll see that
Benchmarking also shows that there's almost no difference between the cast and a call to
gives
|
Currently Go does not provide a utility to compare efficiently a byte slice against a string. Doing
string(byteSlice) == str
is inefficient as it copies the slice first into a new string. It would be nice to have a utility likebytes.EqualString(b []byte, s string)
that is just as efficient asbytes.Equal(a, b []byte)
. Another alternative is to optimizestring(byteSlice) compareOp str
so compareOp accesses the slice directly. The latter would benefit the existing code even if it is harder to implement.The text was updated successfully, but these errors were encountered: