From: Josh Bleecher Snyder Date: Sun, 5 Mar 2017 00:55:03 +0000 (-0800) Subject: runtime: don't allocate to build strings of length 1 X-Git-Tag: go1.11beta1~1395 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9372e3f5ef6c9653d29cbba2dc06bdcad2b3724c;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- 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) {