]> Cypherpunks.ru repositories - gostls13.git/commitdiff
slices: avoid an unnecessary check in Replace
authorgo101 <tapir.liu@gmail.com>
Wed, 18 Oct 2023 17:17:18 +0000 (17:17 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 19 Oct 2023 00:48:21 +0000 (00:48 +0000)
The current implementation of the builtin copy function will return early
when it is found that the addresses of the first elements of the two
slice arguments are identical, so it is unnecessarily to do this in user code.

See #57759 for details.

Change-Id: I7c101eee496923d7aa59f94720da6c84feb93af8
GitHub-Last-Rev: 4d6819fb25143f5ad3ff65eca7fe6094c37f2af2
GitHub-Pull-Request: golang/go#63617
Reviewed-on: https://go-review.googlesource.com/c/go/+/536255
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/slices/slices.go

index 252a8eecfc9f90bb1079b420ac8175e30b926b2b..465af14f8e5eeeec2535c2d2d3a64fb1ee61d23e 100644 (file)
@@ -268,9 +268,7 @@ func Replace[S ~[]E, E any](s S, i, j int, v ...E) S {
        if i+len(v) <= j {
                // Easy, as v fits in the deleted portion.
                copy(r[i:], v)
-               if i+len(v) != j {
-                       copy(r[i+len(v):], s[j:])
-               }
+               copy(r[i+len(v):], s[j:])
                return r
        }