Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gslice/gslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,9 @@ func Union[S ~[]T, T comparable](ss ...S) S {
if len(ss) == 1 {
return Uniq(ss[0])
}
members := set.New[T]()
ret := S{} // TODO: Guess a cap.
size := Sum(Map(ss, func(s S) int { return len(s) }))
members := set.NewWithCap[T](size)
ret := make(S, 0, size/2)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a benchmark for this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go test ./gslice -bench=BenchmarkUnion -benchmem

goos: darwin
goarch: arm64
pkg: github.com/bytedance/gg/gslice
cpu: Apple M4 Pro
BenchmarkUnion/new-union-diff-2-10-14            3053234               373.5 ns/op           944 B/op          8 allocs/op
BenchmarkUnion/old-union-diff-2-10-14            2819313               412.5 ns/op          1744 B/op         10 allocs/op
BenchmarkUnion/new-union-diff-5-100-14            178990              6407 ns/op           24736 B/op          8 allocs/op
BenchmarkUnion/old-union-diff-5-100-14             73288             16443 ns/op           44624 B/op         22 allocs/op
BenchmarkUnion/new-union-same-2-10-14            5271890               218.1 ns/op           784 B/op          7 allocs/op
BenchmarkUnion/old-union-same-2-10-14            5344665               226.7 ns/op          1248 B/op          5 allocs/op
BenchmarkUnion/new-union-same-5-100-14            368089              3117 ns/op           20640 B/op          7 allocs/op
BenchmarkUnion/old-union-same-5-100-14            603517              2002 ns/op            1248 B/op          5 allocs/op
BenchmarkUnion/new-union-half-2-100-14            668262              1811 ns/op            5888 B/op          7 allocs/op
BenchmarkUnion/old-union-half-2-100-14            415273              2723 ns/op            5616 B/op         14 allocs/op

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

for _, s := range ss {
for _, v := range s {
if members.Add(v) {
Expand Down
Loading