Skip to content

Commit b09cf96

Browse files
feat(divan_compat): share instance of codspeed helper across benches
1 parent 428b726 commit b09cf96

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

crates/divan_compat/src/compat/bench/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use self::{
1111
};
1212

1313
use codspeed::codspeed::CodSpeed;
14-
use std::{cell::RefCell, rc::Rc};
14+
use std::cell::RefCell;
1515

1616
/// Using this in place of `()` for `GenI` prevents `Bencher::with_inputs` from
1717
/// working with `()` unintentionally.
@@ -23,18 +23,18 @@ pub struct BencherConfig<GenI = Unit> {
2323
}
2424

2525
pub struct Bencher<'a, 'b, C = BencherConfig> {
26-
pub(crate) codspeed: Rc<RefCell<CodSpeed>>,
26+
pub(crate) codspeed: &'a RefCell<CodSpeed>,
2727
pub(crate) uri: String,
2828
pub(crate) config: C,
29-
pub(crate) _marker: std::marker::PhantomData<&'a &'b ()>,
29+
pub(crate) _marker: std::marker::PhantomData<&'b ()>,
3030
}
3131

3232
#[allow(clippy::needless_lifetimes)]
3333
impl<'a, 'b> Bencher<'a, 'b> {
34-
pub(crate) fn new(uri: String) -> Self {
34+
pub(crate) fn new(codspeed: &'a RefCell<CodSpeed>, uri: String) -> Self {
3535
Self {
36-
codspeed: Rc::new(RefCell::new(CodSpeed::new())),
3736
config: BencherConfig { gen_input: Unit },
37+
codspeed,
3838
uri,
3939
_marker: std::marker::PhantomData,
4040
}

crates/divan_compat/src/compat/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ mod entry;
1414
mod uri;
1515
mod util;
1616

17+
use std::{cell::RefCell, rc::Rc};
18+
1719
pub use bench::*;
20+
use codspeed::codspeed::CodSpeed;
1821

1922
pub fn main() {
2023
// Outlined steps of original divan::main and their equivalent in codspeed instrumented mode
@@ -31,6 +34,7 @@ pub fn main() {
3134
// filtering is managed by the `cargo-codspeed` wrappers before we reach this point.
3235

3336
// 4. Scan the tree and execute benchmarks
37+
let codspeed = Rc::new(RefCell::new(CodSpeed::new()));
3438
for entry in bench_entries.iter() {
3539
let entry_uri = uri::generate(entry.meta.display_name, &entry.meta);
3640

@@ -42,14 +46,14 @@ pub fn main() {
4246
}
4347
match entry.bench {
4448
entry::BenchEntryRunner::Plain(bench_fn) => {
45-
bench_fn(bench::Bencher::new(entry_uri));
49+
bench_fn(bench::Bencher::new(&codspeed, entry_uri));
4650
}
4751
entry::BenchEntryRunner::Args(bench_runner) => {
4852
let bench_runner = bench_runner();
4953

5054
for (arg_index, arg_name) in bench_runner.arg_names().iter().enumerate() {
5155
let uri_with_arg = uri::append_arg(entry_uri.as_str(), arg_name);
52-
let bencher = bench::Bencher::new(uri_with_arg);
56+
let bencher = bench::Bencher::new(&codspeed, uri_with_arg);
5357

5458
bench_runner.bench(bencher, arg_index);
5559
}

0 commit comments

Comments
 (0)