cmd/gc: don't copy string in range []byte(str)
Using benchmark from the issue:
benchmark old ns/op new ns/op delta
BenchmarkRangeStringCast 2162 1152 -46.72%
benchmark old allocs new allocs delta
BenchmarkRangeStringCast 1 0 -100.00%
Fixes #2204
Change-Id: I92c5edd2adca4a7b6fba00713a581bf49dc59afe
Reviewed-on: https://go-review.googlesource.com/3790
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/src/runtime/string_test.go b/src/runtime/string_test.go
index 27a44ad..dfda950 100644
--- a/src/runtime/string_test.go
+++ b/src/runtime/string_test.go
@@ -221,3 +221,17 @@
t.Fatalf("want 0 allocs, got %v", n)
}
}
+
+func TestRangeStringCast(t *testing.T) {
+ s := "abc"
+ n := testing.AllocsPerRun(1000, func() {
+ for i, c := range []byte(s) {
+ if c != s[i] {
+ t.Fatalf("want '%c' at pos %v, got '%c'", s[i], i, c)
+ }
+ }
+ })
+ if n != 0 {
+ t.Fatalf("want 0 allocs, got %v", n)
+ }
+}