aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorKeith Randall <khr@google.com>2018-10-30 10:56:02 -0700
committerKeith Randall <khr@golang.org>2018-10-30 20:03:54 +0000
commit56b7c61c5e88e684c1bdb3b9ae61dadbeda96fd0 (patch)
tree9651f046cea2082ca590f82dac768170194039e2 /src/runtime
parentf14067f3c10e15343f29aed439ff60af856eb323 (diff)
downloadgo-56b7c61c5e88e684c1bdb3b9ae61dadbeda96fd0.tar.xz
strings: declare IndexByte as noescape
This lets []byte->string conversions which are used as arguments to strings.IndexByte and friends have their backing store allocated on the stack. It only prevents allocation when the string is small enough (32 bytes), so it isn't perfect. But reusing the []byte backing store directly requires a bunch more compiler analysis (see #2205 and related issues). Fixes #25864. Change-Id: Ie52430422196e3c91e5529d6e56a8435ced1fc4c Reviewed-on: https://go-review.googlesource.com/c/146018 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/string_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/runtime/string_test.go b/src/runtime/string_test.go
index 678ff00363..a1716fa32f 100644
--- a/src/runtime/string_test.go
+++ b/src/runtime/string_test.go
@@ -240,6 +240,34 @@ func TestCompareTempString(t *testing.T) {
}
}
+func TestStringIndexHaystack(t *testing.T) {
+ // See issue 25864.
+ haystack := []byte("hello")
+ needle := "ll"
+ n := testing.AllocsPerRun(1000, func() {
+ if strings.Index(string(haystack), needle) != 2 {
+ t.Fatalf("needle not found")
+ }
+ })
+ if n != 0 {
+ t.Fatalf("want 0 allocs, got %v", n)
+ }
+}
+
+func TestStringIndexNeedle(t *testing.T) {
+ // See issue 25864.
+ haystack := "hello"
+ needle := []byte("ll")
+ n := testing.AllocsPerRun(1000, func() {
+ if strings.Index(haystack, string(needle)) != 2 {
+ t.Fatalf("needle not found")
+ }
+ })
+ if n != 0 {
+ t.Fatalf("want 0 allocs, got %v", n)
+ }
+}
+
func TestStringOnStack(t *testing.T) {
s := ""
for i := 0; i < 3; i++ {