-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
745 lines (712 loc) · 11.5 KB
/
bench_test.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package dscope
import (
"reflect"
"testing"
)
type (
T1 int
T2 int
T3 int
T4 int
T5 int
T6 int
T7 int
T8 int
T9 int
T10 int
T11 int
T12 int
T13 int
T14 int
T15 int
T16 int
T17 int
T18 int
T19 int
T20 int
T21 int
T22 int
T23 int
T24 int
T25 int
T26 int
T27 int
T28 int
T29 int
T30 int
)
func BenchmarkForkWithNewDeps(b *testing.B) {
scope := New().Fork(
func() T1 { return 42 },
func(t1 T1) T2 { return T2(t1) },
func(t2 T2) T3 { return T3(t2) },
func(t3 T3) T4 { return T4(t3) },
func(t4 T4) T5 { return T5(t4) },
func(t5 T5) T6 { return T6(t5) },
func(t6 T6) T7 { return T7(t6) },
func(t7 T7) T8 { return T8(t7) },
func(t8 T8) T9 { return T9(t8) },
func(t9 T9) T10 { return T10(t9) },
func(t10 T10) T11 { return T11(t10) },
func(t11 T11) T12 { return T12(t11) },
func(t12 T12) T13 { return T13(t12) },
func(t13 T13) T14 { return T14(t13) },
func(t14 T14) T15 { return T15(t14) },
func(t15 T15) T16 { return T16(t15) },
func(t16 T16) T17 { return T17(t16) },
func(t17 T17) T18 { return T18(t17) },
func(t18 T18) T19 { return T19(t18) },
func(t19 T19) T20 { return T20(t19) },
func(t20 T20) T21 { return T21(t20) },
func(t21 T21) T22 { return T22(t21) },
func(t22 T22) T23 { return T23(t22) },
func(t23 T23) T24 { return T24(t23) },
func(t24 T24) T25 { return T25(t24) },
func(t25 T25) T26 { return T26(t25) },
func(t26 T26) T27 { return T27(t26) },
func(t27 T27) T28 { return T28(t27) },
func(t28 T28) T29 { return T29(t28) },
func(t29 T29) T30 { return T30(t29) },
)
type S string
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Fork(
func(s S) T2 {
return 29
},
func() S {
return "42"
},
)
}
}
func BenchmarkForkWithoutNewDeps(b *testing.B) {
scope := New().Fork(
func() T1 { return 42 },
func(t1 T1) T2 { return T2(t1) },
func(t2 T2) T3 { return T3(t2) },
func(t3 T3) T4 { return T4(t3) },
func(t4 T4) T5 { return T5(t4) },
func(t5 T5) T6 { return T6(t5) },
func(t6 T6) T7 { return T7(t6) },
func(t7 T7) T8 { return T8(t7) },
func(t8 T8) T9 { return T9(t8) },
func(t9 T9) T10 { return T10(t9) },
func(t10 T10) T11 { return T11(t10) },
func(t11 T11) T12 { return T12(t11) },
func(t12 T12) T13 { return T13(t12) },
func(t13 T13) T14 { return T14(t13) },
func(t14 T14) T15 { return T15(t14) },
func(t15 T15) T16 { return T16(t15) },
func(t16 T16) T17 { return T17(t16) },
func(t17 T17) T18 { return T18(t17) },
func(t18 T18) T19 { return T19(t18) },
func(t19 T19) T20 { return T20(t19) },
func(t20 T20) T21 { return T21(t20) },
func(t21 T21) T22 { return T22(t21) },
func(t22 T22) T23 { return T23(t22) },
func(t23 T23) T24 { return T24(t23) },
func(t24 T24) T25 { return T25(t24) },
func(t25 T25) T26 { return T26(t25) },
func(t26 T26) T27 { return T27(t26) },
func(t27 T27) T28 { return T28(t27) },
func(t28 T28) T29 { return T29(t28) },
func(t29 T29) T30 { return T30(t29) },
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Fork(
func() T30 {
return 42
},
)
}
}
func BenchmarkCall(b *testing.B) {
scope := New().Fork(
func() T1 { return 1 },
func() T2 { return 2 },
func() T3 { return 3 },
func() T4 { return 4 },
func() T5 { return 5 },
func() T6 { return 6 },
func() T7 { return 7 },
func() T8 { return 8 },
func() T9 { return 9 },
func() T10 { return 10 },
func() T11 { return 11 },
func() T12 { return 12 },
func() T13 { return 13 },
func() T14 { return 14 },
func() T15 { return 15 },
func() T16 { return 16 },
func() T17 { return 17 },
func() T18 { return 18 },
func() T19 { return 19 },
func() T20 { return 20 },
func() T21 { return 21 },
func() T22 { return 22 },
)
var t1 T1
var t2 T2
var t3 T3
var t4 T4
var t5 T5
var t6 T6
var t7 T7
var t8 T8
var t9 T9
var t10 T10
var t11 T11
var t12 T12
var t13 T13
var t14 T14
var t15 T15
var t16 T16
var t17 T17
var t18 T18
var t19 T19
var t20 T20
var t21 T21
var t22 T22
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Call(func(
t1 T1,
t2 T2,
t3 T3,
t4 T4,
t5 T5,
t6 T6,
t7 T7,
t8 T8,
t9 T9,
t10 T10,
t11 T11,
t12 T12,
t13 T13,
t14 T14,
t15 T15,
t16 T16,
t17 T17,
t18 T18,
t19 T19,
t20 T20,
t21 T21,
t22 T22,
) (
T1,
T2,
T3,
T4,
T5,
T6,
T7,
T8,
T9,
T10,
T11,
T12,
T13,
T14,
T15,
T16,
T17,
T18,
T19,
T20,
T21,
T22,
) {
return t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15,
t16,
t17,
t18,
t19,
t20,
t21,
t22
},
).Assign(
&t1,
&t2,
&t3,
&t4,
&t5,
&t6,
&t7,
&t8,
&t9,
&t10,
&t11,
&t12,
&t13,
&t14,
&t15,
&t16,
&t17,
&t18,
&t19,
&t20,
&t21,
&t22,
)
}
}
func BenchmarkGetAndCall(b *testing.B) {
scope := New().Fork(
func() T1 { return 1 },
func() T2 { return 2 },
func() T3 { return 3 },
func() T4 { return 4 },
func() T5 { return 5 },
func() T6 { return 6 },
func() T7 { return 7 },
func() T8 { return 8 },
func() T9 { return 9 },
func() T10 { return 10 },
func() T11 { return 11 },
func() T12 { return 12 },
func() T13 { return 13 },
func() T14 { return 14 },
func() T15 { return 15 },
func() T16 { return 16 },
func() T17 { return 17 },
func() T18 { return 18 },
func() T19 { return 19 },
func() T20 { return 20 },
func() T21 { return 21 },
func() T22 { return 22 },
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
func(
t1 T1,
t2 T2,
t3 T3,
t4 T4,
t5 T5,
t6 T6,
t7 T7,
t8 T8,
t9 T9,
t10 T10,
t11 T11,
t12 T12,
t13 T13,
t14 T14,
t15 T15,
t16 T16,
t17 T17,
t18 T18,
t19 T19,
t20 T20,
t21 T21,
t22 T22,
) (
T1,
T2,
T3,
T4,
T5,
T6,
T7,
T8,
T9,
T10,
T11,
T12,
T13,
T14,
T15,
T16,
T17,
T18,
T19,
T20,
T21,
T22,
) {
return t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15,
t16,
t17,
t18,
t19,
t20,
t21,
t22
}(
Get[T1](scope),
Get[T2](scope),
Get[T3](scope),
Get[T4](scope),
Get[T5](scope),
Get[T6](scope),
Get[T7](scope),
Get[T8](scope),
Get[T9](scope),
Get[T10](scope),
Get[T11](scope),
Get[T12](scope),
Get[T13](scope),
Get[T14](scope),
Get[T15](scope),
Get[T16](scope),
Get[T17](scope),
Get[T18](scope),
Get[T19](scope),
Get[T20](scope),
Get[T21](scope),
Get[T22](scope),
)
}
}
func BenchmarkCallPointerProvider(b *testing.B) {
t1 := T1(1)
t2 := T2(2)
t3 := T3(3)
t4 := T4(4)
t5 := T5(5)
t6 := T6(6)
t7 := T7(7)
t8 := T8(8)
t9 := T9(9)
t10 := T10(10)
t11 := T11(11)
t12 := T12(12)
t13 := T13(13)
t14 := T14(14)
t15 := T15(15)
t16 := T16(16)
t17 := T17(17)
t18 := T18(18)
t19 := T19(19)
t20 := T20(20)
t21 := T21(21)
t22 := T22(22)
scope := New().Fork(
&t1,
&t2,
&t3,
&t4,
&t5,
&t6,
&t7,
&t8,
&t9,
&t10,
&t11,
&t12,
&t13,
&t14,
&t15,
&t16,
&t17,
&t18,
&t19,
&t20,
&t21,
&t22,
)
var r1 T1
var r2 T2
var r3 T3
var r4 T4
var r5 T5
var r6 T6
var r7 T7
var r8 T8
var r9 T9
var r10 T10
var r11 T11
var r12 T12
var r13 T13
var r14 T14
var r15 T15
var r16 T16
var r17 T17
var r18 T18
var r19 T19
var r20 T20
var r21 T21
var r22 T22
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Call(func(
t1 T1,
t2 T2,
t3 T3,
t4 T4,
t5 T5,
t6 T6,
t7 T7,
t8 T8,
t9 T9,
t10 T10,
t11 T11,
t12 T12,
t13 T13,
t14 T14,
t15 T15,
t16 T16,
t17 T17,
t18 T18,
t19 T19,
t20 T20,
t21 T21,
t22 T22,
) (
T1,
T2,
T3,
T4,
T5,
T6,
T7,
T8,
T9,
T10,
T11,
T12,
T13,
T14,
T15,
T16,
T17,
T18,
T19,
T20,
T21,
T22,
) {
return t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15,
t16,
t17,
t18,
t19,
t20,
t21,
t22
},
).Assign(
&r1,
&r2,
&r3,
&r4,
&r5,
&r6,
&r7,
&r8,
&r9,
&r10,
&r11,
&r12,
&r13,
&r14,
&r15,
&r16,
&r17,
&r18,
&r19,
&r20,
&r21,
&r22,
)
}
}
func BenchmarkSimpleCall(b *testing.B) {
type Foo int
f := Foo(42)
scope := New(&f)
var ret Foo
for i := 0; i < b.N; i++ {
scope.Call(func(
f Foo,
) Foo {
return f
}).Assign(&ret)
}
}
var assignBenchDefs = []any{
func() T1 { return 42 },
func(t1 T1) T2 { return T2(t1) },
func(t2 T2) T3 { return T3(t2) },
func(t3 T3) T4 { return T4(t3) },
func(t4 T4) T5 { return T5(t4) },
func(t5 T5) T6 { return T6(t5) },
func(t6 T6) T7 { return T7(t6) },
func(t7 T7) T8 { return T8(t7) },
func(t8 T8) T9 { return T9(t8) },
func(t9 T9) T10 { return T10(t9) },
func(t10 T10) T11 { return T11(t10) },
func(t11 T11) T12 { return T12(t11) },
func(t12 T12) T13 { return T13(t12) },
func(t13 T13) T14 { return T14(t13) },
func(t14 T14) T15 { return T15(t14) },
func(t15 T15) T16 { return T16(t15) },
func(t16 T16) T17 { return T17(t16) },
func(t17 T17) T18 { return T18(t17) },
func(t18 T18) T19 { return T19(t18) },
func(t19 T19) T20 { return T20(t19) },
func(t20 T20) T21 { return T21(t20) },
func(t21 T21) T22 { return T22(t21) },
func(t22 T22) T23 { return T23(t22) },
func(t23 T23) T24 { return T24(t23) },
func(t24 T24) T25 { return T25(t24) },
func(t25 T25) T26 { return T26(t25) },
func(t26 T26) T27 { return T27(t26) },
func(t27 T27) T28 { return T28(t27) },
func(t28 T28) T29 { return T29(t28) },
func(t29 T29) T30 { return T30(t29) },
}
func BenchmarkAssign(b *testing.B) {
scope := New().Fork(assignBenchDefs...)
b.ResetTimer()
var t30 T30
for i := 0; i < b.N; i++ {
scope.Assign(&t30)
}
}
func BenchmarkGenericAssign(b *testing.B) {
scope := New().Fork(assignBenchDefs...)
b.ResetTimer()
var t30 T30
for i := 0; i < b.N; i++ {
Assign(scope, &t30)
}
}
func BenchmarkFork(b *testing.B) {
scope := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Fork()
}
}
func BenchmarkFork1(b *testing.B) {
scope := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Fork(func() int {
return 42
})
}
}
func BenchmarkFork2(b *testing.B) {
scope := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope.Fork(func() int {
return 42
}, func() string {
return "foo"
})
}
}
func BenchmarkGenericGet(b *testing.B) {
s := New(func() int {
return 42
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Get[int](s)
}
}
func BenchmarkGetTypeID(b *testing.B) {
scopeType := reflect.TypeFor[Scope]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getTypeID(scopeType)
}
}
func BenchmarkGetTypeIDParallel(b *testing.B) {
scopeType := reflect.TypeFor[Scope]()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
getTypeID(scopeType)
}
})
}
func BenchmarkRecursiveFork(b *testing.B) {
scope := New(
func() int {
return 42
},
)
var n int
b.ResetTimer()
for i := 0; i < b.N; i++ {
scope = scope.Fork(
func() string {
return "42"
},
)
scope.Assign(&n)
}
}
type benchMultiFunc func()
var _ CustomReducer = benchMultiFunc(nil)
func (benchMultiFunc) Reduce(_ Scope, vs []reflect.Value) reflect.Value {
return Reduce(vs)
}
func BenchmarkMultipleDispatch(b *testing.B) {
s := New(
func() benchMultiFunc {
return func() {
}
},
func() benchMultiFunc {
return func() {
}
},
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Call(func(fn benchMultiFunc) {
fn()
})
}
}