diff --git a/src/derived/slot.rs b/src/derived/slot.rs index 91684c83..338605ef 100644 --- a/src/derived/slot.rs +++ b/src/derived/slot.rs @@ -362,7 +362,7 @@ where QueryState::NotComputed => false, QueryState::InProgress { .. } => panic!("query in progress"), QueryState::Memoized(memo) => { - memo.durability.is_constant() && memo.is_still_constant(db) + db.salsa_runtime().is_constant(memo.durability) && memo.is_still_constant(db) } } } diff --git a/src/input.rs b/src/input.rs index a6cf3c6b..0f83f121 100644 --- a/src/input.rs +++ b/src/input.rs @@ -145,9 +145,12 @@ where Ok(value) } - fn is_constant(&self, _db: &DB, key: &Q::Key) -> bool { + fn is_constant(&self, db: &DB, key: &Q::Key) -> bool { self.slot(key) - .map(|slot| slot.stamped_value.read().durability.is_constant()) + .map(|slot| { + db.salsa_runtime() + .is_constant(slot.stamped_value.read().durability) + }) .unwrap_or(false) } @@ -190,7 +193,8 @@ where fn set_constant(&self, db: &DB, key: &Q::Key, database_key: &DB::DatabaseKey, value: Q::Value) { log::debug!("{:?}({:?}) = {:?}", Q::default(), key, value); - self.set_common(db, key, database_key, value, Durability::CONSTANT); + let durability = db.salsa_runtime().max_durability(); + self.set_common(db, key, database_key, value, durability); } } diff --git a/src/runtime.rs b/src/runtime.rs index 530e1b7b..b4329565 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -139,6 +139,16 @@ where db.for_each_query(|query_storage| query_storage.sweep(db, strategy)); } + /// True if `d` represents the max durability level. + pub(crate) fn is_constant(&self, d: Durability) -> bool { + d == self.max_durability() + } + + /// Returns the max durability, used for constants. + pub(crate) fn max_durability(&self) -> Durability { + Durability((self.shared_state.revisions.len() - 1) as u8) + } + /// The unique identifier attached to this `SalsaRuntime`. Each /// snapshotted runtime has a distinct identifier. #[inline] @@ -324,7 +334,8 @@ where }); // Push the active query onto the stack. - let active_query = self.local_state.push_query(database_key); + let max_durability = self.max_durability(); + let active_query = self.local_state.push_query(database_key, max_durability); // Execute user's code, accumulating inputs etc. let value = execute(); @@ -569,10 +580,10 @@ pub(crate) struct ComputedQueryResult { } impl ActiveQuery { - fn new(database_key: DB::DatabaseKey) -> Self { + fn new(database_key: DB::DatabaseKey, max_durability: Durability) -> Self { ActiveQuery { database_key, - durability: Durability::CONSTANT, + durability: max_durability, changed_at: Revision::start(), dependencies: Some(FxIndexSet::default()), } @@ -611,14 +622,9 @@ pub(crate) struct Durability(u8); impl Durability { pub(crate) const MUTABLE: Durability = Durability(0); - pub(crate) const CONSTANT: Durability = Durability(1); - - pub(crate) fn is_constant(self) -> bool { - self == Self::CONSTANT - } pub(crate) fn and(self, c: Durability) -> Durability { - Durability(self.0 & c.0) + self.min(c) } fn index(self) -> usize { diff --git a/src/runtime/local_state.rs b/src/runtime/local_state.rs index 87274177..d6175277 100644 --- a/src/runtime/local_state.rs +++ b/src/runtime/local_state.rs @@ -29,9 +29,13 @@ impl Default for LocalState { } impl LocalState { - pub(super) fn push_query(&self, database_key: &DB::DatabaseKey) -> ActiveQueryGuard<'_, DB> { + pub(super) fn push_query( + &self, + database_key: &DB::DatabaseKey, + max_durability: Durability, + ) -> ActiveQueryGuard<'_, DB> { let mut query_stack = self.query_stack.borrow_mut(); - query_stack.push(ActiveQuery::new(database_key.clone())); + query_stack.push(ActiveQuery::new(database_key.clone(), max_durability)); ActiveQueryGuard { local_state: self, push_len: query_stack.len(),