Repository navigation
Expand file tree
/
Copy pathbench_test.go
More file actions
150 lines (133 loc) · 3.41 KB
/
Copy pathbench_test.go
File metadata and controls
150 lines (133 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package chunkflow_test
import (
"context"
"iter"
"testing"
"github.com/shults/chunkflow"
"github.com/shults/chunkflow/seq"
)
// memStream is the in-memory, infallible pipeline that chunkflow shipped as Stream[T]
// until v0.1.0. It was removed from the library because Stream (the former Stream) is a
// strict superset, and keeping two types meant every operator, test and doc twice. It
// lives on here, private, as the reference point for what the error/context plumbing
// costs on pure data. If the numbers ever matter to someone, this is the starting point
// for a separate type.
type memStream[T any] struct{ seq iter.Seq[T] }
func newMem[T any](s iter.Seq[T]) memStream[T] { return memStream[T]{seq: s} }
func (a memStream[T]) Map[R any](fn func(T) R) memStream[R] {
return newMem(func(yield func(R) bool) {
for v := range a.seq {
if !yield(fn(v)) {
return
}
}
})
}
func (a memStream[T]) Filter(pred func(T) bool) memStream[T] {
return newMem(func(yield func(T) bool) {
for v := range a.seq {
if pred(v) && !yield(v) {
return
}
}
})
}
// Chunk takes R as an explicit type parameter for the same reason Stream.Chunk does:
// returning memStream[[]T] directly forms a generic instantiation cycle (T, []T, [][]T, ...).
func (a memStream[T]) Chunk[R []T](size int) memStream[R] {
return newMem(func(yield func(R) bool) {
chunk := make([]T, 0, size)
for v := range a.seq {
chunk = append(chunk, v)
if len(chunk) == size {
if !yield(chunk) {
return
}
chunk = make([]T, 0, size)
}
}
if len(chunk) > 0 {
yield(chunk)
}
})
}
func memFlatten[E any](a memStream[[]E]) memStream[E] {
return newMem(func(yield func(E) bool) {
for chunk := range a.seq {
for _, v := range chunk {
if !yield(v) {
return
}
}
}
})
}
func (a memStream[T]) Reduce[R any](init R, fn func(R, T) R) R {
acc := init
for v := range a.seq {
acc = fn(acc, v)
}
return acc
}
func (a memStream[T]) Collect() []T {
var out []T
for v := range a.seq {
out = append(out, v)
}
return out
}
const benchN = 100_000
var (
benchSink []int
benchSinkInt int
)
func BenchmarkMem_MapFilterCollect(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
benchSink = newMem(seq.Range(0, benchN)).
Map(func(i int) int { return i * 2 }).
Filter(func(i int) bool { return i%3 == 0 }).
Collect()
}
}
func BenchmarkStream_MapFilterCollect(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
for b.Loop() {
benchSink, _ = chunkflow.New(ctx).Seq(seq.Range(0, benchN)).
Map(func(i int) int { return i * 2 }).
Filter(func(i int) bool { return i%3 == 0 }).
Collect()
}
}
func BenchmarkMem_ChunkFlattenReduce(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
benchSinkInt = memFlatten(newMem(seq.Range(0, benchN)).Chunk[[]int](100)).
Reduce(0, func(acc, i int) int { return acc + i })
}
}
func BenchmarkStream_ChunkFlattenReduce(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
for b.Loop() {
benchSinkInt, _ = chunkflow.New(ctx).Seq(seq.Range(0, benchN)).
Chunk[[]int](100).
Through(chunkflow.Flatten).
Reduce(0, func(acc, i int) int { return acc + i })
}
}
// Baseline: the same work as a plain loop, to see what any abstraction costs at all.
func BenchmarkBaseline_PlainLoop(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
var out []int
for i := range benchN {
v := i * 2
if v%3 == 0 {
out = append(out, v)
}
}
benchSink = out
}
}