aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/test
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/cmd/compile/internal/test
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/cmd/compile/internal/test')
-rw-r--r--src/cmd/compile/internal/test/issue62407_test.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/test/issue62407_test.go b/src/cmd/compile/internal/test/issue62407_test.go
new file mode 100644
index 0000000000..d065673627
--- /dev/null
+++ b/src/cmd/compile/internal/test/issue62407_test.go
@@ -0,0 +1,62 @@
+// Copyright 2024 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 test
+
+import (
+ "reflect"
+ "testing"
+)
+
+//go:noinline
+func foo() string { return "foo" }
+
+//go:noinline
+func empty() string { return "" }
+
+func TestConcatBytes(t *testing.T) {
+ empty := empty()
+ s := foo()
+ tests := map[string]struct {
+ got []byte
+ want []byte
+ }{
+ "two empty elements": {got: []byte(empty + empty), want: []byte{}},
+ "two nonempty elements": {got: []byte(s + s), want: []byte("foofoo")},
+ "one empty and one nonempty element": {got: []byte(s + empty), want: []byte("foo")},
+ "multiple empty elements": {got: []byte(empty + empty + empty + empty + empty + empty), want: []byte{}},
+ "multiple nonempty elements": {got: []byte("1" + "2" + "3" + "4" + "5" + "6"), want: []byte("123456")},
+ }
+
+ for name, test := range tests {
+ if !reflect.DeepEqual(test.got, test.want) {
+ t.Errorf("[%s] got: %s, want: %s", name, test.got, test.want)
+ }
+ }
+}
+
+func TestConcatBytesAllocations(t *testing.T) {
+ empty := empty()
+ s := foo()
+ tests := map[string]struct {
+ f func() []byte
+ allocs float64
+ }{
+ "two empty elements": {f: func() []byte { return []byte(empty + empty) }, allocs: 0},
+ "multiple empty elements": {f: func() []byte { return []byte(empty + empty + empty + empty + empty + empty) }, allocs: 0},
+
+ "two elements": {f: func() []byte { return []byte(s + s) }, allocs: 1},
+ "three elements": {f: func() []byte { return []byte(s + s + s) }, allocs: 1},
+ "four elements": {f: func() []byte { return []byte(s + s + s + s) }, allocs: 1},
+ "five elements": {f: func() []byte { return []byte(s + s + s + s + s) }, allocs: 1},
+ "one empty and one nonempty element": {f: func() []byte { return []byte(s + empty) }, allocs: 1},
+ "two empty and two nonempty element": {f: func() []byte { return []byte(s + empty + s + empty) }, allocs: 1},
+ }
+ for name, test := range tests {
+ allocs := testing.AllocsPerRun(100, func() { test.f() })
+ if allocs != test.allocs {
+ t.Errorf("concatbytes [%s]: %v allocs, want %v", name, allocs, test.allocs)
+ }
+ }
+}