Skip to content

Commit

Permalink
Use proper setup for compare benches
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 4, 2025
1 parent 418e6ea commit 016094c
Showing 1 changed file with 68 additions and 37 deletions.
105 changes: 68 additions & 37 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::mem::transmute;

use codspeed_criterion_compat::{
criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion,
};
use salsa::Setter;

#[salsa::input]
Expand Down Expand Up @@ -27,23 +31,28 @@ fn mutating_inputs(c: &mut Criterion) {
> = c.benchmark_group("Mutating Inputs");

for n in &[10, 20, 30] {
let mut db = salsa::DatabaseImpl::default();
let base_string = "hello, world!".to_owned();
let base_len = base_string.len();

let string = base_string.clone().repeat(*n);
let new_len = string.len();

group.bench_function(BenchmarkId::new("mutating", n), |b| {
b.iter(|| {
let input = Input::new(&db, base_string.clone());
let actual_len = length(&db, input);
assert_eq!(base_len, actual_len);

input.set_text(&mut db).to(string.clone());
let actual_len = length(&db, input);
assert_eq!(new_len, actual_len);
})
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();
let base_string = "hello, world!".to_owned();
let base_len = base_string.len();

let string = base_string.clone().repeat(*n);
let new_len = string.len();
(db, base_string, base_len, string, new_len)
},
|&mut (ref mut db, ref base_string, base_len, ref string, new_len)| {
let input = Input::new(db, base_string.clone());
let actual_len = length(db, input);
assert_eq!(base_len, actual_len);

input.set_text(db).to(string.clone());
let actual_len = length(db, input);
assert_eq!(new_len, actual_len);
},
BatchSize::SmallInput,
)
});
}

Expand All @@ -55,36 +64,58 @@ fn inputs(c: &mut Criterion) {
codspeed_criterion_compat::measurement::WallTime,
> = c.benchmark_group("Mutating Inputs");

let db = salsa::DatabaseImpl::default();
group.bench_function(BenchmarkId::new("new", "InternedInput"), |b| {
b.iter(|| {
let input: InternedInput = InternedInput::new(&db, "hello, world!".to_owned());
interned_length(&db, input);
})
b.iter_batched_ref(
salsa::DatabaseImpl::default,
|db| {
let input: InternedInput = InternedInput::new(db, "hello, world!".to_owned());
interned_length(db, input);
},
BatchSize::SmallInput,
)
});

let db = salsa::DatabaseImpl::default();
group.bench_function(BenchmarkId::new("amortized", "InternedInput"), |b| {
let input = InternedInput::new(&db, "hello, world!".to_owned());
let _ = interned_length(&db, input);

b.iter(|| interned_length(&db, input));
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();
// we can't pass this along otherwise, and the lifetime is generally informational
let input: InternedInput<'static> =
unsafe { transmute(InternedInput::new(&db, "hello, world!".to_owned())) };
let _ = interned_length(&db, input);
(db, input)
},
|&mut (ref db, input)| {
interned_length(db, input);
},
BatchSize::SmallInput,
)
});

let db = salsa::DatabaseImpl::default();
group.bench_function(BenchmarkId::new("new", "Input"), |b| {
b.iter(|| {
let input = Input::new(&db, "hello, world!".to_owned());
length(&db, input);
})
b.iter_batched_ref(
salsa::DatabaseImpl::default,
|db| {
let input = Input::new(db, "hello, world!".to_owned());
length(db, input);
},
BatchSize::SmallInput,
)
});

let db = salsa::DatabaseImpl::default();
group.bench_function(BenchmarkId::new("amortized", "Input"), |b| {
let input = Input::new(&db, "hello, world!".to_owned());
let _ = length(&db, input);

b.iter(|| length(&db, input));
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();
let input = Input::new(&db, "hello, world!".to_owned());
let _ = length(&db, input);
(db, input)
},
|&mut (ref db, input)| {
length(db, input);
},
BatchSize::SmallInput,
)
});

group.finish();
Expand Down

0 comments on commit 016094c

Please sign in to comment.