go/src/runtime/string.go

578 lines
13 KiB
Go
Raw Normal View History

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
import (
"internal/abi"
"internal/bytealg"
"internal/goarch"
"unsafe"
)
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
// The constant is known to the compiler.
// There is no fundamental theory behind this number.
const tmpStringBufSize = 32
type tmpBuf [tmpStringBufSize]byte
// concatstrings implements a Go string concatenation x+y+z+...
// The operands are passed in the slice a.
// If buf != nil, the compiler has determined that the result does not
// escape the calling function, so the string data can be stored in buf
// if small enough.
func concatstrings(buf *tmpBuf, a []string) string {
idx := 0
l := 0
count := 0
for i, x := range a {
n := len(x)
if n == 0 {
continue
}
if l+n < l {
throw("string concatenation too long")
}
l += n
count++
idx = i
}
if count == 0 {
return ""
}
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
// If there is just one string and either it is not on the stack
// or our result does not escape the calling frame (buf != nil),
// then we can return that string directly.
if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
return a[idx]
}
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
s, b := rawstringtmp(buf, l)
for _, x := range a {
copy(b, x)
b = b[len(x):]
}
return s
}
func concatstring2(buf *tmpBuf, a0, a1 string) string {
return concatstrings(buf, []string{a0, a1})
}
func concatstring3(buf *tmpBuf, a0, a1, a2 string) string {
return concatstrings(buf, []string{a0, a1, a2})
}
func concatstring4(buf *tmpBuf, a0, a1, a2, a3 string) string {
return concatstrings(buf, []string{a0, a1, a2, a3})
}
func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
return concatstrings(buf, []string{a0, a1, a2, a3, a4})
}
// slicebytetostring converts a byte slice to a string.
// It is inserted by the compiler into generated code.
// ptr is a pointer to the first element of the slice;
// n is the length of the slice.
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
// Buf is a fixed-size buffer for the result,
// it is not nil if the result does not escape.
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) string {
if n == 0 {
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
// Turns out to be a relatively common case.
// Consider that you want to parse out data between parens in "foo()bar",
// you find the indices and convert the subslice to string.
return ""
}
if raceenabled {
racereadrangepc(unsafe.Pointer(ptr),
uintptr(n),
getcallerpc(),
abi.FuncPCABIInternal(slicebytetostring))
}
if msanenabled {
msanread(unsafe.Pointer(ptr), uintptr(n))
}
if asanenabled {
asanread(unsafe.Pointer(ptr), uintptr(n))
}
if n == 1 {
p := unsafe.Pointer(&staticuint64s[*ptr])
if goarch.BigEndian {
p = add(p, 7)
}
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
return unsafe.String((*byte)(p), 1)
}
var p unsafe.Pointer
if buf != nil && n <= len(buf) {
p = unsafe.Pointer(buf)
} else {
p = mallocgc(uintptr(n), nil, false)
}
memmove(p, unsafe.Pointer(ptr), uintptr(n))
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
return unsafe.String((*byte)(p), n)
}
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
// stringDataOnStack reports whether the string's data is
// stored on the current goroutine's stack.
func stringDataOnStack(s string) bool {
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
ptr := uintptr(unsafe.Pointer(unsafe.StringData(s)))
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
stk := getg().stack
return stk.lo <= ptr && ptr < stk.hi
}
func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
if buf != nil && l <= len(buf) {
b = buf[:l]
s = slicebytetostringtmp(&b[0], len(b))
cmd/gc: allocate buffers for non-escaped strings on stack Currently we always allocate string buffers in heap. For example, in the following code we allocate a temp string just for comparison: if string(byteSlice) == "abc" { ... } This change extends escape analysis to cover []byte->string conversions and string concatenation. If the result of operations does not escape, compiler allocates a small buffer on stack and passes it to slicebytetostring and concatstrings. Then runtime uses the buffer if the result fits into it. Size of the buffer is 32 bytes. There is no fundamental theory behind this number. Just an observation that on std lib tests/benchmarks frequency of string allocation is inversely proportional to string length; and there is significant number of allocations up to length 32. benchmark old allocs new allocs delta BenchmarkFprintfBytes 2 1 -50.00% BenchmarkDecodeComplex128Slice 318 316 -0.63% BenchmarkDecodeFloat64Slice 318 316 -0.63% BenchmarkDecodeInt32Slice 318 316 -0.63% BenchmarkDecodeStringSlice 2318 2316 -0.09% BenchmarkStripTags 11 5 -54.55% BenchmarkDecodeGray 111 102 -8.11% BenchmarkDecodeNRGBAGradient 200 188 -6.00% BenchmarkDecodeNRGBAOpaque 165 152 -7.88% BenchmarkDecodePaletted 319 309 -3.13% BenchmarkDecodeRGB 166 157 -5.42% BenchmarkDecodeInterlacing 279 268 -3.94% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% BenchmarkGoLookupIPWithBrokenNameServer 245 226 -7.76% BenchmarkClientServerParallel4 62 61 -1.61% BenchmarkClientServerParallel64 62 61 -1.61% BenchmarkClientServerParallelTLS4 79 78 -1.27% BenchmarkClientServerParallelTLS64 112 111 -0.89% benchmark old ns/op new ns/op delta BenchmarkFprintfBytes 381 311 -18.37% BenchmarkStripTags 2615 2351 -10.10% BenchmarkDecodeNRGBAGradient 3715887 3635096 -2.17% BenchmarkDecodeNRGBAOpaque 3047645 2928644 -3.90% BenchmarkGoLookupIP 153 135 -11.76% BenchmarkGoLookupIPNoSuchHost 508 466 -8.27% Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99 Reviewed-on: https://go-review.googlesource.com/3120 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-21 17:37:59 +03:00
} else {
s, b = rawstring(l)
}
return
}
// slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
//
// Callers need to ensure that the returned string will not be used after
// the calling goroutine modifies the original slice or synchronizes with
// another goroutine.
//
// The function is only called when instrumenting
// and otherwise intrinsified by the compiler.
//
// Some internal compiler optimizations use this function.
// - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
// where k is []byte, T1 to Tn is a nesting of struct and array literals.
// - Used for "<"+string(b)+">" concatenation where b is []byte.
// - Used for string(b)=="foo" comparison where b is []byte.
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
func slicebytetostringtmp(ptr *byte, n int) string {
if raceenabled && n > 0 {
racereadrangepc(unsafe.Pointer(ptr),
uintptr(n),
getcallerpc(),
abi.FuncPCABIInternal(slicebytetostringtmp))
}
if msanenabled && n > 0 {
msanread(unsafe.Pointer(ptr), uintptr(n))
}
if asanenabled && n > 0 {
asanread(unsafe.Pointer(ptr), uintptr(n))
}
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
return unsafe.String(ptr, n)
}
func stringtoslicebyte(buf *tmpBuf, s string) []byte {
var b []byte
if buf != nil && len(s) <= len(buf) {
*buf = tmpBuf{}
b = buf[:len(s)]
} else {
b = rawbyteslice(len(s))
}
copy(b, s)
return b
}
func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
// two passes.
// unlike slicerunetostring, no race because strings are immutable.
n := 0
cmd/compile: improve string iteration performance Generate a for loop for ranging over strings that only needs to call the runtime function charntorune for non ASCII characters. This provides faster iteration over ASCII characters and slightly faster iteration for other characters. The runtime function charntorune is changed to take an index from where to start decoding and returns the index after the last byte belonging to the decoded rune. All call sites of charntorune in the runtime are replaced by a for loop that will be transformed by the compiler instead of calling the charntorune function directly. go binary size decreases by 80 bytes. godoc binary size increases by around 4 kilobytes. runtime: name old time/op new time/op delta RuneIterate/range/ASCII-4 43.7ns ± 3% 10.3ns ± 4% -76.33% (p=0.000 n=44+45) RuneIterate/range/Japanese-4 72.5ns ± 2% 62.8ns ± 2% -13.41% (p=0.000 n=49+50) RuneIterate/range1/ASCII-4 43.5ns ± 2% 10.4ns ± 3% -76.18% (p=0.000 n=50+50) RuneIterate/range1/Japanese-4 72.5ns ± 2% 62.9ns ± 2% -13.26% (p=0.000 n=50+49) RuneIterate/range2/ASCII-4 43.5ns ± 3% 10.3ns ± 2% -76.22% (p=0.000 n=48+47) RuneIterate/range2/Japanese-4 72.4ns ± 2% 62.7ns ± 2% -13.47% (p=0.000 n=50+50) strings: name old time/op new time/op delta IndexRune-4 64.7ns ± 5% 22.4ns ± 3% -65.43% (p=0.000 n=25+21) MapNoChanges-4 269ns ± 2% 157ns ± 2% -41.46% (p=0.000 n=23+24) Fields-4 23.0ms ± 2% 19.7ms ± 2% -14.35% (p=0.000 n=25+25) FieldsFunc-4 23.1ms ± 2% 19.6ms ± 2% -14.94% (p=0.000 n=25+24) name old speed new speed delta Fields-4 45.6MB/s ± 2% 53.2MB/s ± 2% +16.87% (p=0.000 n=24+25) FieldsFunc-4 45.5MB/s ± 2% 53.5MB/s ± 2% +17.57% (p=0.000 n=25+24) Updates #13162 Change-Id: I79ffaf828d82bf9887592f08e5cad883e9f39701 Reviewed-on: https://go-review.googlesource.com/27853 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Martin Möhrmann <martisch@uos.de>
2016-08-26 15:00:46 +02:00
for range s {
n++
}
cmd/compile: improve string iteration performance Generate a for loop for ranging over strings that only needs to call the runtime function charntorune for non ASCII characters. This provides faster iteration over ASCII characters and slightly faster iteration for other characters. The runtime function charntorune is changed to take an index from where to start decoding and returns the index after the last byte belonging to the decoded rune. All call sites of charntorune in the runtime are replaced by a for loop that will be transformed by the compiler instead of calling the charntorune function directly. go binary size decreases by 80 bytes. godoc binary size increases by around 4 kilobytes. runtime: name old time/op new time/op delta RuneIterate/range/ASCII-4 43.7ns ± 3% 10.3ns ± 4% -76.33% (p=0.000 n=44+45) RuneIterate/range/Japanese-4 72.5ns ± 2% 62.8ns ± 2% -13.41% (p=0.000 n=49+50) RuneIterate/range1/ASCII-4 43.5ns ± 2% 10.4ns ± 3% -76.18% (p=0.000 n=50+50) RuneIterate/range1/Japanese-4 72.5ns ± 2% 62.9ns ± 2% -13.26% (p=0.000 n=50+49) RuneIterate/range2/ASCII-4 43.5ns ± 3% 10.3ns ± 2% -76.22% (p=0.000 n=48+47) RuneIterate/range2/Japanese-4 72.4ns ± 2% 62.7ns ± 2% -13.47% (p=0.000 n=50+50) strings: name old time/op new time/op delta IndexRune-4 64.7ns ± 5% 22.4ns ± 3% -65.43% (p=0.000 n=25+21) MapNoChanges-4 269ns ± 2% 157ns ± 2% -41.46% (p=0.000 n=23+24) Fields-4 23.0ms ± 2% 19.7ms ± 2% -14.35% (p=0.000 n=25+25) FieldsFunc-4 23.1ms ± 2% 19.6ms ± 2% -14.94% (p=0.000 n=25+24) name old speed new speed delta Fields-4 45.6MB/s ± 2% 53.2MB/s ± 2% +16.87% (p=0.000 n=24+25) FieldsFunc-4 45.5MB/s ± 2% 53.5MB/s ± 2% +17.57% (p=0.000 n=25+24) Updates #13162 Change-Id: I79ffaf828d82bf9887592f08e5cad883e9f39701 Reviewed-on: https://go-review.googlesource.com/27853 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Martin Möhrmann <martisch@uos.de>
2016-08-26 15:00:46 +02:00
var a []rune
if buf != nil && n <= len(buf) {
*buf = [tmpStringBufSize]rune{}
a = buf[:n]
} else {
a = rawruneslice(n)
}
cmd/compile: improve string iteration performance Generate a for loop for ranging over strings that only needs to call the runtime function charntorune for non ASCII characters. This provides faster iteration over ASCII characters and slightly faster iteration for other characters. The runtime function charntorune is changed to take an index from where to start decoding and returns the index after the last byte belonging to the decoded rune. All call sites of charntorune in the runtime are replaced by a for loop that will be transformed by the compiler instead of calling the charntorune function directly. go binary size decreases by 80 bytes. godoc binary size increases by around 4 kilobytes. runtime: name old time/op new time/op delta RuneIterate/range/ASCII-4 43.7ns ± 3% 10.3ns ± 4% -76.33% (p=0.000 n=44+45) RuneIterate/range/Japanese-4 72.5ns ± 2% 62.8ns ± 2% -13.41% (p=0.000 n=49+50) RuneIterate/range1/ASCII-4 43.5ns ± 2% 10.4ns ± 3% -76.18% (p=0.000 n=50+50) RuneIterate/range1/Japanese-4 72.5ns ± 2% 62.9ns ± 2% -13.26% (p=0.000 n=50+49) RuneIterate/range2/ASCII-4 43.5ns ± 3% 10.3ns ± 2% -76.22% (p=0.000 n=48+47) RuneIterate/range2/Japanese-4 72.4ns ± 2% 62.7ns ± 2% -13.47% (p=0.000 n=50+50) strings: name old time/op new time/op delta IndexRune-4 64.7ns ± 5% 22.4ns ± 3% -65.43% (p=0.000 n=25+21) MapNoChanges-4 269ns ± 2% 157ns ± 2% -41.46% (p=0.000 n=23+24) Fields-4 23.0ms ± 2% 19.7ms ± 2% -14.35% (p=0.000 n=25+25) FieldsFunc-4 23.1ms ± 2% 19.6ms ± 2% -14.94% (p=0.000 n=25+24) name old speed new speed delta Fields-4 45.6MB/s ± 2% 53.2MB/s ± 2% +16.87% (p=0.000 n=24+25) FieldsFunc-4 45.5MB/s ± 2% 53.5MB/s ± 2% +17.57% (p=0.000 n=25+24) Updates #13162 Change-Id: I79ffaf828d82bf9887592f08e5cad883e9f39701 Reviewed-on: https://go-review.googlesource.com/27853 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Martin Möhrmann <martisch@uos.de>
2016-08-26 15:00:46 +02:00
n = 0
cmd/compile: improve string iteration performance Generate a for loop for ranging over strings that only needs to call the runtime function charntorune for non ASCII characters. This provides faster iteration over ASCII characters and slightly faster iteration for other characters. The runtime function charntorune is changed to take an index from where to start decoding and returns the index after the last byte belonging to the decoded rune. All call sites of charntorune in the runtime are replaced by a for loop that will be transformed by the compiler instead of calling the charntorune function directly. go binary size decreases by 80 bytes. godoc binary size increases by around 4 kilobytes. runtime: name old time/op new time/op delta RuneIterate/range/ASCII-4 43.7ns ± 3% 10.3ns ± 4% -76.33% (p=0.000 n=44+45) RuneIterate/range/Japanese-4 72.5ns ± 2% 62.8ns ± 2% -13.41% (p=0.000 n=49+50) RuneIterate/range1/ASCII-4 43.5ns ± 2% 10.4ns ± 3% -76.18% (p=0.000 n=50+50) RuneIterate/range1/Japanese-4 72.5ns ± 2% 62.9ns ± 2% -13.26% (p=0.000 n=50+49) RuneIterate/range2/ASCII-4 43.5ns ± 3% 10.3ns ± 2% -76.22% (p=0.000 n=48+47) RuneIterate/range2/Japanese-4 72.4ns ± 2% 62.7ns ± 2% -13.47% (p=0.000 n=50+50) strings: name old time/op new time/op delta IndexRune-4 64.7ns ± 5% 22.4ns ± 3% -65.43% (p=0.000 n=25+21) MapNoChanges-4 269ns ± 2% 157ns ± 2% -41.46% (p=0.000 n=23+24) Fields-4 23.0ms ± 2% 19.7ms ± 2% -14.35% (p=0.000 n=25+25) FieldsFunc-4 23.1ms ± 2% 19.6ms ± 2% -14.94% (p=0.000 n=25+24) name old speed new speed delta Fields-4 45.6MB/s ± 2% 53.2MB/s ± 2% +16.87% (p=0.000 n=24+25) FieldsFunc-4 45.5MB/s ± 2% 53.5MB/s ± 2% +17.57% (p=0.000 n=25+24) Updates #13162 Change-Id: I79ffaf828d82bf9887592f08e5cad883e9f39701 Reviewed-on: https://go-review.googlesource.com/27853 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Martin Möhrmann <martisch@uos.de>
2016-08-26 15:00:46 +02:00
for _, r := range s {
a[n] = r
n++
}
return a
}
func slicerunetostring(buf *tmpBuf, a []rune) string {
if raceenabled && len(a) > 0 {
racereadrangepc(unsafe.Pointer(&a[0]),
uintptr(len(a))*unsafe.Sizeof(a[0]),
getcallerpc(),
abi.FuncPCABIInternal(slicerunetostring))
}
if msanenabled && len(a) > 0 {
msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
}
if asanenabled && len(a) > 0 {
asanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
}
var dum [4]byte
size1 := 0
for _, r := range a {
size1 += encoderune(dum[:], r)
}
s, b := rawstringtmp(buf, size1+3)
size2 := 0
for _, r := range a {
// check for race
if size2 >= size1 {
break
}
size2 += encoderune(b[size2:], r)
}
return s[:size2]
}
type stringStruct struct {
str unsafe.Pointer
len int
}
// Variant with *byte pointer type for DWARF debugging.
type stringStructDWARF struct {
str *byte
len int
}
func stringStructOf(sp *string) *stringStruct {
return (*stringStruct)(unsafe.Pointer(sp))
}
func intstring(buf *[4]byte, v int64) (s string) {
var b []byte
if buf != nil {
b = buf[:]
s = slicebytetostringtmp(&b[0], len(b))
} else {
s, b = rawstring(4)
}
if int64(rune(v)) != v {
v = runeError
}
n := encoderune(b, rune(v))
return s[:n]
}
// rawstring allocates storage for a new string. The returned
// string and byte slice both refer to the same storage.
// The storage is not zeroed. Callers should use
// b to set the string contents and then drop b.
func rawstring(size int) (s string, b []byte) {
p := mallocgc(uintptr(size), nil, false)
runtime: replace stringStruct with unsafe.String where appropriate Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-07 13:23:10 +07:00
return unsafe.String((*byte)(p), size), unsafe.Slice((*byte)(p), size)
}
// rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
func rawbyteslice(size int) (b []byte) {
cap := roundupsize(uintptr(size))
p := mallocgc(cap, nil, false)
if cap != uintptr(size) {
memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
}
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
return
}
// rawruneslice allocates a new rune slice. The rune slice is not zeroed.
func rawruneslice(size int) (b []rune) {
if uintptr(size) > maxAlloc/4 {
throw("out of memory")
}
mem := roundupsize(uintptr(size) * 4)
p := mallocgc(mem, nil, false)
if mem != uintptr(size)*4 {
memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
}
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
return
}
// used by cmd/cgo
func gobytes(p *byte, n int) (b []byte) {
if n == 0 {
return make([]byte, 0)
}
if n < 0 || uintptr(n) > maxAlloc {
panic(errorString("gobytes: length out of range"))
}
bp := mallocgc(uintptr(n), nil, false)
memmove(bp, unsafe.Pointer(p), uintptr(n))
*(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
return
}
// This is exported via linkname to assembly in syscall (for Plan9).
//
//go:linkname gostring
func gostring(p *byte) string {
l := findnull(p)
if l == 0 {
return ""
}
s, b := rawstring(l)
memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
return s
}
func gostringn(p *byte, l int) string {
if l == 0 {
return ""
}
s, b := rawstring(l)
memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
return s
}
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
const (
maxUint64 = ^uint64(0)
maxInt64 = int64(maxUint64 >> 1)
)
// atoi64 parses an int64 from a string s.
// The bool result reports whether s is a number
// representable by a value of type int64.
func atoi64(s string) (int64, bool) {
if s == "" {
return 0, false
}
neg := false
if s[0] == '-' {
neg = true
s = s[1:]
}
un := uint64(0)
for i := 0; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
return 0, false
}
if un > maxUint64/10 {
// overflow
return 0, false
}
un *= 10
un1 := un + uint64(c) - '0'
if un1 < un {
// overflow
return 0, false
}
un = un1
}
if !neg && un > uint64(maxInt64) {
return 0, false
}
if neg && un > uint64(maxInt64)+1 {
return 0, false
}
n := int64(un)
if neg {
n = -n
}
return n, true
}
// atoi is like atoi64 but for integers
// that fit into an int.
func atoi(s string) (int, bool) {
if n, ok := atoi64(s); n == int64(int(n)) {
return int(n), ok
}
return 0, false
}
// atoi32 is like atoi but for integers
// that fit into an int32.
func atoi32(s string) (int32, bool) {
if n, ok := atoi64(s); n == int64(int32(n)) {
return int32(n), ok
}
return 0, false
}
// parseByteCount parses a string that represents a count of bytes.
//
// s must match the following regular expression:
//
// ^[0-9]+(([KMGT]i)?B)?$
//
// In other words, an integer byte count with an optional unit
// suffix. Acceptable suffixes include one of
// - KiB, MiB, GiB, TiB which represent binary IEC/ISO 80000 units, or
// - B, which just represents bytes.
//
// Returns an int64 because that's what its callers want and receive,
// but the result is always non-negative.
func parseByteCount(s string) (int64, bool) {
// The empty string is not valid.
if s == "" {
return 0, false
}
// Handle the easy non-suffix case.
last := s[len(s)-1]
if last >= '0' && last <= '9' {
n, ok := atoi64(s)
if !ok || n < 0 {
return 0, false
}
return n, ok
}
// Failing a trailing digit, this must always end in 'B'.
// Also at this point there must be at least one digit before
// that B.
if last != 'B' || len(s) < 2 {
return 0, false
}
// The one before that must always be a digit or 'i'.
if c := s[len(s)-2]; c >= '0' && c <= '9' {
// Trivial 'B' suffix.
n, ok := atoi64(s[:len(s)-1])
if !ok || n < 0 {
return 0, false
}
return n, ok
} else if c != 'i' {
return 0, false
}
// Finally, we need at least 4 characters now, for the unit
// prefix and at least one digit.
if len(s) < 4 {
return 0, false
}
power := 0
switch s[len(s)-3] {
case 'K':
power = 1
case 'M':
power = 2
case 'G':
power = 3
case 'T':
power = 4
default:
// Invalid suffix.
return 0, false
}
m := uint64(1)
for i := 0; i < power; i++ {
m *= 1024
}
n, ok := atoi64(s[:len(s)-3])
if !ok || n < 0 {
return 0, false
}
un := uint64(n)
if un > maxUint64/m {
// Overflow.
return 0, false
}
un *= m
if un > uint64(maxInt64) {
// Overflow.
return 0, false
}
return int64(un), true
}
//go:nosplit
func findnull(s *byte) int {
if s == nil {
return 0
}
// Avoid IndexByteString on Plan 9 because it uses SSE instructions
// on x86 machines, and those are classified as floating point instructions,
// which are illegal in a note handler.
if GOOS == "plan9" {
p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
l := 0
for p[l] != 0 {
l++
}
return l
}
// pageSize is the unit we scan at a time looking for NULL.
// It must be the minimum page size for any architecture Go
// runs on. It's okay (just a minor performance loss) if the
// actual system page size is larger than this value.
const pageSize = 4096
offset := 0
ptr := unsafe.Pointer(s)
// IndexByteString uses wide reads, so we need to be careful
// with page boundaries. Call IndexByteString on
// [ptr, endOfPage) interval.
safeLen := int(pageSize - uintptr(ptr)%pageSize)
for {
t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
// Check one page at a time.
if i := bytealg.IndexByteString(t, 0); i != -1 {
return offset + i
}
// Move to next page
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
offset += safeLen
safeLen = pageSize
}
}
func findnullw(s *uint16) int {
if s == nil {
return 0
}
p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
l := 0
for p[l] != 0 {
l++
}
return l
}
//go:nosplit
func gostringnocopy(str *byte) string {
ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
s := *(*string)(unsafe.Pointer(&ss))
return s
}
func gostringw(strw *uint16) string {
var buf [8]byte
str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
n1 := 0
for i := 0; str[i] != 0; i++ {
n1 += encoderune(buf[:], rune(str[i]))
}
s, b := rawstring(n1 + 4)
n2 := 0
for i := 0; str[i] != 0; i++ {
// check for race
if n2 >= n1 {
break
}
n2 += encoderune(b[n2:], rune(str[i]))
}
b[n2] = 0 // for luck
return s[:n2]
}