|
| 1 | +use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 2 | +use salsa::Setter; |
| 3 | + |
| 4 | +#[salsa::input] |
| 5 | +pub struct Input { |
| 6 | + pub text: String, |
| 7 | +} |
| 8 | + |
| 9 | +#[salsa::tracked] |
| 10 | +pub fn length(db: &dyn salsa::Database, input: Input) -> usize { |
| 11 | + input.text(db).len() |
| 12 | +} |
| 13 | + |
| 14 | +#[salsa::interned] |
| 15 | +pub struct InternedInput<'db> { |
| 16 | + pub text: String, |
| 17 | +} |
| 18 | + |
| 19 | +#[salsa::tracked] |
| 20 | +pub fn interned_length<'db>(db: &'db dyn salsa::Database, input: InternedInput<'db>) -> usize { |
| 21 | + input.text(db).len() |
| 22 | +} |
| 23 | + |
| 24 | +fn mutating_inputs(c: &mut Criterion) { |
| 25 | + let mut group: codspeed_criterion_compat::BenchmarkGroup< |
| 26 | + codspeed_criterion_compat::measurement::WallTime, |
| 27 | + > = c.benchmark_group("Mutating Inputs"); |
| 28 | + |
| 29 | + let mut db = salsa::DatabaseImpl::default(); |
| 30 | + |
| 31 | + for n in &[10, 20, 30] { |
| 32 | + let base_string = "hello, world!".to_owned(); |
| 33 | + let base_len = base_string.len(); |
| 34 | + |
| 35 | + let string = base_string.clone().repeat(*n); |
| 36 | + let new_len = string.len(); |
| 37 | + |
| 38 | + group.bench_function(BenchmarkId::new("mutating", n), |b| { |
| 39 | + b.iter(|| { |
| 40 | + let input = Input::new(&db, base_string.clone()); |
| 41 | + let actual_len = length(&db, input); |
| 42 | + assert_eq!(base_len, actual_len); |
| 43 | + |
| 44 | + input.set_text(&mut db).to(string.clone()); |
| 45 | + let actual_len = length(&db, input); |
| 46 | + assert_eq!(new_len, actual_len); |
| 47 | + }) |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + group.finish(); |
| 52 | +} |
| 53 | + |
| 54 | +fn inputs(c: &mut Criterion) { |
| 55 | + let mut group: codspeed_criterion_compat::BenchmarkGroup< |
| 56 | + codspeed_criterion_compat::measurement::WallTime, |
| 57 | + > = c.benchmark_group("Mutating Inputs"); |
| 58 | + |
| 59 | + let db = salsa::DatabaseImpl::default(); |
| 60 | + |
| 61 | + group.bench_function(BenchmarkId::new("new", "InternedInput"), |b| { |
| 62 | + b.iter(|| { |
| 63 | + let input: InternedInput = InternedInput::new(&db, "hello, world!".to_owned()); |
| 64 | + interned_length(&db, input); |
| 65 | + }) |
| 66 | + }); |
| 67 | + |
| 68 | + group.bench_function(BenchmarkId::new("amortized", "InternedInput"), |b| { |
| 69 | + let input = InternedInput::new(&db, "hello, world!".to_owned()); |
| 70 | + let _ = interned_length(&db, input); |
| 71 | + |
| 72 | + b.iter(|| interned_length(&db, input)); |
| 73 | + }); |
| 74 | + |
| 75 | + group.bench_function(BenchmarkId::new("new", "Input"), |b| { |
| 76 | + b.iter(|| { |
| 77 | + let input = Input::new(&db, "hello, world!".to_owned()); |
| 78 | + length(&db, input); |
| 79 | + }) |
| 80 | + }); |
| 81 | + |
| 82 | + group.bench_function(BenchmarkId::new("amortized", "Input"), |b| { |
| 83 | + let input = Input::new(&db, "hello, world!".to_owned()); |
| 84 | + let _ = length(&db, input); |
| 85 | + |
| 86 | + b.iter(|| length(&db, input)); |
| 87 | + }); |
| 88 | + |
| 89 | + group.finish(); |
| 90 | +} |
| 91 | + |
| 92 | +criterion_group!(benches, mutating_inputs, inputs); |
| 93 | +criterion_main!(benches); |
0 commit comments