Skip to content

Commit bca616b

Browse files
authored
slices: change implementation for Clone()
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)
1 parent 3fce111 commit bca616b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/slices/slices.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ func Clone[S ~[]E, E any](s S) S {
340340
if s == nil {
341341
return nil
342342
}
343-
return append(S([]E{}), s...)
343+
t := make([]E, len(s))
344+
copy(t, s)
345+
return t
344346
}
345347

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

0 commit comments

Comments
 (0)