-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: suboptimal cloning/optimization in slices.Clone #53643
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
Comments
I believe we use the The With both those said, it would be nice if the |
If there is anything to be fixed here, it is in the compiler, so retitled. |
"The append version is also knows that it doesn't need to zero the allocation before overwriting it with the copy." I think what we should do is detect cases of |
my 2 cents is to do write the library code so it's readable and not "cool one liner", ie option 2 (make+copy) is imo a better implementation to read - and happen to be faster, so ... great! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes with
go version devel go1.19-d3ffff2790 Tue Jun 28 13:01:41 2022 +0000 linux/amd64
.What operating system and processor architecture are you using (
go env
)?go env
OutputDiscoveries / Proposal
slices.Clone
is currently defined as:append
here seems to be optimized to output what is essentiallymake
+copy
. But if we do that explicitly:and benchmark it (see https://go.dev/play/p/WHvD0zJ33S1) then the new function generally performs better:
Looking at the assembly, it seems that the current version uses
runtime.growslice
while the new one,runtime.mallocgc
. There are other changes in the assembly as well.The text was updated successfully, but these errors were encountered: