]> Cypherpunks repositories - gostls13.git/commit
cmd/gc: don't copy []byte during string concatenation
authorDmitry Vyukov <dvyukov@google.com>
Thu, 22 Jan 2015 14:56:12 +0000 (17:56 +0300)
committerDmitry Vyukov <dvyukov@google.com>
Tue, 27 Jan 2015 18:15:42 +0000 (18:15 +0000)
commit205ae07cd3c39dbb17948e3c957a785212a8c5d1
tree69283f203f5313c4ab078ce9186a7fff28e66e16
parenta7bb393628cbd6c5934e4bc34a45e1f0eabc908e
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>
src/cmd/gc/order.c
src/runtime/malloc_test.go
src/runtime/string.go