Skip to content

Commit

Permalink
internal: add some initial input benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbarsky committed Jul 23, 2024
1 parent 2f4f80f commit d5b492c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ ordered-float = "3.0"
rustversion = "1.0"
test-log = "0.2.11"
trybuild = "1.0"
criterion = "0.5.1"

[[bench]]
name = "compare"
harness = false

[workspace]
members = [ "components/salsa-macro-rules","components/salsa-macros"]
97 changes: 97 additions & 0 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use salsa::Setter;

#[salsa::db]
pub trait Db: salsa::Database {}

#[salsa::db]
#[derive(Default)]
pub struct Database {
storage: salsa::Storage<Self>,
}

#[salsa::db]
impl salsa::Database for Database {}

#[salsa::db]
impl Db for Database {}

#[salsa::input]
pub struct Input {
pub text: String,
}

#[salsa::tracked]
pub fn length(db: &dyn Db, input: Input) -> usize {
input.text(db).len()
}

#[salsa::interned]
pub struct InternedInput<'db> {
pub text: String,
}

#[salsa::tracked]
pub fn interned_length<'db>(db: &'db dyn Db, input: InternedInput<'db>) -> usize {
input.text(db).len()
}

fn inputs(c: &mut Criterion) {
let mut group = c.benchmark_group("Inputs");
let mut db = Database::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);
})
});

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

b.iter(|| interned_length(&db, input));
});

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

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));
});

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

let string = std::iter::repeat(base_string.clone())
.take(*n)
.collect::<String>();
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);
})
});
}

group.finish();
}

criterion_group!(benches, inputs);
criterion_main!(benches);

0 comments on commit d5b492c

Please sign in to comment.