Skip to content

Commit

Permalink
Add benchmarks for vector.
Browse files Browse the repository at this point in the history
BenchmarkVector/PersistentVector_NoTransient/Conj-4     1338901  950 ns/op  725 B/op  4 allocs/op
BenchmarkVector/PersistentVector_WithTransient/Conj-4  16131096  169 ns/op   54 B/op  0 allocs/op
BenchmarkVector/TransientVector_NoBatch/Conj-4          7930293  140 ns/op   78 B/op  2 allocs/op
BenchmarkVector/TransientVector_Batched/Conj-4         45790796  176 ns/op   54 B/op  0 allocs/op

Not bad!
  • Loading branch information
lthibault committed Nov 5, 2020
1 parent b205219 commit f8ca0f3
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions builtin/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,106 @@ func TestTransientVector(t *testing.T) {
"persistent() ∘ Transient() ∘ persistent() ∘ EmptyVector != EmptyVector")
})
}

func BenchmarkVector(b *testing.B) {
for name, runner := range map[string]func(*testing.B){
"PersistentVector_NoTransient": runBenchmarks(b, new(persistentUnoptimized)),
"PersistentVector_WithTransient": runBenchmarks(b, new(persistentOptimized)),
"TransientVector_NoBatch": runBenchmarks(b, new(transientUnbatched)),
"TransientVector_Batched": runBenchmarks(b, new(transientBatched)),
} {
b.Run(name, runner)
}
}

func runBenchmarks(b *testing.B, s benchSuite) func(*testing.B) {
return func(b *testing.B) {
for name, runner := range map[string]func(*testing.B){
"Conj": s.BenchmarkConj,
} {
b.Run(name, func(b *testing.B) {
s.Setup(b)
defer s.Teardown()

b.ReportAllocs()
b.ResetTimer()

runner(b)
})
}
}
}

type benchSuite interface {
Setup(*testing.B)
Teardown()
BenchmarkConj(*testing.B)
}

type persistentUnoptimized struct{ vec core.Vector }

func (suite *persistentUnoptimized) Setup(b *testing.B) { suite.vec = EmptyVector }

func (suite *persistentUnoptimized) Teardown() {}

func (suite *persistentUnoptimized) BenchmarkConj(b *testing.B) {
for i := 0; i < b.N; i++ {
// call Conj() one item at a time to avoid triggering transient optimization.
suite.vec, _ = suite.vec.Conj(Int64(i))
}
}

type persistentOptimized struct {
vec core.Vector
items []core.Any
}

func (suite *persistentOptimized) Setup(b *testing.B) {
suite.vec = EmptyVector

suite.items = make([]core.Any, b.N)
for i := 0; i < b.N; i++ {
suite.items[i] = Int64(i)
}
}

func (suite *persistentOptimized) Teardown() {}

func (suite *persistentOptimized) BenchmarkConj(b *testing.B) {
suite.vec, _ = suite.vec.Conj(suite.items...)
}

type transientUnbatched struct{ vec core.Vector }

func (suite *transientUnbatched) Setup(b *testing.B) {
suite.vec = EmptyVector.Transient()
}

func (suite *transientUnbatched) Teardown() {}

func (suite *transientUnbatched) BenchmarkConj(b *testing.B) {
for i := 0; i < b.N; i++ {
// call Conj() one item at a time to avoid triggering transient optimization.
suite.vec, _ = suite.vec.Conj(Int64(i))
}
}

type transientBatched struct {
vec core.Vector
items []core.Any
}

func (suite *transientBatched) Setup(b *testing.B) {
suite.vec = EmptyVector.Transient()

suite.items = make([]core.Any, b.N)
for i := 0; i < b.N; i++ {
suite.items[i] = Int64(i)
}
}

func (suite *transientBatched) Teardown() {}

func (suite *transientBatched) BenchmarkConj(b *testing.B) {
suite.vec, _ = suite.vec.Conj(suite.items...)
}

0 comments on commit f8ca0f3

Please sign in to comment.