• Dmitry Vyukov's avatar
    cmd/gc: don't copy []byte during string concatenation · 205ae07c
    Dmitry Vyukov authored
    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/3163Reviewed-by: 's avatarKeith Randall <khr@golang.org>
    Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
    205ae07c
malloc_test.go 4.39 KB