mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-16 10:12:02 +00:00
9df075b63c
Accumulators don't currently work across revisions due to a few bugs. This commit adds 2 tests to show the problems and reworks the implementation strategy. We keep track of when the values in an accumulator were pushed and reset the vector to empty when the push occurs in a new revision. We also ignore stale values from old revisions (but update the revision when it is marked as validated). Finally, we treat an accumulator as an untracked read, which is quite conservative but correct. To get better reuse, we would need to (a) somehow determine when different values were pushed, e.g. by hashing or tracked the old values; and (b) have some `DatabaseKeyIndex` we can use to identify "the values pushed by this query". Both of these would add overhead to accumulators and I didn'τ feel like doing it, particularly since the main use case for them is communicating errors and things which are not typically used from within queries.
92 lines
2.1 KiB
Rust
92 lines
2.1 KiB
Rust
//! Accumulator re-use test.
|
|
//!
|
|
//! Tests behavior when a query's only inputs
|
|
//! are the accumulated values from another query.
|
|
|
|
use salsa_2022_tests::{HasLogger, Logger};
|
|
|
|
use expect_test::expect;
|
|
use test_log::test;
|
|
|
|
#[salsa::jar(db = Db)]
|
|
struct Jar(List, Integers, compute);
|
|
|
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
|
|
|
#[salsa::input]
|
|
struct List {
|
|
value: u32,
|
|
next: Option<List>,
|
|
}
|
|
|
|
#[salsa::accumulator]
|
|
struct Integers(u32);
|
|
|
|
#[salsa::tracked]
|
|
fn compute(db: &dyn Db, input: List) -> u32 {
|
|
db.push_log(format!("compute({:?})", input,));
|
|
|
|
// always pushes 0
|
|
Integers::push(db, 0);
|
|
|
|
let result = if let Some(next) = input.next(db) {
|
|
let next_integers = compute::accumulated::<Integers>(db, next);
|
|
let v = input.value(db) + next_integers.iter().sum::<u32>();
|
|
v
|
|
} else {
|
|
input.value(db)
|
|
};
|
|
|
|
// return value changes
|
|
result
|
|
}
|
|
|
|
#[salsa::db(Jar)]
|
|
#[derive(Default)]
|
|
struct Database {
|
|
storage: salsa::Storage<Self>,
|
|
logger: Logger,
|
|
}
|
|
|
|
impl salsa::Database for Database {
|
|
fn salsa_event(&self, _event: salsa::Event) {}
|
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime {
|
|
self.storage.runtime()
|
|
}
|
|
}
|
|
|
|
impl Db for Database {}
|
|
|
|
impl HasLogger for Database {
|
|
fn logger(&self) -> &Logger {
|
|
&self.logger
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test1() {
|
|
let mut db = Database::default();
|
|
|
|
let l1 = List::new(&mut db, 1, None);
|
|
let l2 = List::new(&mut db, 2, Some(l1));
|
|
|
|
assert_eq!(compute(&db, l2), 2);
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"compute(List(Id { value: 2 }))",
|
|
"compute(List(Id { value: 1 }))",
|
|
]"#]]);
|
|
|
|
// When we mutate `l1`, we should re-execute `compute` for `l1`,
|
|
// but we should not have to re-execute `compute` for `l2`.
|
|
// The only inpout for `compute(l1)` is the accumulated values from `l1`,
|
|
// which have not changed.
|
|
l1.set_value(&mut db, 2);
|
|
assert_eq!(compute(&db, l2), 2);
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"compute(List(Id { value: 2 }))",
|
|
"compute(List(Id { value: 1 }))",
|
|
]"#]]);
|
|
}
|