aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/string.go
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2017-03-04 16:54:50 -0800
committerJosh Bleecher Snyder <josharian@gmail.com>2017-03-08 22:05:52 +0000
commit23be728950be7973a4c4852449e1987c64dc2739 (patch)
tree24017f1e5ac37c951982858020000bec6adfd6b9 /src/runtime/string.go
parent228438e0973b688829fdd601e31352920a5914f4 (diff)
downloadgo-23be728950be7973a4c4852449e1987c64dc2739.tar.gz
go-23be728950be7973a4c4852449e1987c64dc2739.zip
runtime: optimize slicebytestostring
Inline rawstringtmp and simplify. Use memmove instead of copy. name old time/op new time/op delta SliceByteToString/1-8 19.4ns ± 2% 14.1ns ± 1% -27.04% (p=0.000 n=20+17) SliceByteToString/2-8 20.8ns ± 2% 15.5ns ± 2% -25.46% (p=0.000 n=20+20) SliceByteToString/4-8 20.7ns ± 1% 14.9ns ± 1% -28.30% (p=0.000 n=20+20) SliceByteToString/8-8 23.2ns ± 1% 17.1ns ± 1% -26.22% (p=0.000 n=19+19) SliceByteToString/16-8 29.4ns ± 1% 23.6ns ± 1% -19.76% (p=0.000 n=17+20) SliceByteToString/32-8 31.4ns ± 1% 26.0ns ± 1% -17.11% (p=0.000 n=16+19) SliceByteToString/64-8 36.1ns ± 0% 30.0ns ± 0% -16.96% (p=0.000 n=16+16) SliceByteToString/128-8 46.9ns ± 0% 38.9ns ± 0% -17.15% (p=0.000 n=17+19) Change-Id: I422e688830e4a9bd21897d1f74964625b735f436 Reviewed-on: https://go-review.googlesource.com/37791 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marvin Stenger <marvin.stenger94@gmail.com> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r--src/runtime/string.go20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go
index 822adaacf1..0ccc81ee58 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -69,7 +69,7 @@ func concatstring5(buf *tmpBuf, a [5]string) string {
// Buf is a fixed-size buffer for the result,
// it is not nil if the result does not escape.
-func slicebytetostring(buf *tmpBuf, b []byte) string {
+func slicebytetostring(buf *tmpBuf, b []byte) (str string) {
l := len(b)
if l == 0 {
// Turns out to be a relatively common case.
@@ -77,18 +77,26 @@ func slicebytetostring(buf *tmpBuf, b []byte) string {
// you find the indices and convert the subslice to string.
return ""
}
- if raceenabled && l > 0 {
+ if raceenabled {
racereadrangepc(unsafe.Pointer(&b[0]),
uintptr(l),
getcallerpc(unsafe.Pointer(&buf)),
funcPC(slicebytetostring))
}
- if msanenabled && l > 0 {
+ if msanenabled {
msanread(unsafe.Pointer(&b[0]), uintptr(l))
}
- s, c := rawstringtmp(buf, l)
- copy(c, b)
- return s
+
+ var p unsafe.Pointer
+ if buf != nil && len(b) <= len(buf) {
+ p = unsafe.Pointer(buf)
+ } else {
+ p = mallocgc(uintptr(len(b)), nil, false)
+ }
+ stringStructOf(&str).str = p
+ stringStructOf(&str).len = len(b)
+ memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
+ return
}
// stringDataOnStack reports whether the string's data is