From a50047ab0d60eb3499e6dc29565e0fad9b5e80b7 Mon Sep 17 00:00:00 2001 From: Abdul-Rahman Sibahi Date: Tue, 3 Dec 2024 00:53:46 +0300 Subject: [PATCH] perf: avoid clone in bencher (#67) --------- Co-authored-by: Matt Clarke --- src/template/runner.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/template/runner.rs b/src/template/runner.rs index 0a48767..43a2bb4 100644 --- a/src/template/runner.rs +++ b/src/template/runner.rs @@ -9,7 +9,7 @@ use std::{cmp, env, process}; use crate::template::ANSI_BOLD; use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET}; -pub fn run_part(func: impl Fn(I) -> Option, input: I, day: Day, part: u8) { +pub fn run_part(func: impl Fn(I) -> Option, input: I, day: Day, part: u8) { let part_str = format!("Part {part}"); let (result, duration, samples) = @@ -25,15 +25,13 @@ pub fn run_part(func: impl Fn(I) -> Option, input: I, d /// Run a solution part. The behavior differs depending on whether we are running a release or debug build: /// 1. in debug, the function is executed once. /// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.) -fn run_timed( +fn run_timed( func: impl Fn(I) -> T, input: I, hook: impl Fn(&T), ) -> (T, Duration, u128) { let timer = Instant::now(); let result = { - let input = input.clone(); - #[cfg(feature = "dhat-heap")] let _profiler = dhat::Profiler::new_heap(); @@ -52,7 +50,7 @@ fn run_timed( (result, run.0, run.1) } -fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) { +fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) { let mut stdout = stdout(); print!(" > {ANSI_ITALIC}benching{ANSI_RESET}"); @@ -64,10 +62,8 @@ fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> let mut timers: Vec = vec![]; for _ in 0..bench_iterations { - // need a clone here to make the borrow checker happy. - let cloned = input.clone(); let timer = Instant::now(); - black_box(func(black_box(cloned))); + black_box(func(black_box(input))); timers.push(timer.elapsed()); }