compiler: avoid copy for string([]byte) conversion used in string comparison
If a string([]byte) conversion is used immediately in a string
comparison, we don't need to copy the backing store of the byte
slice, as the string comparison doesn't hold any reference to
it. Instead, just create a string header from the byte slice and
pass it for comparison.
A new type of expression, String_value_expression, is introduced,
for constructing string headers.
A test could be added in the GCC testsuite:
Index: gcc/testsuite/go.dg/cmpstring.go
===================================================================
--- gcc/testsuite/go.dg/cmpstring.go (nonexistent)
+++ gcc/testsuite/go.dg/cmpstring.go (working copy)
@@ -0,0 +1,13 @@
+// { dg-do compile }
+// { dg-options "-fgo-debug-optimization" }
+
+package p
+
+func F(x []byte, y string) bool {
+ return string(x) == y // { dg-error "no copy string\\(\\\[\\\]byte\\)" }
+}
+
+func BytesEqual(x, y []byte) bool {
+ return string(x) == // { dg-error "no copy string\\(\\\[\\\]byte\\)" }
+ string(y) // { dg-error "no copy string\\(\\\[\\\]byte\\)" }
+}
Change-Id: I872d210d0d1a66a5df9c2cfabe634532406c47a9
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/170894
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2 files changed