From: Keith Randall Date: Mon, 28 Oct 2024 21:15:13 +0000 (-0700) Subject: strings,bytes: use result of copy in subsequent slicing X-Git-Tag: go1.24rc1~551 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4b30a40d8856cc3f6c8f629a9f825feeaf9848af;p=gostls13.git strings,bytes: use result of copy in subsequent slicing This can get rid of a bounds check. Followup to CL 622240. Change-Id: I9d0a2c0408b8d274c46136d32d7a5fb09b4aad1c Reviewed-on: https://go-review.googlesource.com/c/go/+/622955 Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor --- diff --git a/src/bytes/buffer.go b/src/bytes/buffer.go index 4176d670ec..f90d9eca0f 100644 --- a/src/bytes/buffer.go +++ b/src/bytes/buffer.go @@ -247,8 +247,8 @@ func growSlice(b []byte, n int) []byte { c = 2 * cap(b) } b2 := append([]byte(nil), make([]byte, c)...) - copy(b2, b) - return b2[:len(b)] + i := copy(b2, b) + return b2[:i] } // WriteTo writes data to w until the buffer is drained or an error occurs. diff --git a/src/runtime/string.go b/src/runtime/string.go index 640ee02a3c..e43f4cca51 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -51,8 +51,8 @@ func concatstrings(buf *tmpBuf, a []string) string { } s, b := rawstringtmp(buf, l) for _, x := range a { - copy(b, x) - b = b[len(x):] + n := copy(b, x) + b = b[n:] } return s }