diff --git a/components/salsa-2022/src/function/fetch.rs b/components/salsa-2022/src/function/fetch.rs index a6b30dc9..1bbda7a5 100644 --- a/components/salsa-2022/src/function/fetch.rs +++ b/components/salsa-2022/src/function/fetch.rs @@ -1,10 +1,6 @@ use arc_swap::Guard; -use crate::{ - database::AsSalsaDatabase, - runtime::{local_state::QueryOrigin, StampedValue}, - AsId, Database, -}; +use crate::{database::AsSalsaDatabase, runtime::StampedValue, AsId, Database}; use super::{Configuration, DynDb, FunctionIngredient}; @@ -94,23 +90,6 @@ where } fn evict(&self, key: C::Key) { - if let Some(memo) = self.memo_map.get(key) { - match memo.revisions.origin { - QueryOrigin::Assigned(_) - | QueryOrigin::DerivedUntracked(_) - | QueryOrigin::BaseInput - | QueryOrigin::Field => { - // Careful: Cannot evict memos whose values were - // assigned as output of another query - // or those with untracked inputs - // as their values cannot be reconstructed. - return; - } - - QueryOrigin::Derived(_) => { - self.delete_memo(key); - } - } - } + self.memo_map.evict(key); } } diff --git a/components/salsa-2022/src/function/memo.rs b/components/salsa-2022/src/function/memo.rs index 50f4ec42..45b424c2 100644 --- a/components/salsa-2022/src/function/memo.rs +++ b/components/salsa-2022/src/function/memo.rs @@ -42,6 +42,40 @@ impl MemoMap { pub(super) fn get(&self, key: K) -> Option>>> { self.map.get(&key).map(|v| v.load()) } + + /// Evicts the existing memo for the given key, replacing it + /// with an equivalent memo that has no value. If the memo is untracked, BaseInput, + /// or has values assigned as output of another query, this has no effect. + pub(super) fn evict(&self, key: K) { + use crate::runtime::local_state::QueryOrigin; + use dashmap::mapref::entry::Entry::*; + + if let Occupied(entry) = self.map.entry(key) { + let memo = entry.get().load(); + match memo.revisions.origin { + QueryOrigin::Assigned(_) + | QueryOrigin::DerivedUntracked(_) + | QueryOrigin::BaseInput + | QueryOrigin::Field => { + // Careful: Cannot evict memos whose values were + // assigned as output of another query + // or those with untracked inputs + // as their values cannot be reconstructed. + return; + } + + QueryOrigin::Derived(_) => { + let memo_evicted = Arc::new(Memo::new( + None::, + memo.verified_at.load(), + memo.revisions.clone(), + )); + + entry.get().store(memo_evicted); + } + } + } + } } #[derive(Debug)]