aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/string.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-01-22 17:56:12 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-01-27 18:15:42 +0000
commit205ae07cd3c39dbb17948e3c957a785212a8c5d1 (patch)
tree69283f203f5313c4ab078ce9186a7fff28e66e16 /src/runtime/string.go
parenta7bb393628cbd6c5934e4bc34a45e1f0eabc908e (diff)
downloadgo-205ae07cd3c39dbb17948e3c957a785212a8c5d1.tar.gz
go-205ae07cd3c39dbb17948e3c957a785212a8c5d1.zip
cmd/gc: don't copy []byte during string concatenation
Consider the following code: s := "(" + string(byteSlice) + ")" Currently we allocate a new string during []byte->string conversion, and pass it to concatstrings. String allocation is unnecessary in this case, because concatstrings does memorize the strings for later use. This change uses slicebytetostringtmp to construct temp string directly from []byte buffer and passes it to concatstrings. I've found few such cases in std lib: s += string(msg[off:off+c]) + "." buf.WriteString("Sec-WebSocket-Accept: " + string(c.accept) + "\r\n") bw.WriteString("Sec-WebSocket-Key: " + string(nonce) + "\r\n") err = xml.Unmarshal([]byte("<Top>"+string(data)+"</Top>"), &logStruct) d.err = d.syntaxError("invalid XML name: " + string(b)) return m, ProtocolError("malformed MIME header line: " + string(kv)) But there are much more in our internal code base. Change-Id: I42f401f317131237ddd0cb9786b0940213af16fb Reviewed-on: https://go-review.googlesource.com/3163 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r--src/runtime/string.go3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go
index 96f9579624..8aa0dd076d 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -73,8 +73,9 @@ func slicebytetostringtmp(b []byte) string {
// that know that the string form will be discarded before
// the calling goroutine could possibly modify the original
// slice or synchronize with another goroutine.
- // Today, the only such case is a m[string(k)] lookup where
+ // First such case is a m[string(k)] lookup where
// m is a string-keyed map and k is a []byte.
+ // Second such case is "<"+string(b)+">" concatenation where b is []byte.
if raceenabled && len(b) > 0 {
racereadrangepc(unsafe.Pointer(&b[0]),