Merge pull request #643 from Veykril/veykril/push-xkzvyxqwxurm
Some checks are pending
Book / Book (push) Waiting to run
Book / Deploy (push) Blocked by required conditions
Test / Test (push) Waiting to run
Test / Miri (push) Waiting to run
Test / Benchmarks (push) Waiting to run

Fix compare benchmarks not benching the right things
This commit is contained in:
Lukas Wirth 2025-01-04 13:34:22 +00:00 committed by GitHub
commit 6c0dd82119
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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; use salsa::Setter;
#[salsa::input] #[salsa::input]
@ -26,25 +30,30 @@ fn mutating_inputs(c: &mut Criterion) {
codspeed_criterion_compat::measurement::WallTime, codspeed_criterion_compat::measurement::WallTime,
> = c.benchmark_group("Mutating Inputs"); > = c.benchmark_group("Mutating Inputs");
let mut db = salsa::DatabaseImpl::default();
for n in &[10, 20, 30] { for n in &[10, 20, 30] {
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| { group.bench_function(BenchmarkId::new("mutating", n), |b| {
b.iter(|| { b.iter_batched_ref(
let input = Input::new(&db, base_string.clone()); || {
let actual_len = length(&db, input); let db = salsa::DatabaseImpl::default();
assert_eq!(base_len, actual_len); let base_string = "hello, world!".to_owned();
let base_len = base_string.len();
input.set_text(&mut db).to(string.clone()); let string = base_string.clone().repeat(*n);
let actual_len = length(&db, input); let new_len = string.len();
assert_eq!(new_len, actual_len);
}) let input = Input::new(&db, base_string.clone());
let actual_len = length(&db, input);
assert_eq!(base_len, actual_len);
(db, input, string, new_len)
},
|&mut (ref mut db, input, ref string, new_len)| {
input.set_text(db).to(string.clone());
let actual_len = length(db, input);
assert_eq!(new_len, actual_len);
},
BatchSize::SmallInput,
)
}); });
} }
@ -56,34 +65,58 @@ fn inputs(c: &mut Criterion) {
codspeed_criterion_compat::measurement::WallTime, codspeed_criterion_compat::measurement::WallTime,
> = c.benchmark_group("Mutating Inputs"); > = c.benchmark_group("Mutating Inputs");
let db = salsa::DatabaseImpl::default();
group.bench_function(BenchmarkId::new("new", "InternedInput"), |b| { group.bench_function(BenchmarkId::new("new", "InternedInput"), |b| {
b.iter(|| { b.iter_batched_ref(
let input: InternedInput = InternedInput::new(&db, "hello, world!".to_owned()); salsa::DatabaseImpl::default,
interned_length(&db, input); |db| {
}) let input: InternedInput = InternedInput::new(db, "hello, world!".to_owned());
interned_length(db, input);
},
BatchSize::SmallInput,
)
}); });
group.bench_function(BenchmarkId::new("amortized", "InternedInput"), |b| { group.bench_function(BenchmarkId::new("amortized", "InternedInput"), |b| {
let input = InternedInput::new(&db, "hello, world!".to_owned()); b.iter_batched_ref(
let _ = interned_length(&db, input); || {
let db = salsa::DatabaseImpl::default();
b.iter(|| interned_length(&db, input)); // 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,
)
}); });
group.bench_function(BenchmarkId::new("new", "Input"), |b| { group.bench_function(BenchmarkId::new("new", "Input"), |b| {
b.iter(|| { b.iter_batched_ref(
let input = Input::new(&db, "hello, world!".to_owned()); salsa::DatabaseImpl::default,
length(&db, input); |db| {
}) let input = Input::new(db, "hello, world!".to_owned());
length(db, input);
},
BatchSize::SmallInput,
)
}); });
group.bench_function(BenchmarkId::new("amortized", "Input"), |b| { group.bench_function(BenchmarkId::new("amortized", "Input"), |b| {
let input = Input::new(&db, "hello, world!".to_owned()); b.iter_batched_ref(
let _ = length(&db, input); || {
let db = salsa::DatabaseImpl::default();
b.iter(|| length(&db, input)); 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(); group.finish();