Skip to content

feat: add support consts with divan #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/divan_compat/benches/basic_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ fn mut_borrow(bencher: Bencher) {
});
}

// Examples taken from the docs: https://docs.rs/divan/latest/divan/attr.bench.html#consts
mod const_bench {
const LEN: usize = 42;

const fn len() -> usize {
4
}

#[codspeed_divan_compat::bench(consts = [1000, LEN, len()])]
fn init_array<const N: usize>() -> [i32; N] {
let mut result = [0; N];

#[allow(clippy::needless_range_loop)]
for i in 0..N {
result[i] = divan::black_box(i as i32);
}

result
}

const SIZES: &[usize] = &[1, 10, LEN, len()];
#[codspeed_divan_compat::bench(consts = SIZES)]
fn bench_array1<const N: usize>() -> [i32; N] {
init_array::<N>()
}

#[codspeed_divan_compat::bench(consts = SIZES)]
fn bench_array2<const N: usize>() -> [i32; N] {
init_array::<N>()
}
}

fn main() {
codspeed_divan_compat::main();
}
2 changes: 1 addition & 1 deletion crates/divan_compat/src/compat/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
BenchArgsRunner,
};

pub use generic::{EntryType, GenericBenchEntry};
pub use generic::{EntryConst, EntryType, GenericBenchEntry};

/// Benchmark entries generated by `#[divan::bench]`.
///
Expand Down
6 changes: 3 additions & 3 deletions crates/divan_compat/src/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pub mod __private {
pub use super::{
bench::{BenchArgs, BenchOptions},
entry::{
BenchEntry, BenchEntryRunner, EntryList, EntryLocation, EntryMeta, EntryType,
GenericBenchEntry, GroupEntry, BENCH_ENTRIES, GROUP_ENTRIES,
BenchEntry, BenchEntryRunner, EntryConst, EntryList, EntryLocation, EntryMeta,
EntryType, GenericBenchEntry, GroupEntry, BENCH_ENTRIES, GROUP_ENTRIES,
},
};

pub use divan::__private::{Arg, ToStringHelper};
pub use divan::__private::{shrink_array, Arg, ToStringHelper};
}

mod bench;
Expand Down