Skip to content
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

slices: make Clone preallocate and copy instead of solely invoking append #61186

Closed
wants to merge 1 commit into from
Closed

Conversation

David-Mao
Copy link

@David-Mao David-Mao commented Jul 5, 2023

slices: make Clone preallocate and copy instead of solely invoking append

By preallocating a slice of equal size then invoking copy, this
change results in a speedup:

                  │   old.txt    │               new.txt                │
                  │    sec/op    │    sec/op     vs base                │
IntClone_10B-12     41.18n ±  1%   34.07n ±  1%  -17.26% (p=0.000 n=10)
IntClone_1Kb-12     719.5n ±  2%   718.9n ±  2%        ~ (p=0.912 n=10)
IntClone_10Kb-12    7.032µ ± 29%   6.946µ ± 14%        ~ (p=1.000 n=10)
IntClone_1Mb-12     239.6µ ±  1%   238.9µ ±  1%        ~ (p=0.353 n=10)
IntClone_10Mb-12    2.431m ±  1%   2.418m ±  1%        ~ (p=0.143 n=10)
ByteClone_10B-12    35.86n ±  0%   29.43n ±  0%  -17.92% (p=0.000 n=10)
ByteClone_10Kb-12   689.2n ±  6%   694.3n ±  3%        ~ (p=0.912 n=10)
geomean             3.138µ         2.964µ         -5.56%

                  │   old.txt    │                new.txt                │
                  │     B/op     │     B/op      vs base                 │
IntClone_10B-12       104.0 ± 0%     104.0 ± 0%       ~ (p=1.000 n=10) ¹
IntClone_1Kb-12     8.023Ki ± 0%   8.023Ki ± 0%       ~ (p=1.000 n=10) ¹
IntClone_10Kb-12    80.02Ki ± 0%   80.02Ki ± 0%       ~ (p=1.000 n=10) ¹
IntClone_1Mb-12     8.000Mi ± 0%   8.000Mi ± 0%       ~ (p=0.350 n=10)
IntClone_10Mb-12    80.00Mi ± 0%   80.00Mi ± 0%       ~ (p=0.173 n=10)
ByteClone_10B-12      40.00 ± 0%     40.00 ± 0%       ~ (p=1.000 n=10) ¹
ByteClone_10Kb-12   10.02Ki ± 0%   10.02Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean             28.97Ki        28.97Ki       -0.00%
¹ all samples are equal

                  │  old.txt   │               new.txt               │
                  │ allocs/op  │ allocs/op   vs base                 │
IntClone_10B-12     2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
IntClone_1Kb-12     2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
IntClone_10Kb-12    2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
IntClone_1Mb-12     2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
IntClone_10Mb-12    2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
ByteClone_10B-12    2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
ByteClone_10Kb-12   2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean             2.000        2.000       +0.00%
¹ all samples are equal

@google-cla
Copy link

google-cla bot commented Jul 5, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gopherbot
Copy link
Contributor

This PR (HEAD: bca616b) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/508075 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Emmanuel Odeke:

Patch Set 1: Hold+1

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: 2c26c97) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/508075 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@David-Mao David-Mao changed the title slices: change implementation for Clone() slices: make Clone preallocate and copy instead of solely invoking append Jul 7, 2023
Change the implementation of slices Clone. It's 28% faster on my laptop. As discussed in #57433 (comment), no extra issue is needed for this simple fix.

goos: darwin
goarch: arm64
pkg: github.com/David-Mao/Euclid/Go/utils
         │   old.txt   │               new.txt               │
         │   sec/op    │   sec/op     vs base                │
Clone-12   16.95n ± 1%   12.06n ± 2%  -28.85% (p=0.000 n=10)
@gopherbot
Copy link
Contributor

This PR (HEAD: 0669c38) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/508075 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from David Mao:

Patch Set 4:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 5:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from David Mao:

Patch Set 5:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ian Lance Taylor:

Patch Set 5:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from David Mao:

Patch Set 5:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/508075.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR is being closed because golang.org/cl/508075 has been abandoned.

Not doing this.

@gopherbot gopherbot closed this Jul 21, 2023
@David-Mao David-Mao deleted the patch-1 branch July 25, 2023 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants