From 297dd2bb3264b6e39ec9506ed448380e11149eef Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sun, 17 Nov 2024 16:44:10 +0100 Subject: [PATCH] Add accumulator benchmark --- Cargo.toml | 5 ++++ benches/accumulator.rs | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 benches/accumulator.rs diff --git a/Cargo.toml b/Cargo.toml index 5e1f5b55..994fbf30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" license = "Apache-2.0 OR MIT" repository = "https://github.com/salsa-rs/salsa" description = "A generic framework for on-demand, incrementalized computation (experimental)" +rust-version = "1.76" [dependencies] arc-swap = "1" @@ -44,5 +45,9 @@ harness = false name = "incremental" harness = false +[[bench]] +name = "accumulator" +harness = false + [workspace] members = ["components/salsa-macro-rules", "components/salsa-macros"] diff --git a/benches/accumulator.rs b/benches/accumulator.rs new file mode 100644 index 00000000..16ca7f71 --- /dev/null +++ b/benches/accumulator.rs @@ -0,0 +1,64 @@ +use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; +use salsa::Accumulator; + +#[salsa::input] +struct Input { + expressions: usize, +} + +#[allow(dead_code)] +#[salsa::accumulator] +struct Diagnostic(String); + +#[salsa::interned] +struct Expression<'db> { + number: usize, +} + +#[salsa::tracked] +fn root<'db>(db: &'db dyn salsa::Database, input: Input) -> Vec { + (0..input.expressions(db)) + .map(|i| infer_expression(db, Expression::new(db, i))) + .collect() +} + +#[salsa::tracked] +fn infer_expression<'db>(db: &'db dyn salsa::Database, expression: Expression<'db>) -> usize { + let number = expression.number(db); + + if number % 10 == 0 { + Diagnostic(format!("Number is {number}")).accumulate(db); + } + + if number != 0 && number % 2 == 0 { + let sub_expression = Expression::new(db, number / 2); + let _ = infer_expression(db, sub_expression); + } + + number +} + +fn accumulator(criterion: &mut Criterion) { + criterion.bench_function("accumulator", |b| { + b.iter_batched_ref( + || { + let db = salsa::DatabaseImpl::new(); + let input = Input::new(&db, 10_000); + // Pre-warm + let _ = root(&db, input); + (db, input) + }, + |(db, input)| { + // Measure the cost of collecting accumulators ignoring the cost of running the + // query itself. + let diagnostics = root::accumulated::(db, *input); + + assert_eq!(diagnostics.len(), 1000); + }, + BatchSize::SmallInput, + ); + }); +} + +criterion_group!(benches, accumulator); +criterion_main!(benches);