aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/string.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-02-03 20:50:58 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-02-04 04:37:21 +0000
commit71be0138421012d04e06991d37d19c9f5b1fa02b (patch)
tree6a5d6528e0e47d9b71ecfd3da9678a0b24517451 /src/runtime/string.go
parent70321df02fcd3fac12ea650ffae7130fe7cd379f (diff)
downloadgo-71be0138421012d04e06991d37d19c9f5b1fa02b.tar.gz
go-71be0138421012d04e06991d37d19c9f5b1fa02b.zip
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>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r--src/runtime/string.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go
index 58198d0e1b..46c3502f77 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -135,6 +135,18 @@ func stringtoslicebyte(s string) []byte {
return b
}
+func stringtoslicebytetmp(s string) []byte {
+ // Return a slice referring to the actual string bytes.
+ // This is only for use by internal compiler optimizations
+ // that know that the slice won't be mutated.
+ // The only such case today is:
+ // for i, c := range []byte(str)
+
+ str := (*stringStruct)(unsafe.Pointer(&s))
+ ret := slice{array: (*byte)(str.str), len: uint(str.len), cap: uint(str.len)}
+ return *(*[]byte)(unsafe.Pointer(&ret))
+}
+
func stringtoslicerune(s string) []rune {
// two passes.
// unlike slicerunetostring, no race because strings are immutable.