Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
104 changes: 104 additions & 0 deletions gslice/gslice_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
"testing"

"github.com/bytedance/gg/collection/set"
"github.com/bytedance/gg/internal/iter"
)

Expand Down Expand Up @@ -107,3 +108,106 @@ func BenchmarkShuffle_Parallel(b *testing.B) {
})
})
}

func oldUnion[S ~[]T, T comparable](ss ...S) S {
if len(ss) == 0 {
return S{}
}
if len(ss) == 1 {
return Uniq(ss[0])
}
members := set.New[T]()
ret := S{} // TODO: Guess a cap.
for _, s := range ss {
for _, v := range s {
if members.Add(v) {
ret = append(ret, v)
}
}
}
return ret
}

func BenchmarkUnion(b *testing.B) {
// 1. all different
ss1 := [][]int{
Range(0, 10),
Range(10, 20),
}
b.Run("new-union-diff-2-10", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Union(ss1...)
}
})
b.Run("old-union-diff-2-10", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldUnion(ss1...)
}
})
ss2 := [][]int{
Range(0, 100),
Range(100, 200),
Range(200, 300),
Range(300, 400),
Range(400, 500),
}
b.Run("new-union-diff-5-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Union(ss2...)
}
})
b.Run("old-union-diff-5-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldUnion(ss2...)
}
})

// 2. all same
ss3 := [][]int{
Repeat(0, 10),
Repeat(0, 10),
}
b.Run("new-union-same-2-10", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Union(ss3...)
}
})
b.Run("old-union-same-2-10", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldUnion(ss3...)
}
})
ss4 := [][]int{
Repeat(0, 100),
Repeat(0, 100),
Repeat(0, 100),
Repeat(0, 100),
Repeat(0, 100),
}
b.Run("new-union-same-5-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Union(ss4...)
}
})
b.Run("old-union-same-5-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldUnion(ss4...)
}
})

// 3. half different
ss5 := [][]int{
Range(0, 100),
Range(50, 150),
}
b.Run("new-union-half-2-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Union(ss5...)
}
})
b.Run("old-union-half-2-100", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldUnion(ss5...)
}
})
}
Loading