From 509cb3ff390869aa91e0e65de082c5fa81675c63 Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Wed, 23 Oct 2024 13:52:50 +0800 Subject: [PATCH] remove Option contraint --- benches/bench.rs | 2 -- benches/bench_group.rs | 7 ++----- benches/bench_input.rs | 5 +++-- benches/test_bench.rs | 1 - src/bench.rs | 14 +++++++------- src/bench_group.rs | 4 ++-- src/bench_input_group.rs | 2 +- src/bench_runner.rs | 2 +- src/output_value.rs | 5 +++++ src/stats.rs | 2 +- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 7ab6dab..88b5e16 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -20,14 +20,12 @@ fn bench_factorial() { for val in [100, 400] { runner.bench_function(format!("factorial {}", val), move |_| { factorial(black_box(val)); - Some(()) }); } let mut group = runner.new_group(); group.register("factorial 100", |()| { factorial(black_box(100)); - Some(()) }); group.run(); } diff --git a/benches/bench_group.rs b/benches/bench_group.rs index 4a88856..79439b9 100644 --- a/benches/bench_group.rs +++ b/benches/bench_group.rs @@ -44,14 +44,11 @@ fn run_bench() { group.set_input_size(data.len() * std::mem::size_of::()); group.register_with_input("vec", data, move |data| { let vec = black_box(test_vec(data)); - Some(vec.len() as u64) + vec.len() as u64 }); group.register_with_input("hashmap", data, move |data| { let map = black_box(test_hashmap(data)); - Some( - map.len() as u64 - * (std::mem::size_of::() + std::mem::size_of::()) as u64, - ) + map.len() as u64 * (std::mem::size_of::() + std::mem::size_of::()) as u64 }); group.run(); } diff --git a/benches/bench_input.rs b/benches/bench_input.rs index b7cdd17..da4386c 100644 --- a/benches/bench_input.rs +++ b/benches/bench_input.rs @@ -35,12 +35,13 @@ fn bench_group(mut runner: InputGroup, u64>) { runner.throughput(|input| input.len() * std::mem::size_of::()); runner.register("vec", |data| { let vec = black_box(test_vec(data)); - Some(vec.len() as u64) // The return value of the function will be reported as the `OutputValue` if it is `Some`. + // The return value of the function will be reported as the `OutputValue` + vec.len() as u64 }); runner.register("hashmap", move |data| { let map = black_box(test_hashmap(data)); // The return value of the function will be reported as the `OutputValue` if it is `Some`. - Some(map.len() as u64 * (std::mem::size_of::() + std::mem::size_of::()) as u64) + map.len() as u64 * (std::mem::size_of::() + std::mem::size_of::()) as u64 }); runner.run(); } diff --git a/benches/test_bench.rs b/benches/test_bench.rs index 6940326..a5ce482 100644 --- a/benches/test_bench.rs +++ b/benches/test_bench.rs @@ -26,7 +26,6 @@ fn run_bench_throughput() { let start = Instant::now(); // Busy loop for approximately 10 milliseconds. This is more precise than sleep. while start.elapsed() < Duration::from_millis(10) {} - Some(()) }); group.run(); } diff --git a/src/bench.rs b/src/bench.rs index 4ad7308..32ed9a3 100644 --- a/src/bench.rs +++ b/src/bench.rs @@ -18,7 +18,7 @@ pub trait Bench<'a> { fn clear_results(&mut self); } -pub(crate) type CallBench<'a, I, O> = Box Option + 'a>; +pub(crate) type CallBench<'a, I, O> = Box O + 'a>; pub(crate) struct NamedBench<'a, I, O> { pub bench_id: BenchId, @@ -125,7 +125,7 @@ impl<'a, I, O: OutputValue> Bench<'a> for InputWithBenchmark<'a, I, O> { perf_counter, input_size_in_bytes: self.input_size_in_bytes, tracked_memory, - output_value: output_value.and_then(|el| el.format()), + output_value: output_value.format(), old_stats: None, old_perf_counter: None, } @@ -163,10 +163,10 @@ fn get_perf_counter( /// There are multiple runs in a group for each benchmark which will be collected to a vector pub struct RunResult { pub duration_ns: u64, - pub output: Option, + pub output: O, } impl RunResult { - fn new(duration_ns: u64, output: Option) -> Self { + fn new(duration_ns: u64, output: O) -> Self { RunResult { duration_ns, output, @@ -217,13 +217,13 @@ impl<'a, I, O> NamedBench<'a, I, O> { bench_id: &self.bench_id, }); let start = std::time::Instant::now(); - let mut res = None; + let mut res: Option = None; for _ in 0..num_iter { - res = black_box((self.fun)(input)); + res = Some(black_box((self.fun)(input))); } let elapsed = start.elapsed(); - let run_result = RunResult::new(elapsed.as_nanos() as u64 / num_iter as u64, res); + let run_result = RunResult::new(elapsed.as_nanos() as u64 / num_iter as u64, res.unwrap()); plugins.emit(PluginEvents::BenchStop { bench_id: &self.bench_id, duration: run_result.duration_ns, diff --git a/src/bench_group.rs b/src/bench_group.rs index 14d3319..d23d213 100644 --- a/src/bench_group.rs +++ b/src/bench_group.rs @@ -57,7 +57,7 @@ impl<'a, 'runner> BenchGroup<'a, 'runner> { input: &'a I, fun: F, ) where - F: Fn(&'a I) -> Option + 'a, + F: Fn(&'a I) -> O + 'a, { let bench = NamedBench::new( self.get_bench_id(bench_name.into()), @@ -72,7 +72,7 @@ impl<'a, 'runner> BenchGroup<'a, 'runner> { /// The return value of the function will be reported as the `OutputValue` if it is `Some`. pub fn register, O: OutputValue + 'static>(&mut self, bench_name: S, fun: F) where - F: Fn(&'a ()) -> Option + 'static, + F: Fn(&'a ()) -> O + 'static, { let bench_name = bench_name.into(); let bench = NamedBench::new( diff --git a/src/bench_input_group.rs b/src/bench_input_group.rs index 7c17dc3..9245737 100644 --- a/src/bench_input_group.rs +++ b/src/bench_input_group.rs @@ -91,7 +91,7 @@ impl InputGroup { /// The return value of the function will be reported as the `OutputValue` if it is `Some`. pub fn register>(&mut self, name: S, fun: F) where - F: Fn(&I) -> Option + 'static + Clone, + F: Fn(&I) -> O + 'static + Clone, { let name = name.into(); diff --git a/src/bench_runner.rs b/src/bench_runner.rs index 06a251b..cbf81f6 100644 --- a/src/bench_runner.rs +++ b/src/bench_runner.rs @@ -99,7 +99,7 @@ impl BenchRunner { /// The return value of the function will be reported as the [OutputValue::column_title] if it is `Some`. pub fn bench_function, O: OutputValue>(&mut self, name: S, f: F) -> &mut Self where - F: Fn(&()) -> Option + 'static, + F: Fn(&()) -> O + 'static, { let bench_id = BenchId::from_bench_name(name).runner_name(self.name.as_deref()); let named_bench = NamedBench::new( diff --git a/src/output_value.rs b/src/output_value.rs index 8f2ef51..b59fe49 100644 --- a/src/output_value.rs +++ b/src/output_value.rs @@ -28,6 +28,11 @@ impl OutputValue for () { None } } +impl OutputValue for Option { + fn format(&self) -> Option { + self.map(format_with_underscores) + } +} impl OutputValue for u64 { fn format(&self) -> Option { Some(format_with_underscores(*self)) diff --git a/src/stats.rs b/src/stats.rs index 7b3de37..720058a 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -133,7 +133,7 @@ mod tests { fn create_res(duration_ns: u64) -> RunResult { RunResult { - output: None, + output: 0, duration_ns, } }