diff options
author | Josh Bleecher Snyder <josharian@gmail.com> | 2017-03-04 16:55:03 -0800 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2018-03-01 17:38:06 +0000 |
commit | 9372e3f5ef6c9653d29cbba2dc06bdcad2b3724c (patch) | |
tree | 5e6ccf2e6cc1e95698d73881ec6682f1d3830349 /src/runtime/string.go | |
parent | aa9c1a8f8038e88da6d7cbfdc56e34ff914b7b04 (diff) | |
download | go-9372e3f5ef6c9653d29cbba2dc06bdcad2b3724c.tar.gz go-9372e3f5ef6c9653d29cbba2dc06bdcad2b3724c.zip |
runtime: don't allocate to build strings of length 1
Use staticbytes instead.
Instrumenting make.bash shows approx 0.5%
of all slicebytetostrings have a buffer of length 1.
name old time/op new time/op delta
SliceByteToString/1-8 14.1ns ± 1% 4.1ns ± 1% -71.13% (p=0.000 n=17+20)
SliceByteToString/2-8 15.5ns ± 2% 15.5ns ± 1% ~ (p=0.061 n=20+18)
SliceByteToString/4-8 14.9ns ± 1% 15.0ns ± 2% +1.25% (p=0.000 n=20+20)
SliceByteToString/8-8 17.1ns ± 1% 17.5ns ± 1% +2.16% (p=0.000 n=19+19)
SliceByteToString/16-8 23.6ns ± 1% 23.9ns ± 1% +1.41% (p=0.000 n=20+18)
SliceByteToString/32-8 26.0ns ± 1% 25.8ns ± 0% -1.05% (p=0.000 n=19+16)
SliceByteToString/64-8 30.0ns ± 0% 30.2ns ± 0% +0.56% (p=0.000 n=16+18)
SliceByteToString/128-8 38.9ns ± 0% 39.0ns ± 0% +0.23% (p=0.019 n=19+15)
Fixes #24172
Change-Id: I3dfa14eefbf9fb4387114e20c9cb40e186abe962
Reviewed-on: https://go-review.googlesource.com/97717
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r-- | src/runtime/string.go | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go index cfe2959b36..5c83895995 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -86,6 +86,11 @@ func slicebytetostring(buf *tmpBuf, b []byte) (str string) { if msanenabled { msanread(unsafe.Pointer(&b[0]), uintptr(l)) } + if l == 1 { + stringStructOf(&str).str = unsafe.Pointer(&staticbytes[b[0]]) + stringStructOf(&str).len = 1 + return + } var p unsafe.Pointer if buf != nil && len(b) <= len(buf) { |