Skip to content

Commit 6563afd

Browse files
committed
small optimization
1 parent 97fa9dd commit 6563afd

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ func main() {
6262
Benchmarks show that this package is faster than both of the above and I find it easier to use.
6363

6464
```
65-
BenchmarkDataloader/caches-8 4512438 267.7 ns/op 168 B/op 5 allocs/op
66-
BenchmarkDataloader/random_spread-8 1000000 1267 ns/op 626 B/op 11 allocs/op
67-
BenchmarkDataloader/concurently-8 16305 78332 ns/op 28619 B/op 154 allocs/op
68-
BenchmarkDataloader/all_in_one_request-8 10000 7081367 ns/op 2576182 B/op 60031 allocs/op
69-
70-
BenchmarkDataloaden/caches-8 18537674 68.09 ns/op 24 B/op 1 allocs/op
71-
BenchmarkDataloaden/random_spread-8 2885696 696.8 ns/op 295 B/op 5 allocs/op
72-
BenchmarkDataloaden/concurently-8 23302 50965 ns/op 2799 B/op 75 allocs/op
73-
BenchmarkDataloaden/all_in_one_request-8 10000 1279412 ns/op 487867 B/op 10007 allocs/op
74-
75-
BenchmarkDataloadgen/caches-8 23384140 53.00 ns/op 8 B/op 0 allocs/op
76-
BenchmarkDataloadgen/random_spread-8 2394097 604.9 ns/op 292 B/op 4 allocs/op
77-
BenchmarkDataloadgen/concurently-8 28359 39131 ns/op 2852 B/op 68 allocs/op
78-
BenchmarkDataloadgen/all_in_one_request-8 10000 1041825 ns/op 573643 B/op 8 allocs/op
65+
BenchmarkDataloader/caches-8 4363897 273.6 ns/op 168 B/op 5 allocs/op
66+
BenchmarkDataloader/random_spread-8 1000000 1308 ns/op 620 B/op 11 allocs/op
67+
BenchmarkDataloader/10_concurently-8 15818 80064 ns/op 29203 B/op 155 allocs/op
68+
BenchmarkDataloader/all_in_one_request-8 10000 6886305 ns/op 2575523 B/op 60026 allocs/op
69+
70+
BenchmarkDataloaden/caches-8 19571458 60.74 ns/op 24 B/op 1 allocs/op
71+
BenchmarkDataloaden/random_spread-8 2477028 653.7 ns/op 302 B/op 5 allocs/op
72+
BenchmarkDataloaden/10_concurently-8 20932 53285 ns/op 2802 B/op 75 allocs/op
73+
BenchmarkDataloaden/all_in_one_request-8 10000 1303027 ns/op 487867 B/op 10007 allocs/op
74+
75+
BenchmarkDataloadgen/caches-8 22270087 53.23 ns/op 8 B/op 0 allocs/op
76+
BenchmarkDataloadgen/random_spread-8 2454928 495.9 ns/op 289 B/op 4 allocs/op
77+
BenchmarkDataloadgen/10_concurently-8 17260 65339 ns/op 9541 B/op 63 allocs/op
78+
BenchmarkDataloadgen/all_in_one_request-8 10000 978196 ns/op 573651 B/op 8 allocs/op
7979
```
8080

8181
To run the benchmarks, run `go test -bench=. . -benchmem` from the benchmark directory.

benchmark/benchmarks_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func BenchmarkDataloader(b *testing.B) {
6161
for n := 0; n < b.N; n++ {
6262
queries = append(queries, rand.Int())
6363
}
64-
b.ResetTimer()
6564
thunks := make([]func() (benchmarkUser, error), b.N)
65+
b.ResetTimer()
6666
for i := 0; i < b.N; i++ {
6767
thunks[i] = dl.Load(ctx, queries[i])
6868
}
@@ -72,7 +72,7 @@ func BenchmarkDataloader(b *testing.B) {
7272
}
7373
})
7474

75-
b.Run("concurently", func(b *testing.B) {
75+
b.Run("10 concurently", func(b *testing.B) {
7676
queries := []int{}
7777
for n := 0; n < b.N*10; n++ {
7878
queries = append(queries, rand.Int())
@@ -154,8 +154,8 @@ func BenchmarkDataloadgen(b *testing.B) {
154154
for n := 0; n < b.N; n++ {
155155
queries = append(queries, rand.Int())
156156
}
157-
b.ResetTimer()
158157
thunks := make([]func() (benchmarkUser, error), b.N)
158+
b.ResetTimer()
159159
for i := 0; i < b.N; i++ {
160160
thunks[i] = dl.LoadThunk(ctx, queries[i])
161161
}
@@ -165,7 +165,7 @@ func BenchmarkDataloadgen(b *testing.B) {
165165
}
166166
})
167167

168-
b.Run("concurently", func(b *testing.B) {
168+
b.Run("10 concurently", func(b *testing.B) {
169169
queries := []int{}
170170
for n := 0; n < 10*b.N; n++ {
171171
queries = append(queries, rand.Int())
@@ -238,8 +238,8 @@ func BenchmarkDataloaden(b *testing.B) {
238238
for n := 0; n < b.N; n++ {
239239
queries = append(queries, rand.Int())
240240
}
241-
b.ResetTimer()
242241
thunks := make([]func() (*User, error), b.N)
242+
b.ResetTimer()
243243
for i := 0; i < b.N; i++ {
244244
thunks[i] = dl.LoadThunk(queries[i])
245245
}
@@ -249,7 +249,7 @@ func BenchmarkDataloaden(b *testing.B) {
249249
}
250250
})
251251

252-
b.Run("concurently", func(b *testing.B) {
252+
b.Run("10 concurently", func(b *testing.B) {
253253
queries := []int{}
254254
for n := 0; n < b.N*10; n++ {
255255
queries = append(queries, rand.Int())

benchmark/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/graph-gophers/dataloader/v7 v7.1.0
7-
github.com/vikstrous/dataloadgen v0.0.4
7+
github.com/vikstrous/dataloadgen v0.0.6
88
)
99

1010
require (

dataloadgen.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ func (l *Loader[KeyT, ValueT]) startBatch(ctx context.Context) {
237237
done: make(chan struct{}),
238238
firstContext: ctx,
239239
}
240+
if l.maxBatch != 0 {
241+
batch.contexts = make([]context.Context, 0, l.maxBatch)
242+
batch.keys = make([]KeyT, 0, l.maxBatch)
243+
if l.tracer != nil {
244+
batch.spans = make([]trace.Span, 0, l.maxBatch)
245+
}
246+
}
240247
l.batch = batch
241248
go func(l *Loader[KeyT, ValueT]) {
242249
time.Sleep(l.wait)

0 commit comments

Comments
 (0)