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
cmd/compile: replace []rune(s)[0]
to decoderune(s, 0)
to reduce allocations
#45260
Comments
I dont think this pattern is common enough to warrant special handling. Generally the unicode packages should be preferred over All the optimisations on strings also create an asymmetry because equivalent byte slice expressions are not optimized. Specific to this case I think its also not very practical to use As Encouraging As a side note: If I will be allowed to remove compiler special handling of |
Maybe offtopic, but let me ask.
Why you think it's better to remove? I'm using this construct often and it has very good bench results (near the same as |
Possible vet warning for the cc @mdempsky |
I did some code search. The number of occurrences of
|
I agree that the benefits here for adding an optimization or vet check seem to small to justify the added maintenance cost of that code. It should be easy enough for someone to write their own linter for this using x/tools/go/analysis. |
@dominikh staticcheck might be a possible alternative home. (For more on criteria for vet checkers: https://golang.org/src/cmd/vet/README) |
Feel free to file an issue on Staticcheck at https://github.com/dominikh/go-tools/issues |
Added a simple checker to find []rune("abc")[0] and suggest utf8.DecodeRuneInString("abc"). Expressions like ``` []rune("abc")[0] ``` that cause unnecessary rune slice allocation. See golang/go#45260
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
https://play.golang.org/p/Y81VmhfOpXV
and got
What did you expect to see?
Equal performance of
DecodeRuneInString(s)
and[]rune(s)[0]
without[]rune
allocation.What did you see instead?
[]rune(s)[0]
causes unnecessary allocation of rune slice usingruntime.stringtoslicerune
(https://go.godbolt.org/z/nb9538EvP). This expression just can be replaced byruntime.decoderune
and bounds check.The text was updated successfully, but these errors were encountered: