Skip to content

Commit 48a9a91

Browse files
committed
Remove BenchmarkWrapper.
It's an extra layer that isn't needed, and removing it makes things a little simpler.
1 parent e56aacb commit 48a9a91

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

collector/benchlib/src/benchmark.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ pub fn run_benchmark_group<F: FnOnce(&mut BenchmarkGroup)>(define_func: F) {
1515
}
1616

1717
/// 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>>;
2119

2220
#[derive(Default)]
2321
pub struct BenchmarkGroup {
24-
benchmarks: HashMap<&'static str, BenchmarkWrapper>,
22+
benchmarks: HashMap<&'static str, BenchmarkFn>,
2523
}
2624

2725
impl BenchmarkGroup {
@@ -37,11 +35,8 @@ impl BenchmarkGroup {
3735
constructor: F,
3836
) {
3937
// 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() {
4540
panic!("Benchmark {} was registered twice", name);
4641
}
4742
}
@@ -63,7 +58,7 @@ impl BenchmarkGroup {
6358
}
6459

6560
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
6762
.benchmarks
6863
.into_iter()
6964
.filter(|(name, _)| {
@@ -74,10 +69,10 @@ impl BenchmarkGroup {
7469

7570
let mut stdout = std::io::stdout().lock();
7671

77-
for (name, def) in items {
72+
for (name, benchmark_fn) in items {
7873
let mut stats: Vec<BenchmarkStats> = Vec::with_capacity(args.iterations as usize);
7974
for i in 0..args.iterations {
80-
let benchmark_stats = (def.func)()?;
75+
let benchmark_stats = benchmark_fn()?;
8176
log::info!("Benchmark (run {i}) `{name}` completed: {benchmark_stats:?}");
8277
stats.push(benchmark_stats);
8378
}

0 commit comments

Comments
 (0)