From 4b30a40d8856cc3f6c8f629a9f825feeaf9848af Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Mon, 28 Oct 2024 14:15:13 -0700 Subject: [PATCH] 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 --- src/bytes/buffer.go | 4 ++-- src/runtime/string.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 } -- 2.48.1