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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
) {
}

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

Expand Down
10 changes: 4 additions & 6 deletions src/active_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::ops::Not;

use super::zalsa_local::{QueryEdges, QueryOrigin, QueryRevisions};
use crate::accumulator::accumulated_map::AtomicInputAccumulatedValues;
use crate::key::OutputDependencyIndex;
use crate::tracked_struct::{DisambiguatorMap, IdentityHash, IdentityMap};
use crate::zalsa_local::QueryEdge;
use crate::{
accumulator::accumulated_map::{AccumulatedMap, InputAccumulatedValues},
durability::Durability,
hash::FxIndexSet,
key::{DatabaseKeyIndex, InputDependencyIndex},
key::DatabaseKeyIndex,
tracked_struct::Disambiguator,
Cycle, Revision,
};
Expand Down Expand Up @@ -80,7 +79,7 @@ impl ActiveQuery {

pub(super) fn add_read(
&mut self,
input: InputDependencyIndex,
input: DatabaseKeyIndex,
durability: Durability,
revision: Revision,
accumulated: InputAccumulatedValues,
Expand All @@ -104,12 +103,12 @@ impl ActiveQuery {
}

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

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

Expand Down Expand Up @@ -149,7 +148,6 @@ 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: InputDependencyIndex = p.into();
self.input_outputs.shift_remove(&QueryEdge::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 @@ -57,7 +57,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 [`DatabaseKeyIndex`](`crate::DatabaseKeyIndex`) values.
fn ingredient_debug_name(&self, ingredient_index: IngredientIndex) -> Cow<'_, str> {
Cow::Borrowed(
self.zalsa()
Expand Down
9 changes: 3 additions & 6 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::thread::ThreadId;

use crate::{
key::DatabaseKeyIndex,
key::{InputDependencyIndex, OutputDependencyIndex},
};
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 @@ -76,7 +73,7 @@ pub enum EventKind {
execute_key: DatabaseKeyIndex,

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

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

/// Accumulator that was accumulated into
accumulator: InputDependencyIndex,
accumulator: DatabaseKeyIndex,
},
}
9 changes: 3 additions & 6 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ where
}
}

pub fn database_key_index(&self, k: Id) -> DatabaseKeyIndex {
DatabaseKeyIndex {
ingredient_index: self.index,
key_index: k,
}
pub fn database_key_index(&self, key: Id) -> DatabaseKeyIndex {
DatabaseKeyIndex::new(self.index, key)
}

pub fn set_capacity(&mut self, capacity: usize) {
Expand Down Expand Up @@ -266,7 +263,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: crate::Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(C::DEBUG_NAME, index, fmt)
}

Expand Down
6 changes: 3 additions & 3 deletions src/function/accumulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ where
continue;
}

let ingredient = zalsa.lookup_ingredient(k.ingredient_index);
let ingredient = zalsa.lookup_ingredient(k.ingredient_index());
// Extend `output` with any values accumulated by `k`.
let (accumulated_map, input) = ingredient.accumulated(db, k.key_index);
let (accumulated_map, input) = ingredient.accumulated(db, k.key_index());
if let Some(accumulated_map) = accumulated_map {
accumulated_map.extend_with_accumulated(accumulator.index(), &mut output);
}
Expand All @@ -71,7 +71,7 @@ where
// output vector, we want to push in execution order, so reverse order to
// ensure the first child that was executed will be the first child popped
// from the stack.
let Some(origin) = ingredient.origin(db, k.key_index) else {
let Some(origin) = ingredient.origin(db, k.key_index()) else {
continue;
};

Expand Down
8 changes: 4 additions & 4 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::OutputDependencyIndex, zalsa::Zalsa, zalsa_local::QueryRevisions,
AsDynDatabase as _, Database, DatabaseKeyIndex, Event, EventKind,
hash::FxHashSet, zalsa::Zalsa, zalsa_local::QueryRevisions, AsDynDatabase as _, Database,
DatabaseKeyIndex, Event, EventKind,
};

impl<C> IngredientImpl<C>
Expand Down Expand Up @@ -34,7 +34,7 @@ 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, &mut value| {
!old_outputs.contains(&OutputDependencyIndex::new(k.ingredient_index(), value))
!old_outputs.contains(&DatabaseKeyIndex::new(k.ingredient_index(), value))
});
}

Expand All @@ -47,7 +47,7 @@ where
zalsa: &Zalsa,
db: &C::DbView,
key: DatabaseKeyIndex,
output: OutputDependencyIndex,
output: DatabaseKeyIndex,
) {
db.salsa_event(&|| {
Event::new(EventKind::WillDiscardStaleOutput {
Expand Down
2 changes: 1 addition & 1 deletion src/function/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
// Query was not previously executed, or value is potentially
// stale, or value is absent. Let's execute!
let database_key_index = active_query.database_key_index;
let id = database_key_index.key_index;
let id = database_key_index.key_index();
let value = match Cycle::catch(|| C::execute(db, C::id_to_input(db, id))) {
Ok(v) => v,
Err(cycle) => {
Expand Down
2 changes: 1 addition & 1 deletion src/function/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
self.lru.record_use(id);

zalsa_local.report_tracked_read(
self.database_key_index(id).into(),
self.database_key_index(id),
durability,
changed_at,
match &memo.revisions.accumulated {
Expand Down
5 changes: 2 additions & 3 deletions src/function/specify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +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();
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 @@ -94,7 +93,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
14 changes: 3 additions & 11 deletions src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
);
}

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

impl dyn Ingredient {
Expand Down Expand Up @@ -164,16 +164,8 @@ 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:?})")
}

#[derive(Copy, Clone, Debug)]
Expand Down
11 changes: 4 additions & 7 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
id::{AsId, FromIdWithDb},
ingredient::{fmt_index, Ingredient, MaybeChangedAfter},
input::singleton::{Singleton, SingletonChoice},
key::{DatabaseKeyIndex, InputDependencyIndex},
key::DatabaseKeyIndex,
plumbing::{Jar, Stamp},
table::{memo::MemoTable, sync::SyncTable, Slot, Table},
zalsa::{IngredientIndex, Zalsa},
Expand Down Expand Up @@ -97,10 +97,7 @@ impl<C: Configuration> IngredientImpl<C> {
}

pub fn database_key_index(&self, id: C::Struct) -> DatabaseKeyIndex {
DatabaseKeyIndex {
ingredient_index: self.ingredient_index,
key_index: id.as_id(),
}
DatabaseKeyIndex::new(self.ingredient_index, id.as_id())
}

pub fn new_input(&self, db: &dyn Database, fields: C::Fields, stamps: C::Stamps) -> C::Struct {
Expand Down Expand Up @@ -178,7 +175,7 @@ impl<C: Configuration> IngredientImpl<C> {
let value = Self::data(zalsa, id);
let stamp = &value.stamps[field_index];
zalsa_local.report_tracked_read(
InputDependencyIndex::new(field_ingredient_index, id),
DatabaseKeyIndex::new(field_ingredient_index, id),
stamp.durability,
stamp.changed_at,
InputAccumulatedValues::Empty,
Expand Down Expand Up @@ -258,7 +255,7 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
);
}

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
2 changes: 1 addition & 1 deletion src/input/input_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
) {
}

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

Expand Down
Loading