public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Go patch committed: Avoid copy for string([]byte) conversion in string concat
@ 2019-06-18 23:56 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2019-06-18 23:56 UTC (permalink / raw)
  To: gcc-patches, gofrontend-dev

[-- Attachment #1: Type: text/plain, Size: 490 bytes --]

This patch to the Go frontend by Cherry Zhang avoids a copy for a
string([]byte) conversion used in string concatenation.  If a
string([]byte) conversion is used immediately in a string
concatenation, we don't need to copy the backing store of the byte
slice, as the runtime function doesn't hold any reference to it.
Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu.  Committed
to mainline.

Ian

2019-06-18  Cherry Zhang  <cherryyz@google.com>

* go.dg/concatstring.go: New test.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2118 bytes --]

Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 272133)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@
-b1ae35965cadac235d7d218e689944286cccdd90
+62d1b667f3e85f72a186b04aad36d701160a4611
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: gcc/go/gofrontend/expressions.cc
===================================================================
--- gcc/go/gofrontend/expressions.cc	(revision 272133)
+++ gcc/go/gofrontend/expressions.cc	(working copy)
@@ -7408,6 +7408,26 @@ String_concat_expression::do_flatten(Gog
     return this;
   Location loc = this->location();
   Type* type = this->type();
+
+  // Mark string([]byte) operands to reuse the backing store.
+  // runtime.concatstrings does not keep the reference.
+  //
+  // Note: in the gc runtime, if all but one inputs are empty,
+  // concatstrings returns the only nonempty input without copy.
+  // So it is not safe to reuse the backing store if it is a
+  // string([]byte) conversion. So the gc compiler does the
+  // no-copy optimization only when there is at least one
+  // constant nonempty input. Currently the gccgo runtime
+  // doesn't do this, so we don't do the check.
+  for (Expression_list::iterator p = this->exprs_->begin();
+       p != this->exprs_->end();
+       ++p)
+    {
+      Type_conversion_expression* tce = (*p)->conversion_expression();
+      if (tce != NULL)
+        tce->set_no_copy(true);
+    }
+
   Expression* nil_arg = Expression::make_nil(loc);
   Expression* call;
   switch (this->exprs_->size())
Index: gcc/testsuite/go.dg/concatstring.go
===================================================================
--- gcc/testsuite/go.dg/concatstring.go	(nonexistent)
+++ gcc/testsuite/go.dg/concatstring.go	(working copy)
@@ -0,0 +1,8 @@
+// { dg-do compile }
+// { dg-options "-fgo-debug-optimization" }
+
+package p
+
+func F(b []byte, x string) string {
+	return "hello " + string(b) + x // { dg-error "no copy string\\(\\\[\\\]byte\\)" }
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-06-18 23:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-18 23:56 Go patch committed: Avoid copy for string([]byte) conversion in string concat Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).