This commit is contained in:
zjp 2022-08-21 23:47:27 +08:00
parent 8dfc578edc
commit c84f88d23a
2 changed files with 34 additions and 25 deletions

View file

@ -30,15 +30,17 @@ mod store;
mod sync; mod sync;
/// Function ingredients are the "workhorse" of salsa. /// Function ingredients are the "workhorse" of salsa.
/// They are used for tracked functions, for the "value" fields of tracked structs, and for the fields of input structs. /// They are used for tracked functions, for the "value" fields of tracked structs, and for the
/// The function ingredient is fairly complex and so its code is spread across multiple modules, typically one per method. /// fields of input structs. The function ingredient is fairly complex and so its code is
/// The main entry points are: /// spread across multiple modules, typically one per method. The main entry points are:
/// ///
/// * the `fetch` method, which is invoked when the function is called by the user's code; /// * the `fetch` method, which is invoked when the function is called by the user's code; it
/// it will return a memoized value if one exists, or execute the function otherwise. /// will return a memoized value if one exists, or execute the function otherwise.
/// * the `specify` method, which can only be used when the key is an entity created by the active query. /// * the `specify` method, which can only be used when the key is an entity created by the
/// It sets the value of the function imperatively, so that when later fetches occur, they'll return this value. /// active query. It sets the value of the function imperatively, so that when later fetches
/// * the `store` method, which can only be invoked with an `&mut` reference, and is to set input fields. /// occur, they'll return this value.
/// * the `store` method, which can only be invoked with an `&mut` reference, and is to set
/// input fields.
pub struct FunctionIngredient<C: Configuration> { pub struct FunctionIngredient<C: Configuration> {
/// The ingredient index we were assigned in the database. /// The ingredient index we were assigned in the database.
/// Used to construct `DatabaseKeyIndex` values. /// Used to construct `DatabaseKeyIndex` values.
@ -95,8 +97,8 @@ pub trait Configuration {
/// Invokes after a new result `new_value`` has been computed for which an older memoized /// Invokes after a new result `new_value`` has been computed for which an older memoized
/// value existed `old_value`. Returns true if the new value is equal to the older one /// value existed `old_value`. Returns true if the new value is equal to the older one
/// and hence should be "backdated" (i.e., marked as having last changed in an older revision, /// and hence should be "backdated" (i.e., marked as having last changed in an older
/// even though it was recomputed). /// revision, even though it was recomputed).
/// ///
/// This invokes user's code in form of the `Eq` impl. /// This invokes user's code in form of the `Eq` impl.
fn should_backdate_value(old_value: &Self::Value, new_value: &Self::Value) -> bool; fn should_backdate_value(old_value: &Self::Value, new_value: &Self::Value) -> bool;
@ -183,8 +185,9 @@ where
self.register(db); self.register(db);
let memo = Arc::new(memo); let memo = Arc::new(memo);
let value = unsafe { let value = unsafe {
// Unsafety conditions: memo must be in the map (it's not yet, but it will be by the time this // Unsafety conditions: memo must be in the map (it's not yet, but it will be by the
// value is returned) and anything removed from map is added to deleted entries (ensured elsewhere). // time this value is returned) and anything removed from map is added to
// deleted entries (ensured elsewhere).
self.extend_memo_lifetime(&memo) self.extend_memo_lifetime(&memo)
}; };
if let Some(old_value) = self.memo_map.insert(key, memo) { if let Some(old_value) = self.memo_map.insert(key, memo) {
@ -241,9 +244,10 @@ where
_executor: DatabaseKeyIndex, _executor: DatabaseKeyIndex,
_stale_output_key: Option<crate::Id>, _stale_output_key: Option<crate::Id>,
) { ) {
// This function is invoked when a query Q specifies the value for `stale_output_key` in rev 1, // This function is invoked when a query Q specifies the value for `stale_output_key`
// but not in rev 2. We don't do anything in this case, we just leave the (now stale) memo. // in rev 1, but not in rev 2. We don't do anything in this case, we just leave
// Since its `verified_at` field has not changed, it will be considered dirty if it is invoked. // the (now stale) memo. Since its `verified_at` field has not changed, it will
// be considered dirty if it is invoked.
} }
fn reset_for_new_revision(&mut self) { fn reset_for_new_revision(&mut self) {

View file

@ -7,11 +7,13 @@ use super::Revision;
/// "Ingredients" are the bits of data that are stored within the database to make salsa work. /// "Ingredients" are the bits of data that are stored within the database to make salsa work.
/// Each jar will define some number of ingredients that it requires. /// Each jar will define some number of ingredients that it requires.
/// Each use salsa macro (e.g., `#[salsa::tracked]`, `#[salsa::interned]`) adds one or more ingredients to the jar struct /// Each use salsa macro (e.g., `#[salsa::tracked]`, `#[salsa::interned]`) adds one or more
/// that together are used to create the salsa concept. /// ingredients to the jar struct that together are used to create the salsa concept.
/// For example, a tracked struct defines a [`crate::interned::InternedIngredient`] to store its identity /// For example, a tracked struct defines a [`crate::interned::InternedIngredient`] to store
/// plus [`crate::function::FunctionIngredient`] values to store its fields. /// its identity plus [`crate::function::FunctionIngredient`] values to store its fields.
/// The exact ingredients are determined by [`IngredientsFor`](`crate::storage::IngredientsFor`) implementations generated by the macro. /// The exact ingredients are determined by
/// [`IngredientsFor`](`crate::storage::IngredientsFor`) implementations generated by the
/// macro.
pub trait Ingredient<DB: ?Sized> { pub trait Ingredient<DB: ?Sized> {
/// If this ingredient is a participant in a cycle, what is its cycle recovery strategy? /// If this ingredient is a participant in a cycle, what is its cycle recovery strategy?
/// (Really only relevant to [`crate::function::FunctionIngredient`], /// (Really only relevant to [`crate::function::FunctionIngredient`],
@ -25,7 +27,8 @@ pub trait Ingredient<DB: ?Sized> {
fn origin(&self, key_index: Id) -> Option<QueryOrigin>; fn origin(&self, key_index: Id) -> Option<QueryOrigin>;
/// Invoked when the value `output_key` should be marked as valid in the current revision. /// Invoked when the value `output_key` should be marked as valid in the current revision.
/// This occurs because the value for `executor`, which generated it, was marked as valid in the current revision. /// This occurs because the value for `executor`, which generated it, was marked as valid
/// in the current revision.
fn mark_validated_output(&self, db: &DB, executor: DatabaseKeyIndex, output_key: Option<Id>); fn mark_validated_output(&self, db: &DB, executor: DatabaseKeyIndex, output_key: Option<Id>);
/// Invoked when the value `stale_output` was output by `executor` in a previous /// Invoked when the value `stale_output` was output by `executor` in a previous
@ -46,11 +49,12 @@ pub trait Ingredient<DB: ?Sized> {
fn salsa_struct_deleted(&self, db: &DB, id: Id); fn salsa_struct_deleted(&self, db: &DB, id: Id);
/// Invoked when a new revision is about to start. /// Invoked when a new revision is about to start.
/// This moment is important because it means that we have an `&mut`-reference to the database, /// This moment is important because it means that we have an `&mut`-reference to the
/// and hence any pre-existing `&`-references must have expired. /// database, and hence any pre-existing `&`-references must have expired.
/// Many ingredients, given an `&'db`-reference to the database, /// Many ingredients, given an `&'db`-reference to the database,
/// use unsafe code to return `&'db`-references to internal values. /// use unsafe code to return `&'db`-references to internal values.
/// The backing memory for those values can only be freed once an `&mut`-reference to the database is created. /// The backing memory for those values can only be freed once an `&mut`-reference to the
/// database is created.
/// ///
/// **Important:** to actually receive resets, the ingredient must set /// **Important:** to actually receive resets, the ingredient must set
/// [`IngredientRequiresReset::RESET_ON_NEW_REVISION`] to true. /// [`IngredientRequiresReset::RESET_ON_NEW_REVISION`] to true.
@ -58,7 +62,8 @@ pub trait Ingredient<DB: ?Sized> {
} }
/// Defines a const indicating if an ingredient needs to be reset each round. /// Defines a const indicating if an ingredient needs to be reset each round.
/// This const probably *should* be a member of `Ingredient` trait but then `Ingredient` would not be dyn-safe. /// This const probably *should* be a member of `Ingredient` trait but then `Ingredient` would
/// not be dyn-safe.
pub trait IngredientRequiresReset { pub trait IngredientRequiresReset {
/// If this is true, then `reset_for_new_revision` will be called every new revision. /// If this is true, then `reset_for_new_revision` will be called every new revision.
const RESET_ON_NEW_REVISION: bool; const RESET_ON_NEW_REVISION: bool;