Skip to content

Commit

Permalink
remove Option contraint
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz committed Oct 23, 2024
1 parent fa9f636 commit 509cb3f
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 22 deletions.
2 changes: 0 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
7 changes: 2 additions & 5 deletions benches/bench_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ fn run_bench() {
group.set_input_size(data.len() * std::mem::size_of::<usize>());
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::<usize>() + std::mem::size_of::<i32>()) as u64,
)
map.len() as u64 * (std::mem::size_of::<usize>() + std::mem::size_of::<i32>()) as u64
});
group.run();
}
Expand Down
5 changes: 3 additions & 2 deletions benches/bench_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ fn bench_group(mut runner: InputGroup<Vec<usize>, u64>) {
runner.throughput(|input| input.len() * std::mem::size_of::<usize>());
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::<usize>() + std::mem::size_of::<i32>()) as u64)
map.len() as u64 * (std::mem::size_of::<usize>() + std::mem::size_of::<i32>()) as u64
});
runner.run();
}
Expand Down
1 change: 0 additions & 1 deletion benches/test_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Bench<'a> {
fn clear_results(&mut self);
}

pub(crate) type CallBench<'a, I, O> = Box<dyn FnMut(&'a I) -> Option<O> + 'a>;
pub(crate) type CallBench<'a, I, O> = Box<dyn FnMut(&'a I) -> O + 'a>;

pub(crate) struct NamedBench<'a, I, O> {
pub bench_id: BenchId,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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<O> {
pub duration_ns: u64,
pub output: Option<O>,
pub output: O,
}
impl<O> RunResult<O> {
fn new(duration_ns: u64, output: Option<O>) -> Self {
fn new(duration_ns: u64, output: O) -> Self {
RunResult {
duration_ns,
output,
Expand Down Expand Up @@ -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<O> = 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,
Expand Down
4 changes: 2 additions & 2 deletions src/bench_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a, 'runner> BenchGroup<'a, 'runner> {
input: &'a I,
fun: F,
) where
F: Fn(&'a I) -> Option<O> + 'a,
F: Fn(&'a I) -> O + 'a,
{
let bench = NamedBench::new(
self.get_bench_id(bench_name.into()),
Expand All @@ -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<F, S: Into<String>, O: OutputValue + 'static>(&mut self, bench_name: S, fun: F)
where
F: Fn(&'a ()) -> Option<O> + 'static,
F: Fn(&'a ()) -> O + 'static,
{
let bench_name = bench_name.into();
let bench = NamedBench::new(
Expand Down
2 changes: 1 addition & 1 deletion src/bench_input_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<I: 'static, O: OutputValue + 'static> InputGroup<I, O> {
/// The return value of the function will be reported as the `OutputValue` if it is `Some`.
pub fn register<F, S: Into<String>>(&mut self, name: S, fun: F)
where
F: Fn(&I) -> Option<O> + 'static + Clone,
F: Fn(&I) -> O + 'static + Clone,
{
let name = name.into();

Expand Down
2 changes: 1 addition & 1 deletion src/bench_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, S: Into<String>, O: OutputValue>(&mut self, name: S, f: F) -> &mut Self
where
F: Fn(&()) -> Option<O> + 'static,
F: Fn(&()) -> O + 'static,
{
let bench_id = BenchId::from_bench_name(name).runner_name(self.name.as_deref());
let named_bench = NamedBench::new(
Expand Down
5 changes: 5 additions & 0 deletions src/output_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ impl OutputValue for () {
None
}
}
impl OutputValue for Option<u64> {
fn format(&self) -> Option<String> {
self.map(format_with_underscores)
}
}
impl OutputValue for u64 {
fn format(&self) -> Option<String> {
Some(format_with_underscores(*self))
Expand Down
2 changes: 1 addition & 1 deletion src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mod tests {

fn create_res(duration_ns: u64) -> RunResult<u64> {
RunResult {
output: None,
output: 0,
duration_ns,
}
}
Expand Down

0 comments on commit 509cb3f

Please sign in to comment.