Skip to content

Commit

Permalink
slices: change implementation for Clone()
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
David-Mao authored Jul 5, 2023
1 parent 3fce111 commit bca616b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ func Clone[S ~[]E, E any](s S) S {
if s == nil {
return nil
}
return append(S([]E{}), s...)
t := make([]E, len(s))
copy(t, s)
return t
}

// Compact replaces consecutive runs of equal elements with a single copy.
Expand Down

0 comments on commit bca616b

Please sign in to comment.