diff --git a/tests/gc/db.rs b/tests/gc/db.rs index 5883ebfd..7ccaa5b3 100644 --- a/tests/gc/db.rs +++ b/tests/gc/db.rs @@ -1,8 +1,10 @@ use crate::group; +use crate::log::{HasLog, Log}; #[derive(Default)] pub struct DatabaseImpl { runtime: salsa::Runtime, + log: Log, } impl salsa::Database for DatabaseImpl { @@ -24,3 +26,34 @@ salsa::database_storage! { } } } + +impl DatabaseImpl { + pub(crate) fn clear_log(&self) { + self.log().take(); + } + + pub(crate) fn assert_log(&self, expected_log: &[&str]) { + let expected_text = &format!("{:#?}", expected_log); + let actual_text = &format!("{:#?}", self.log().take()); + + if expected_text == actual_text { + return; + } + + for diff in diff::lines(expected_text, actual_text) { + match diff { + diff::Result::Left(l) => println!("-{}", l), + diff::Result::Both(l, _) => println!(" {}", l), + diff::Result::Right(r) => println!("+{}", r), + } + } + + panic!("incorrect log results"); + } +} + +impl HasLog for DatabaseImpl { + fn log(&self) -> &Log { + &self.log + } +} diff --git a/tests/gc/discard_values.rs b/tests/gc/discard_values.rs new file mode 100644 index 00000000..2de74f40 --- /dev/null +++ b/tests/gc/discard_values.rs @@ -0,0 +1,49 @@ +use crate::db; +use crate::group::{Fibonacci, GcDatabase}; +use salsa::debug::DebugQueryTable; +use salsa::{Database, SweepStrategy}; + +#[test] +fn sweep_default() { + let db = db::DatabaseImpl::default(); + + db.fibonacci(5); + + let k: Vec<_> = db.query(Fibonacci).keys(); + assert_eq!(k.len(), 6); + + db.salsa_runtime().next_revision(); + + db.fibonacci(5); + db.fibonacci(3); + + // fibonacci is a constant, so it will not be invalidated, + // hence we keep 3 and 5 but remove the rest. + db.sweep_all(SweepStrategy::default()); + let mut k: Vec<_> = db.query(Fibonacci).keys(); + k.sort(); + assert_eq!(k, vec![3, 5]); + + // Even though we ran the sweep, 5 is still in cache + db.clear_log(); + db.fibonacci(5); + db.assert_log(&[]); + + // Same but we discard values this time. + db.sweep_all(SweepStrategy::default().discard_values()); + let mut k: Vec<_> = db.query(Fibonacci).keys(); + k.sort(); + assert_eq!(k, vec![3, 5]); + + // Now we have to recompute + db.clear_log(); + db.fibonacci(5); + db.assert_log(&[ + "fibonacci(5)", + "fibonacci(4)", + "fibonacci(3)", + "fibonacci(2)", + "fibonacci(1)", + "fibonacci(0)", + ]); +} diff --git a/tests/gc/group.rs b/tests/gc/group.rs index 9be5f9e2..8c4aa41f 100644 --- a/tests/gc/group.rs +++ b/tests/gc/group.rs @@ -1,5 +1,7 @@ +use crate::log::HasLog; + salsa::query_group! { - pub(crate) trait GcDatabase: salsa::Database { + pub(crate) trait GcDatabase: salsa::Database + HasLog { fn min() -> usize { type Min; storage input; @@ -34,6 +36,7 @@ salsa::query_group! { } fn fibonacci(db: &impl GcDatabase, key: usize) -> usize { + db.log().add(format!("fibonacci({:?})", key)); if key == 0 { 0 } else if key == 1 { @@ -44,6 +47,7 @@ fn fibonacci(db: &impl GcDatabase, key: usize) -> usize { } fn triangular(db: &impl GcDatabase, key: usize) -> usize { + db.log().add(format!("triangular({:?})", key)); if key == 0 { 0 } else { @@ -52,6 +56,7 @@ fn triangular(db: &impl GcDatabase, key: usize) -> usize { } fn compute(db: &impl GcDatabase, key: usize) -> usize { + db.log().add(format!("compute({:?})", key)); if db.use_triangular(key) { db.triangular(key) } else { @@ -60,5 +65,6 @@ fn compute(db: &impl GcDatabase, key: usize) -> usize { } fn compute_all(db: &impl GcDatabase) -> Vec { + db.log().add("compute_all()"); (db.min()..db.max()).map(|v| db.compute(v)).collect() } diff --git a/tests/gc/log.rs b/tests/gc/log.rs new file mode 100644 index 00000000..b9e10889 --- /dev/null +++ b/tests/gc/log.rs @@ -0,0 +1,20 @@ +use std::cell::RefCell; + +pub(crate) trait HasLog { + fn log(&self) -> &Log; +} + +#[derive(Default)] +pub(crate) struct Log { + data: RefCell>, +} + +impl Log { + pub(crate) fn add(&self, text: impl Into) { + self.data.borrow_mut().push(text.into()); + } + + pub(crate) fn take(&self) -> Vec { + std::mem::replace(&mut *self.data.borrow_mut(), vec![]) + } +} diff --git a/tests/gc/main.rs b/tests/gc/main.rs index 04300a1e..663623da 100644 --- a/tests/gc/main.rs +++ b/tests/gc/main.rs @@ -1,4 +1,6 @@ mod db; mod derived_tests; +mod discard_values; mod group; +mod log; mod shallow_constant_tests;