New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: append should be optmized as copy #57759
Comments
cc @dsnet as I've seen him involved in similar threads like #55905 (comment). This issue could perhaps be a duplicate. |
What is the situation in which this occurs in practice? |
Yes, such cases are rare. I haven't encountered such case in my serious Go projects. Maybe, it is good to keep the difference between So does this mean |
Thought it a little more. Please fell free to close this issue if nothing needs to be done. |
It looks package main
import (
"fmt"
)
func main() {
var x = []int{0, 1, 2, 3, 4}
_ = append(x[1:], x[:len(x)-1]...)
fmt.Println(x) // [0 1 2 3 4]
// not [0 0 0 0 0]
} That means dest = dest[:len(src)]
srcStart, srcEnd := &src[0], &src[0] + ElemSize * len(src)
destStart, destEnd := &dest[0], &des[0] + ElemSize * len(src)
if srcStart < destStart && srcEnd > destEnd {
... // need some special handling
} else {
... // copy by normal order
} So, I think adding a |
Sorry, the proving example is corrected now. |
Change https://go.dev/cl/533276 mentions this issue: |
Change https://go.dev/cl/536255 mentions this issue: |
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: 4d6819f GitHub-Pull-Request: #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>
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 golang#57759 for details. Change-Id: I7c101eee496923d7aa59f94720da6c84feb93af8 GitHub-Last-Rev: 4d6819f GitHub-Pull-Request: golang#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>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
What did you expect to see?
Similar CPU usages.
What did you see instead?
The CPU usages differ much.
It looks the
copy
function checks the addresses of the first elements of the two slice argument, butappend
doesn't. Maybe it should?The text was updated successfully, but these errors were encountered: