From cafeb2d1179cf8224fd828f1f2402def8d29afca Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Wed, 23 Oct 2024 13:52:50 +0800 Subject: [PATCH] remove OutputValue Option constraint --- README.md | 7 ++++--- benches/bench.rs | 2 -- benches/bench_group.rs | 7 ++----- benches/bench_input.rs | 7 ++++--- benches/test_bench.rs | 1 - src/bench.rs | 14 +++++++------- src/bench_group.rs | 8 ++++---- src/bench_input_group.rs | 4 ++-- src/bench_runner.rs | 4 ++-- src/lib.rs | 6 ++---- src/output_value.rs | 5 +++++ src/stats.rs | 2 +- 12 files changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index df93345..e5239e4 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,13 @@ fn bench_group(mut runner: InputGroup>) { 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) + // The return value of the function will be reported as the `OutputValue` + map.len() as u64 * (std::mem::size_of::() + std::mem::size_of::()) as u64 }); runner.run(); 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..4996f31 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) + // The return value of the function will be reported as the `OutputValue` + 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..67dbd3d 100644 --- a/src/bench_group.rs +++ b/src/bench_group.rs @@ -50,14 +50,14 @@ impl<'a, 'runner> BenchGroup<'a, 'runner> { /// Register a benchmark with the given name, function and input. /// - /// 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` pub fn register_with_input, O: OutputValue + 'static>( &mut self, bench_name: S, 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()), @@ -69,10 +69,10 @@ impl<'a, 'runner> BenchGroup<'a, 'runner> { /// Register a benchmark with the given name and function. /// - /// 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`. 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..fca415d 100644 --- a/src/bench_input_group.rs +++ b/src/bench_input_group.rs @@ -88,10 +88,10 @@ impl InputGroup { /// Register a benchmark with the given name and function. /// - /// 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` 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..97a209a 100644 --- a/src/bench_runner.rs +++ b/src/bench_runner.rs @@ -96,10 +96,10 @@ impl BenchRunner { /// Run a single function. This will directly execute and report the function and therefore does /// not support interleaved execution. /// - /// The return value of the function will be reported as the [OutputValue::column_title] if it is `Some`. + /// The return value of the function will be reported as the [OutputValue::column_title]. 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/lib.rs b/src/lib.rs index 0a60265..85b7228 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,11 +69,11 @@ //! .add_plugin(PerfCounterPlugin::default()); //! runner.register("vec", move |data| { //! let vec = test_vec(data); -//! Some(vec.len() as u64) +//! vec.len() as u64 //! }); //! runner.register("hashmap", move |data| { //! let map = test_hashmap(data); -//! Some(map.len() as u64) +//! map.len() as u64 //! }); //! runner.run(); //! } @@ -146,11 +146,9 @@ //! group.set_input_size(data.len() * std::mem::size_of::()); //! group.register_with_input("vec", data, move |data| { //! black_box(test_vec(data)); -//! Some(()) //! }); //! group.register_with_input("hashmap", data, move |data| { //! black_box(test_hashmap(data)); -//! Some(()) //! }); //! } //! group.run(); 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, } }