Skip to content

Commit a50047a

Browse files
asibahimjclarke94
andauthored
perf: avoid clone in bencher (fspoettel#67)
--------- Co-authored-by: Matt Clarke <[email protected]>
1 parent 3abf88c commit a50047a

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/template/runner.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{cmp, env, process};
99
use crate::template::ANSI_BOLD;
1010
use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET};
1111

12-
pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
12+
pub fn run_part<I: Copy, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
1313
let part_str = format!("Part {part}");
1414

1515
let (result, duration, samples) =
@@ -25,15 +25,13 @@ pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, d
2525
/// Run a solution part. The behavior differs depending on whether we are running a release or debug build:
2626
/// 1. in debug, the function is executed once.
2727
/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.)
28-
fn run_timed<I: Clone, T>(
28+
fn run_timed<I: Copy, T>(
2929
func: impl Fn(I) -> T,
3030
input: I,
3131
hook: impl Fn(&T),
3232
) -> (T, Duration, u128) {
3333
let timer = Instant::now();
3434
let result = {
35-
let input = input.clone();
36-
3735
#[cfg(feature = "dhat-heap")]
3836
let _profiler = dhat::Profiler::new_heap();
3937

@@ -52,7 +50,7 @@ fn run_timed<I: Clone, T>(
5250
(result, run.0, run.1)
5351
}
5452

55-
fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
53+
fn bench<I: Copy, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
5654
let mut stdout = stdout();
5755

5856
print!(" > {ANSI_ITALIC}benching{ANSI_RESET}");
@@ -64,10 +62,8 @@ fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) ->
6462
let mut timers: Vec<Duration> = vec![];
6563

6664
for _ in 0..bench_iterations {
67-
// need a clone here to make the borrow checker happy.
68-
let cloned = input.clone();
6965
let timer = Instant::now();
70-
black_box(func(black_box(cloned)));
66+
black_box(func(black_box(input)));
7167
timers.push(timer.elapsed());
7268
}
7369

0 commit comments

Comments
 (0)