mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile,runtime: pass only ptr and len to some runtime calls
Some runtime calls accept a slice, but only use ptr and len. This change modifies most such routines to accept only ptr and len. After this change, the only runtime calls that accept an unnecessary cap arg are concatstrings and slicerunetostring. Neither is particularly common, and both are complicated to modify. Negligible compiler performance impact. Shrinks binaries a little. There are only a few regressions; the one I investigated was due to register allocation fluctuation. Passes 'go test -race std cmd', modulo #38265 and #38266. Wow, does that take a long time to run. Updates #36890 file before after Δ % compile 19655024 19655152 +128 +0.001% cover 5244840 5236648 -8192 -0.156% dist 3662376 3658280 -4096 -0.112% link 6680056 6675960 -4096 -0.061% pprof 14789844 14777556 -12288 -0.083% test2json 2824744 2820648 -4096 -0.145% trace 11647876 11639684 -8192 -0.070% vet 8260472 8256376 -4096 -0.050% total 115163736 115118808 -44928 -0.039% Change-Id: Idb29fa6a81d6a82bfd3b65740b98cf3275ca0a78 Reviewed-on: https://go-review.googlesource.com/c/go/+/227163 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
parent
ade0811dc8
commit
b6feb03b24
10 changed files with 153 additions and 109 deletions
|
|
@ -71,27 +71,30 @@ func concatstring5(buf *tmpBuf, a [5]string) string {
|
|||
return concatstrings(buf, a[:])
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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) (str string) {
|
||||
l := len(b)
|
||||
if l == 0 {
|
||||
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
|
||||
if n == 0 {
|
||||
// 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(&b[0]),
|
||||
uintptr(l),
|
||||
racereadrangepc(unsafe.Pointer(ptr),
|
||||
uintptr(n),
|
||||
getcallerpc(),
|
||||
funcPC(slicebytetostring))
|
||||
}
|
||||
if msanenabled {
|
||||
msanread(unsafe.Pointer(&b[0]), uintptr(l))
|
||||
msanread(unsafe.Pointer(ptr), uintptr(n))
|
||||
}
|
||||
if l == 1 {
|
||||
p := unsafe.Pointer(&staticuint64s[b[0]])
|
||||
if n == 1 {
|
||||
p := unsafe.Pointer(&staticuint64s[*ptr])
|
||||
if sys.BigEndian {
|
||||
p = add(p, 7)
|
||||
}
|
||||
|
|
@ -101,14 +104,14 @@ func slicebytetostring(buf *tmpBuf, b []byte) (str string) {
|
|||
}
|
||||
|
||||
var p unsafe.Pointer
|
||||
if buf != nil && len(b) <= len(buf) {
|
||||
if buf != nil && n <= len(buf) {
|
||||
p = unsafe.Pointer(buf)
|
||||
} else {
|
||||
p = mallocgc(uintptr(len(b)), nil, false)
|
||||
p = mallocgc(uintptr(n), nil, false)
|
||||
}
|
||||
stringStructOf(&str).str = p
|
||||
stringStructOf(&str).len = len(b)
|
||||
memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
|
||||
stringStructOf(&str).len = n
|
||||
memmove(p, unsafe.Pointer(ptr), uintptr(n))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -123,7 +126,7 @@ func stringDataOnStack(s string) bool {
|
|||
func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
|
||||
if buf != nil && l <= len(buf) {
|
||||
b = buf[:l]
|
||||
s = slicebytetostringtmp(b)
|
||||
s = slicebytetostringtmp(&b[0], len(b))
|
||||
} else {
|
||||
s, b = rawstring(l)
|
||||
}
|
||||
|
|
@ -144,17 +147,19 @@ func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
|
|||
// 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.
|
||||
func slicebytetostringtmp(b []byte) string {
|
||||
if raceenabled && len(b) > 0 {
|
||||
racereadrangepc(unsafe.Pointer(&b[0]),
|
||||
uintptr(len(b)),
|
||||
func slicebytetostringtmp(ptr *byte, n int) (str string) {
|
||||
if raceenabled && n > 0 {
|
||||
racereadrangepc(unsafe.Pointer(ptr),
|
||||
uintptr(n),
|
||||
getcallerpc(),
|
||||
funcPC(slicebytetostringtmp))
|
||||
}
|
||||
if msanenabled && len(b) > 0 {
|
||||
msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
|
||||
if msanenabled && n > 0 {
|
||||
msanread(unsafe.Pointer(ptr), uintptr(n))
|
||||
}
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
stringStructOf(&str).str = unsafe.Pointer(ptr)
|
||||
stringStructOf(&str).len = n
|
||||
return
|
||||
}
|
||||
|
||||
func stringtoslicebyte(buf *tmpBuf, s string) []byte {
|
||||
|
|
@ -239,7 +244,7 @@ func intstring(buf *[4]byte, v int64) (s string) {
|
|||
var b []byte
|
||||
if buf != nil {
|
||||
b = buf[:]
|
||||
s = slicebytetostringtmp(b)
|
||||
s = slicebytetostringtmp(&b[0], len(b))
|
||||
} else {
|
||||
s, b = rawstring(4)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue