aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/string.go
diff options
context:
space:
mode:
authorPaschalis Tsilias <paschalis.tsilias@gmail.com>2023-09-13 12:44:17 +0300
committerGopher Robot <gobot@golang.org>2024-09-10 21:20:57 +0000
commitfe69121bc538260cf91f11dab705335b690e51a3 (patch)
treed5962a3404521bd5bed263fad4156d462f6673c6 /src/runtime/string.go
parent3da4281df1b0c7ea11b524ff19fc2f409b8e58c0 (diff)
downloadgo-fe69121bc538260cf91f11dab705335b690e51a3.tar.xz
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>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r--src/runtime/string.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go
index d45888b7a8..3c34541ee8 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -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;