diff --git a/src/debug.rs b/src/debug.rs index 00cb2f24..758fc5af 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -14,15 +14,38 @@ pub trait DebugQueryTable { /// Key of this query. type Key; + /// Value of this query. + type Value; + /// True if salsa thinks that the value for `key` is a /// **constant**, meaning that it can never change, no matter what /// values the inputs take on from this point. fn is_constant(&self, key: Self::Key) -> bool; - /// Get the (current) set of the keys in the query table. - fn keys(&self) -> C + /// Get the (current) set of the entries in the query table. + fn entries(&self) -> C where - C: FromIterator; + C: FromIterator>; +} + +/// An entry from a query table, for debugging and inspecting the table state. +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub struct TableEntry { + /// key of the query + pub key: K, + /// value of the query, if it is stored + pub value: Option, + _for_future_use: (), +} + +impl TableEntry { + pub(crate) fn new(key: K, value: Option) -> TableEntry { + TableEntry { + key, + value, + _for_future_use: (), + } + } } impl DebugQueryTable for QueryTable<'_, DB, Q> @@ -31,15 +54,16 @@ where Q: Query, { type Key = Q::Key; + type Value = Q::Value; fn is_constant(&self, key: Q::Key) -> bool { self.storage.is_constant(self.db, &key) } - fn keys(&self) -> C + fn entries(&self) -> C where - C: FromIterator, + C: FromIterator>, { - self.storage.keys(self.db) + self.storage.entries(self.db) } } diff --git a/src/derived.rs b/src/derived.rs index fc2d070b..dcebf4f2 100644 --- a/src/derived.rs +++ b/src/derived.rs @@ -1,3 +1,4 @@ +use crate::debug::TableEntry; use crate::plumbing::CycleDetected; use crate::plumbing::DatabaseKey; use crate::plumbing::QueryFunction; @@ -162,6 +163,13 @@ where waiting: Default::default(), } } + + fn value(&self) -> Option { + match self { + QueryState::InProgress { .. } => None, + QueryState::Memoized(memo) => memo.value.clone(), + } + } } struct Memo @@ -915,12 +923,14 @@ where } } - fn keys(&self, _db: &DB) -> C + fn entries(&self, _db: &DB) -> C where - C: std::iter::FromIterator, + C: std::iter::FromIterator>, { let map = self.map.read(); - map.keys().cloned().collect() + map.iter() + .map(|(key, query_state)| TableEntry::new(key.clone(), query_state.value())) + .collect() } } diff --git a/src/input.rs b/src/input.rs index 8a1940a4..dc534aef 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,3 +1,4 @@ +use crate::debug::TableEntry; use crate::plumbing::CycleDetected; use crate::plumbing::InputQueryStorageOps; use crate::plumbing::QueryStorageMassOps; @@ -194,12 +195,16 @@ where .unwrap_or(false) } - fn keys(&self, _db: &DB) -> C + fn entries(&self, _db: &DB) -> C where - C: std::iter::FromIterator, + C: std::iter::FromIterator>, { let map = self.map.read(); - map.keys().cloned().collect() + map.iter() + .map(|(key, stamped_value)| { + TableEntry::new(key.clone(), Some(stamped_value.value.clone())) + }) + .collect() } } diff --git a/src/plumbing.rs b/src/plumbing.rs index 2406306e..f7d4f413 100644 --- a/src/plumbing.rs +++ b/src/plumbing.rs @@ -1,5 +1,6 @@ #![allow(missing_docs)] +use crate::debug::TableEntry; use crate::Database; use crate::Query; use crate::QueryTable; @@ -176,10 +177,10 @@ where /// Check if `key` is (currently) believed to be a constant. fn is_constant(&self, db: &DB, key: &Q::Key) -> bool; - /// Get the (current) set of the keys in the query storage - fn keys(&self, db: &DB) -> C + /// Get the (current) set of the entries in the query storage + fn entries(&self, db: &DB) -> C where - C: std::iter::FromIterator; + C: std::iter::FromIterator>; } /// An optional trait that is implemented for "user mutable" storage: diff --git a/tests/gc/derived_tests.rs b/tests/gc/derived_tests.rs index 473d52eb..f58a345c 100644 --- a/tests/gc/derived_tests.rs +++ b/tests/gc/derived_tests.rs @@ -3,16 +3,6 @@ use crate::group::*; use salsa::debug::DebugQueryTable; use salsa::{Database, SweepStrategy}; -macro_rules! assert_keys { - ($db:expr, $($query:expr => ($($key:expr),*),)*) => { - $( - let mut keys = $db.query($query).keys::>(); - keys.sort(); - assert_eq!(keys, vec![$($key),*], "query {:?} had wrong keys", $query); - )* - }; -} - #[test] fn compute_one() { let mut db = db::DatabaseImpl::default(); diff --git a/tests/gc/discard_values.rs b/tests/gc/discard_values.rs index 4de16934..0c10c1da 100644 --- a/tests/gc/discard_values.rs +++ b/tests/gc/discard_values.rs @@ -9,7 +9,7 @@ fn sweep_default() { db.fibonacci(5); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 6); db.salsa_runtime().next_revision(); @@ -20,9 +20,10 @@ fn sweep_default() { // 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(FibonacciQuery).keys(); - k.sort(); - assert_eq!(k, vec![3, 5]); + assert_keys! { + db, + FibonacciQuery => (3, 5), + } // Even though we ran the sweep, 5 is still in cache db.clear_log(); @@ -31,9 +32,11 @@ fn sweep_default() { // Same but we discard values this time. db.sweep_all(SweepStrategy::default().discard_values()); - let mut k: Vec<_> = db.query(FibonacciQuery).keys(); - k.sort(); - assert_eq!(k, vec![3, 5]); + db.sweep_all(SweepStrategy::default()); + assert_keys! { + db, + FibonacciQuery => (3, 5), + } // Now we have to recompute db.clear_log(); diff --git a/tests/gc/main.rs b/tests/gc/main.rs index 663623da..1cc88349 100644 --- a/tests/gc/main.rs +++ b/tests/gc/main.rs @@ -1,3 +1,14 @@ +macro_rules! assert_keys { + ($db:expr, $($query:expr => ($($key:expr),*),)*) => { + $( + let entries = $db.query($query).entries::>(); + let mut keys = entries.into_iter().map(|e| e.key).collect::>(); + keys.sort(); + assert_eq!(keys, vec![$($key),*], "query {:?} had wrong keys", $query); + )* + }; +} + mod db; mod derived_tests; mod discard_values; diff --git a/tests/gc/shallow_constant_tests.rs b/tests/gc/shallow_constant_tests.rs index a6405778..423ce010 100644 --- a/tests/gc/shallow_constant_tests.rs +++ b/tests/gc/shallow_constant_tests.rs @@ -13,7 +13,7 @@ fn one_rev() { db.fibonacci(5); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 6); // Everything was used in this revision, so @@ -28,7 +28,7 @@ fn two_rev_nothing() { db.fibonacci(5); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 6); db.salsa_runtime().next_revision(); @@ -37,7 +37,7 @@ fn two_rev_nothing() { // everything gets collected. db.sweep_all(SweepStrategy::default()); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 0); } @@ -47,7 +47,7 @@ fn two_rev_one_use() { db.fibonacci(5); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 6); db.salsa_runtime().next_revision(); @@ -58,8 +58,10 @@ fn two_rev_one_use() { // hence we keep `fibonacci(5)` but remove 0..=4. db.sweep_all(SweepStrategy::default()); - let k: Vec<_> = db.query(FibonacciQuery).keys(); - assert_eq!(k, vec![5]); + assert_keys! { + db, + FibonacciQuery => (5), + } } #[test] @@ -68,7 +70,7 @@ fn two_rev_two_uses() { db.fibonacci(5); - let k: Vec<_> = db.query(FibonacciQuery).keys(); + let k: Vec<_> = db.query(FibonacciQuery).entries(); assert_eq!(k.len(), 6); db.salsa_runtime().next_revision(); @@ -80,7 +82,8 @@ fn two_rev_two_uses() { // hence we keep 3 and 5 but remove the rest. db.sweep_all(SweepStrategy::default()); - let mut k: Vec<_> = db.query(FibonacciQuery).keys(); - k.sort(); - assert_eq!(k, vec![3, 5]); + assert_keys! { + db, + FibonacciQuery => (3, 5), + } }