aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/string_test.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-01-27 23:57:12 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-01-28 19:36:50 +0000
commit69cd91a5981c49eaaa59b33196bdb5586c18d289 (patch)
tree1fcf616422ad0317f5870c49fbff5d64c8ae932c /src/runtime/string_test.go
parent4737399bd99e5221150f508de1125cc00d329f60 (diff)
downloadgo-69cd91a5981c49eaaa59b33196bdb5586c18d289.tar.xz
cmd/gc: don't copy []byte during string comparison
Currently we allocate a new string during []byte->string conversion in string comparison expressions. String allocation is unnecessary in this case, because comparison does memorize the strings for later use. This change uses slicebytetostringtmp to construct temp string directly from []byte buffer and passes it to runtime.eqstring. Change-Id: If00f1faaee2076baa6f6724d245d5b5e0f59b563 Reviewed-on: https://go-review.googlesource.com/3410 Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/runtime/string_test.go')
-rw-r--r--src/runtime/string_test.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/runtime/string_test.go b/src/runtime/string_test.go
index 1551ecc82b..03c8948467 100644
--- a/src/runtime/string_test.go
+++ b/src/runtime/string_test.go
@@ -158,3 +158,20 @@ func TestGostringnocopy(t *testing.T) {
t.Errorf("want %d, got %d", max+9, newmax)
}
}
+
+func TestCompareTempString(t *testing.T) {
+ s := "foo"
+ b := []byte(s)
+ n := testing.AllocsPerRun(1000, func() {
+ if string(b) != s {
+ t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
+ }
+ if string(b) == s {
+ } else {
+ t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
+ }
+ })
+ if n != 0 {
+ t.Fatalf("want 0 allocs, got %v", n)
+ }
+}