]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: don't allocate to build strings of length 1
authorJosh Bleecher Snyder <josharian@gmail.com>
Sun, 5 Mar 2017 00:55:03 +0000 (16:55 -0800)
committerKeith Randall <khr@golang.org>
Thu, 1 Mar 2018 17:38:06 +0000 (17:38 +0000)
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>
src/runtime/string.go

index cfe2959b362cfc03717f8f04cc1c45403b3f0973..5c838959953b3e1d8d172a278f378dd905312a30 100644 (file)
@@ -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) {