Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Garbage Collection for Interned Values #602

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions book/src/plumbing/jars_and_ingredients.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ It combines an [`IngredientIndex`] with a `key_index`, which is a `salsa::Id`:
{{#include ../../../src/key.rs:DatabaseKeyIndex}}
```

A `DependencyIndex` is similar, but the `key_index` is optional.
This is used when we sometimes wish to refer to the ingredient as a whole, and not any specific value within the ingredient.

These kinds of indices are used to store connetions between ingredients.
For example, each memoized value has to track its inputs.
Those inputs are stored as dependency indices.
Expand Down
13 changes: 4 additions & 9 deletions src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
self.index
}

fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Option<Id>,
_revision: Revision,
) -> bool {
fn maybe_changed_after(&self, _db: &dyn Database, _input: Id, _revision: Revision) -> bool {
panic!("nothing should ever depend on an accumulator directly")
}

Expand All @@ -118,15 +113,15 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
&self,
_db: &dyn Database,
_executor: DatabaseKeyIndex,
_output_key: Option<crate::Id>,
_output_key: Id,
) {
}

fn remove_stale_output(
&self,
_db: &dyn Database,
_executor: DatabaseKeyIndex,
_stale_output_key: Option<crate::Id>,
_stale_output_key: Id,
) {
}

Expand All @@ -138,7 +133,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
panic!("unexpected reset on accumulator")
}

fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt_index(&self, index: Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(A::DEBUG_NAME, index, fmt)
}

Expand Down
12 changes: 6 additions & 6 deletions src/active_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
accumulator::accumulated_map::AccumulatedMap,
durability::Durability,
hash::FxIndexSet,
key::{DatabaseKeyIndex, DependencyIndex},
key::DatabaseKeyIndex,
tracked_struct::{Disambiguator, Identity},
zalsa_local::EMPTY_DEPENDENCIES,
Cycle, Id, Revision,
Expand All @@ -30,7 +30,7 @@ pub(crate) struct ActiveQuery {
/// * tracked structs created
/// * invocations of `specify`
/// * accumulators pushed to
input_outputs: FxIndexSet<(EdgeKind, DependencyIndex)>,
input_outputs: FxIndexSet<(EdgeKind, DatabaseKeyIndex)>,

/// True if there was an untracked read.
untracked_read: bool,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl ActiveQuery {

pub(super) fn add_read(
&mut self,
input: DependencyIndex,
input: DatabaseKeyIndex,
durability: Durability,
revision: Revision,
) {
Expand All @@ -95,12 +95,12 @@ impl ActiveQuery {
}

/// Adds a key to our list of outputs.
pub(super) fn add_output(&mut self, key: DependencyIndex) {
pub(super) fn add_output(&mut self, key: DatabaseKeyIndex) {
self.input_outputs.insert((EdgeKind::Output, key));
}

/// True if the given key was output by this query.
pub(super) fn is_output(&self, key: DependencyIndex) -> bool {
pub(super) fn is_output(&self, key: DatabaseKeyIndex) -> bool {
self.input_outputs.contains(&(EdgeKind::Output, key))
}

Expand Down Expand Up @@ -142,7 +142,7 @@ impl ActiveQuery {
/// Used during cycle recovery, see [`Runtime::unblock_cycle_and_maybe_throw`].
pub(super) fn remove_cycle_participants(&mut self, cycle: &Cycle) {
for p in cycle.participant_keys() {
let p: DependencyIndex = p.into();
let p: DatabaseKeyIndex = p;
self.input_outputs.shift_remove(&(EdgeKind::Input, p));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
/// which are the fine-grained components we use to track data. This is intended
/// for debugging and the contents of the returned string are not semver-guaranteed.
///
/// Ingredient indices can be extracted from [`DependencyIndex`](`crate::DependencyIndex`) values.
/// Ingredient indices can be extracted from [`DatabaseIndex`](`crate::DatabaseIndex`) values.
fn ingredient_debug_name(&self, ingredient_index: IngredientIndex) -> Cow<'_, str> {
Cow::Borrowed(
self.zalsa()
Expand Down
6 changes: 3 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::thread::ThreadId;

use crate::{key::DatabaseKeyIndex, key::DependencyIndex};
use crate::key::DatabaseKeyIndex;

/// The `Event` struct identifies various notable things that can
/// occur during salsa execution. Instances of this struct are given
Expand Down Expand Up @@ -64,7 +64,7 @@ pub enum EventKind {
execute_key: DatabaseKeyIndex,

/// Key for the query that is no longer output
output_key: DependencyIndex,
output_key: DatabaseKeyIndex,
},

/// Tracked structs or memoized data were discarded (freed).
Expand All @@ -79,6 +79,6 @@ pub enum EventKind {
executor_key: DatabaseKeyIndex,

/// Accumulator that was accumulated into
accumulator: DependencyIndex,
accumulator: DatabaseKeyIndex,
},
}
22 changes: 5 additions & 17 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,9 @@ where
self.index
}

fn maybe_changed_after(
&self,
db: &dyn Database,
input: Option<Id>,
revision: Revision,
) -> bool {
let key = input.unwrap();
fn maybe_changed_after(&self, db: &dyn Database, input: Id, revision: Revision) -> bool {
let db = db.as_view::<C::DbView>();
self.maybe_changed_after(db, key, revision)
self.maybe_changed_after(db, input, revision)
}

fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy {
Expand All @@ -208,21 +202,15 @@ where
self.origin(db.zalsa(), key)
}

fn mark_validated_output(
&self,
db: &dyn Database,
executor: DatabaseKeyIndex,
output_key: Option<crate::Id>,
) {
let output_key = output_key.unwrap();
fn mark_validated_output(&self, db: &dyn Database, executor: DatabaseKeyIndex, output_key: Id) {
self.validate_specified_value(db, executor, output_key);
}

fn remove_stale_output(
&self,
_db: &dyn Database,
_executor: DatabaseKeyIndex,
_stale_output_key: Option<crate::Id>,
_stale_output_key: Id,
) {
// This function is invoked when a query Q specifies the value for `stale_output_key` in rev 1,
// but not in rev 2. We don't do anything in this case, we just leave the (now stale) memo.
Expand All @@ -237,7 +225,7 @@ where
std::mem::take(&mut self.deleted_entries);
}

fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt_index(&self, index: Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(C::DEBUG_NAME, index, fmt)
}

Expand Down
10 changes: 5 additions & 5 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{memo::Memo, Configuration, IngredientImpl};
use crate::{
hash::FxHashSet, key::DependencyIndex, zalsa_local::QueryRevisions, AsDynDatabase as _,
DatabaseKeyIndex, Event, EventKind,
hash::FxHashSet, zalsa_local::QueryRevisions, AsDynDatabase as _, DatabaseKeyIndex, Event,
EventKind,
};

impl<C> IngredientImpl<C>
Expand Down Expand Up @@ -33,9 +33,9 @@ where
// Remove the outputs that are no longer present in the current revision
// to prevent that the next revision is seeded with a id mapping that no longer exists.
revisions.tracked_struct_ids.retain(|k, value| {
!old_outputs.contains(&DependencyIndex {
!old_outputs.contains(&DatabaseKeyIndex {
ingredient_index: k.ingredient_index(),
key_index: Some(*value),
key_index: *value,
})
});
}
Expand All @@ -45,7 +45,7 @@ where
}
}

fn report_stale_output(db: &C::DbView, key: DatabaseKeyIndex, output: DependencyIndex) {
fn report_stale_output(db: &C::DbView, key: DatabaseKeyIndex, output: DatabaseKeyIndex) {
let db = db.as_dyn_database();

db.salsa_event(&|| Event {
Expand Down
2 changes: 1 addition & 1 deletion src/function/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
self.evict_value_from_memo_for(zalsa, evicted);
}

zalsa_local.report_tracked_read(self.database_key_index(id).into(), durability, changed_at);
zalsa_local.report_tracked_read(self.database_key_index(id), durability, changed_at);

value
}
Expand Down
4 changes: 2 additions & 2 deletions src/function/specify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
//
// Now, if We invoke Q3 first, We get one result for Q2, but if We invoke Q4 first, We get a different value. That's no good.
let database_key_index = <C::Input<'db>>::database_key_index(db.as_dyn_database(), key);
let dependency_index = database_key_index.into();
let dependency_index = database_key_index;
if !zalsa_local.is_output_of_active_query(dependency_index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
let dependency_index = database_key_index;
if !zalsa_local.is_output_of_active_query(dependency_index) {
if !zalsa_local.is_output_of_active_query(database_key_index) {

panic!("can only use `specify` on salsa structs created during the current tracked fn");
}
Expand Down Expand Up @@ -92,7 +92,7 @@ where

// Record that the current query *specified* a value for this cell.
let database_key_index = self.database_key_index(key);
zalsa_local.add_output(database_key_index.into());
zalsa_local.add_output(database_key_index);
}

/// Invoked when the query `executor` has been validated as having green inputs
Expand Down
20 changes: 6 additions & 14 deletions src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
fn maybe_changed_after<'db>(
&'db self,
db: &'db dyn Database,
input: Option<Id>,
input: Id,
revision: Revision,
) -> bool;

Expand All @@ -60,7 +60,7 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
&'db self,
db: &'db dyn Database,
executor: DatabaseKeyIndex,
output_key: Option<Id>,
output_key: Id,
);

/// Invoked when the value `stale_output` was output by `executor` in a previous
Expand All @@ -71,7 +71,7 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
&self,
db: &dyn Database,
executor: DatabaseKeyIndex,
stale_output_key: Option<Id>,
stale_output_key: Id,
);

/// Returns the [`IngredientIndex`] of this ingredient.
Expand All @@ -98,7 +98,7 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
/// [`IngredientRequiresReset::RESET_ON_NEW_REVISION`] to true.
fn reset_for_new_revision(&mut self);

fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
fn fmt_index(&self, index: Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
}

impl dyn Ingredient {
Expand Down Expand Up @@ -138,14 +138,6 @@ impl dyn Ingredient {
}

/// A helper function to show human readable fmt.
pub(crate) fn fmt_index(
debug_name: &str,
id: Option<Id>,
fmt: &mut fmt::Formatter<'_>,
) -> fmt::Result {
if let Some(i) = id {
write!(fmt, "{debug_name}({i:?})")
} else {
write!(fmt, "{debug_name}()")
}
pub(crate) fn fmt_index(debug_name: &str, id: Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{debug_name}({id:?})")
}
19 changes: 7 additions & 12 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
cycle::CycleRecoveryStrategy,
id::{AsId, FromId},
ingredient::{fmt_index, Ingredient},
key::{DatabaseKeyIndex, DependencyIndex},
key::DatabaseKeyIndex,
plumbing::{Jar, JarAux, Stamp},
table::{memo::MemoTable, sync::SyncTable, Slot, Table},
zalsa::{IngredientIndex, Zalsa},
Expand Down Expand Up @@ -182,9 +182,9 @@ impl<C: Configuration> IngredientImpl<C> {
let value = Self::data(zalsa, id);
let stamp = &value.stamps[field_index];
zalsa_local.report_tracked_read(
DependencyIndex {
DatabaseKeyIndex {
ingredient_index: field_ingredient_index,
key_index: Some(id),
key_index: id,
},
stamp.durability,
stamp.changed_at,
Expand All @@ -207,12 +207,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
self.ingredient_index
}

fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Option<Id>,
_revision: Revision,
) -> bool {
fn maybe_changed_after(&self, _db: &dyn Database, _input: Id, _revision: Revision) -> bool {
// Input ingredients are just a counter, they store no data, they are immortal.
// Their *fields* are stored in function ingredients elsewhere.
false
Expand All @@ -230,7 +225,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
&self,
_db: &dyn Database,
executor: DatabaseKeyIndex,
output_key: Option<Id>,
output_key: Id,
) {
unreachable!(
"mark_validated_output({:?}, {:?}): input cannot be the output of a tracked function",
Expand All @@ -242,7 +237,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
&self,
_db: &dyn Database,
executor: DatabaseKeyIndex,
stale_output_key: Option<Id>,
stale_output_key: Id,
) {
unreachable!(
"remove_stale_output({:?}, {:?}): input cannot be the output of a tracked function",
Expand All @@ -258,7 +253,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
panic!("unexpected call to `reset_for_new_revision`")
}

fn fmt_index(&self, index: Option<Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt_index(&self, index: Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(C::DEBUG_NAME, index, fmt)
}

Expand Down
Loading
Loading