Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/salsa/src/derived.rs')
-rw-r--r--crates/salsa/src/derived.rs98
1 files changed, 14 insertions, 84 deletions
diff --git a/crates/salsa/src/derived.rs b/crates/salsa/src/derived.rs
index fd31ab2041..8b2fdd6b19 100644
--- a/crates/salsa/src/derived.rs
+++ b/crates/salsa/src/derived.rs
@@ -1,9 +1,7 @@
use crate::debug::TableEntry;
use crate::durability::Durability;
use crate::hash::FxIndexMap;
-use crate::lru::Lru;
use crate::plumbing::DerivedQueryStorageOps;
-use crate::plumbing::LruQueryStorageOps;
use crate::plumbing::QueryFunction;
use crate::plumbing::QueryStorageMassOps;
use crate::plumbing::QueryStorageOps;
@@ -13,7 +11,6 @@ use crate::{Database, DatabaseKeyIndex, QueryDb, Revision};
use parking_lot::RwLock;
use std::borrow::Borrow;
use std::hash::Hash;
-use std::marker::PhantomData;
use triomphe::Arc;
mod slot;
@@ -22,79 +19,33 @@ use slot::Slot;
/// Memoized queries store the result plus a list of the other queries
/// that they invoked. This means we can avoid recomputing them when
/// none of those inputs have changed.
-pub type MemoizedStorage<Q> = DerivedStorage<Q, AlwaysMemoizeValue>;
-
-/// "Dependency" queries just track their dependencies and not the
-/// actual value (which they produce on demand). This lessens the
-/// storage requirements.
-pub type DependencyStorage<Q> = DerivedStorage<Q, NeverMemoizeValue>;
+pub type MemoizedStorage<Q> = DerivedStorage<Q>;
/// Handles storage where the value is 'derived' by executing a
/// function (in contrast to "inputs").
-pub struct DerivedStorage<Q, MP>
+pub struct DerivedStorage<Q>
where
Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
{
group_index: u16,
- lru_list: Lru<Slot<Q, MP>>,
- slot_map: RwLock<FxIndexMap<Q::Key, Arc<Slot<Q, MP>>>>,
- policy: PhantomData<MP>,
+ slot_map: RwLock<FxIndexMap<Q::Key, Arc<Slot<Q>>>>,
}
-impl<Q, MP> std::panic::RefUnwindSafe for DerivedStorage<Q, MP>
+impl<Q> std::panic::RefUnwindSafe for DerivedStorage<Q>
where
Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
+
Q::Key: std::panic::RefUnwindSafe,
Q::Value: std::panic::RefUnwindSafe,
{
}
-pub trait MemoizationPolicy<Q>: Send + Sync
-where
- Q: QueryFunction,
-{
- fn should_memoize_value(key: &Q::Key) -> bool;
-
- fn memoized_value_eq(old_value: &Q::Value, new_value: &Q::Value) -> bool;
-}
-
-pub enum AlwaysMemoizeValue {}
-impl<Q> MemoizationPolicy<Q> for AlwaysMemoizeValue
+impl<Q> DerivedStorage<Q>
where
Q: QueryFunction,
Q::Value: Eq,
{
- fn should_memoize_value(_key: &Q::Key) -> bool {
- true
- }
-
- fn memoized_value_eq(old_value: &Q::Value, new_value: &Q::Value) -> bool {
- old_value == new_value
- }
-}
-
-pub enum NeverMemoizeValue {}
-impl<Q> MemoizationPolicy<Q> for NeverMemoizeValue
-where
- Q: QueryFunction,
-{
- fn should_memoize_value(_key: &Q::Key) -> bool {
- false
- }
-
- fn memoized_value_eq(_old_value: &Q::Value, _new_value: &Q::Value) -> bool {
- panic!("cannot reach since we never memoize")
- }
-}
-
-impl<Q, MP> DerivedStorage<Q, MP>
-where
- Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
-{
- fn slot(&self, key: &Q::Key) -> Arc<Slot<Q, MP>> {
+ fn slot(&self, key: &Q::Key) -> Arc<Slot<Q>> {
if let Some(v) = self.slot_map.read().get(key) {
return v.clone();
}
@@ -111,20 +62,15 @@ where
}
}
-impl<Q, MP> QueryStorageOps<Q> for DerivedStorage<Q, MP>
+impl<Q> QueryStorageOps<Q> for DerivedStorage<Q>
where
Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
+ Q::Value: Eq,
{
const CYCLE_STRATEGY: crate::plumbing::CycleRecoveryStrategy = Q::CYCLE_STRATEGY;
fn new(group_index: u16) -> Self {
- DerivedStorage {
- group_index,
- slot_map: RwLock::new(FxIndexMap::default()),
- lru_list: Default::default(),
- policy: PhantomData,
- }
+ DerivedStorage { group_index, slot_map: RwLock::new(FxIndexMap::default()) }
}
fn fmt_index(
@@ -161,10 +107,6 @@ where
let slot = self.slot(key);
let StampedValue { value, durability, changed_at } = slot.read(db, key);
- if let Some(evicted) = self.lru_list.record_use(&slot) {
- evicted.evict();
- }
-
db.salsa_runtime().report_query_read_and_unwind_if_cycle_resulted(
slot.database_key_index(),
durability,
@@ -175,7 +117,7 @@ where
}
fn durability(&self, db: &<Q as QueryDb<'_>>::DynDb, key: &Q::Key) -> Durability {
- self.slot(key).durability(db)
+ self.slot_map.read().get(key).map_or(Durability::LOW, |slot| slot.durability(db))
}
fn entries<C>(&self, _db: &<Q as QueryDb<'_>>::DynDb) -> C
@@ -187,31 +129,19 @@ where
}
}
-impl<Q, MP> QueryStorageMassOps for DerivedStorage<Q, MP>
+impl<Q> QueryStorageMassOps for DerivedStorage<Q>
where
Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
{
fn purge(&self) {
- self.lru_list.purge();
*self.slot_map.write() = Default::default();
}
}
-impl<Q, MP> LruQueryStorageOps for DerivedStorage<Q, MP>
-where
- Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
-{
- fn set_lru_capacity(&self, new_capacity: usize) {
- self.lru_list.set_lru_capacity(new_capacity);
- }
-}
-
-impl<Q, MP> DerivedQueryStorageOps<Q> for DerivedStorage<Q, MP>
+impl<Q> DerivedQueryStorageOps<Q> for DerivedStorage<Q>
where
Q: QueryFunction,
- MP: MemoizationPolicy<Q>,
+ Q::Value: Eq,
{
fn invalidate<S>(&self, runtime: &mut Runtime, key: &S)
where