@@ -15,13 +15,11 @@ pub fn run_benchmark_group<F: FnOnce(&mut BenchmarkGroup)>(define_func: F) {
15
15
}
16
16
17
17
/// Type-erased function that executes a single benchmark.
18
- struct BenchmarkWrapper {
19
- func : Box < dyn Fn ( ) -> anyhow:: Result < BenchmarkStats > > ,
20
- }
18
+ type BenchmarkFn = Box < dyn Fn ( ) -> anyhow:: Result < BenchmarkStats > > ;
21
19
22
20
#[ derive( Default ) ]
23
21
pub struct BenchmarkGroup {
24
- benchmarks : HashMap < & ' static str , BenchmarkWrapper > ,
22
+ benchmarks : HashMap < & ' static str , BenchmarkFn > ,
25
23
}
26
24
27
25
impl BenchmarkGroup {
@@ -37,11 +35,8 @@ impl BenchmarkGroup {
37
35
constructor : F ,
38
36
) {
39
37
// We want to type-erase the target `func` by wrapping it in a Box.
40
- let benchmark_func = Box :: new ( move || benchmark_function ( constructor. clone ( ) ) ) ;
41
- let benchmark_def = BenchmarkWrapper {
42
- func : benchmark_func,
43
- } ;
44
- if self . benchmarks . insert ( name, benchmark_def) . is_some ( ) {
38
+ let benchmark_fn = Box :: new ( move || benchmark_function ( constructor. clone ( ) ) ) ;
39
+ if self . benchmarks . insert ( name, benchmark_fn) . is_some ( ) {
45
40
panic ! ( "Benchmark {} was registered twice" , name) ;
46
41
}
47
42
}
@@ -63,7 +58,7 @@ impl BenchmarkGroup {
63
58
}
64
59
65
60
fn run_benchmarks ( self , args : BenchmarkArgs ) -> anyhow:: Result < ( ) > {
66
- let mut items: Vec < ( & ' static str , BenchmarkWrapper ) > = self
61
+ let mut items: Vec < ( & ' static str , BenchmarkFn ) > = self
67
62
. benchmarks
68
63
. into_iter ( )
69
64
. filter ( |( name, _) | {
@@ -74,10 +69,10 @@ impl BenchmarkGroup {
74
69
75
70
let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
76
71
77
- for ( name, def ) in items {
72
+ for ( name, benchmark_fn ) in items {
78
73
let mut stats: Vec < BenchmarkStats > = Vec :: with_capacity ( args. iterations as usize ) ;
79
74
for i in 0 ..args. iterations {
80
- let benchmark_stats = ( def . func ) ( ) ?;
75
+ let benchmark_stats = benchmark_fn ( ) ?;
81
76
log:: info!( "Benchmark (run {i}) `{name}` completed: {benchmark_stats:?}" ) ;
82
77
stats. push ( benchmark_stats) ;
83
78
}
0 commit comments