cmd/compile: optimize []byte(string1 + string2)

This CL optimizes the compilation of string-to-bytes conversion in the
case of string additions.

Fixes #62407

Change-Id: Ic47df758478e5d061880620025c4ec7dbbff8a64
Reviewed-on: https://go-review.googlesource.com/c/go/+/527935
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Tim King <taking@google.com>
This commit is contained in:
Paschalis Tsilias 2023-09-13 12:44:17 +03:00 committed by Gopher Robot
parent 3da4281df1
commit fe69121bc5
8 changed files with 438 additions and 287 deletions

View file

@ -72,6 +72,49 @@ func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
return concatstrings(buf, []string{a0, a1, a2, a3, a4})
}
// concatbytes implements a Go string concatenation x+y+z+... returning a slice
// of bytes.
// The operands are passed in the slice a.
func concatbytes(a []string) []byte {
l := 0
for _, x := range a {
n := len(x)
if l+n < l {
throw("string concatenation too long")
}
l += n
}
if l == 0 {
// This is to match the return type of the non-optimized concatenation.
return []byte{}
}
b := rawbyteslice(l)
offset := 0
for _, x := range a {
copy(b[offset:], x)
offset += len(x)
}
return b
}
func concatbyte2(a0, a1 string) []byte {
return concatbytes([]string{a0, a1})
}
func concatbyte3(a0, a1, a2 string) []byte {
return concatbytes([]string{a0, a1, a2})
}
func concatbyte4(a0, a1, a2, a3 string) []byte {
return concatbytes([]string{a0, a1, a2, a3})
}
func concatbyte5(a0, a1, a2, a3, a4 string) []byte {
return concatbytes([]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;