-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: teach the compiler that arguments passed to {strings|bytes}.{Index*|Count|Has*} don't escape #25864
Comments
CC @dr2chase I don't think we want to start using |
I think it will be too difficult figuring out this for functions written in assembly. Probably, it would be OK adding |
This change may have significant positive impact on standard packages, since then escape analysis may prove that arguments to many functions written in Go that call |
Yes, sorry, it's fine to use |
go/src/internal/bytealg/indexbyte_native.go Lines 9 to 10 in a2f72cc
I suspect this is just a case where the string cast could be cleverer. |
I guess we could fix this, but why are you using |
Then the
This is just an artificial example :) |
The non-escaping
This is a preferred way for now, since prefix is usually smaller than haystack itself, and it can probably fit fixes 32-byte stack allocated buffer. |
Performance check. Suggests to replace strings.Index with bytes.Index where it can make code more efficient. See golang/go#25864
Performance check. Suggests to replace strings.Index with bytes.Index where it can make code more efficient. See golang/go#25864
Here is a simple benchmarking code: package foo
import (
"bytes"
"strings"
"testing"
)
var haystack = bytes.Repeat([]byte("a"), 100)
var needle = "aaa"
func BenchmarkStringsIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.Index(string(haystack), needle)
}
}
func BenchmarkBytesIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = bytes.Index(haystack, []byte(needle))
}
} The results:
I've written a simple check that detects such calls that can be replaced with |
Performance check. Suggests to replace strings.Index with bytes.Index where it can make code more efficient. See golang/go#25864
Change https://golang.org/cl/146018 mentions this issue: |
The CL I just mailed fixes |
The issue
Go tip doesn't know that
string
/[]byte
arguments passed to functions likestrings.IndexByte
don't escape. This leads to unnecessary allocation and copy in the following code:Benchmark results indicate an unnecessary allocation:
The solution
Mark
strings
andbytes
functions acceptingstring
/[]byte
asgo:noescape
in order to eliminate unnecessary allocations in the code above.The text was updated successfully, but these errors were encountered: