Skip to content

Commit

Permalink
Add scratch space for diff_outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 26, 2025
1 parent 5c37082 commit a58a51a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
use super::{memo::Memo, Configuration, IngredientImpl};
use crate::{
hash::FxHashSet, key::OutputDependencyIndex, zalsa::Zalsa, zalsa_local::QueryRevisions,
key::OutputDependencyIndex, plumbing::ZalsaLocal, zalsa::Zalsa, zalsa_local::QueryRevisions,
AsDynDatabase as _, Database, DatabaseKeyIndex, Event, EventKind,
};

impl<C> IngredientImpl<C>
where
C: Configuration,
{
/// Compute the old and new outputs and invoke the `clear_stale_output` callback
/// Compute the old and new outputs and invoke the `remove_stale_output` callback
/// for each output that was generated before but is not generated now.
///
/// This function takes a `&mut` reference to `revisions` to remove outputs
/// that no longer exist in this revision from [`QueryRevisions::tracked_struct_ids`].
pub(super) fn diff_outputs(
&self,
zalsa: &Zalsa,
zalsa_local: &ZalsaLocal,
db: &C::DbView,
key: DatabaseKeyIndex,
old_memo: &Memo<C::Output<'_>>,
revisions: &mut QueryRevisions,
) {
// Iterate over the outputs of the `old_memo` and put them into a hashset
let mut old_outputs: FxHashSet<_> = old_memo.revisions.origin.outputs().collect();
let old_outputs = &mut *zalsa_local.diff_outputs_scratch.borrow_mut();
old_outputs.extend(old_memo.revisions.origin.outputs());

// Iterate over the outputs of the current query
// and remove elements from `old_outputs` when we find them
Expand All @@ -38,7 +40,7 @@ where
});
}

for old_output in old_outputs {
for old_output in old_outputs.drain() {
Self::report_stale_output(zalsa, db, key, old_output);
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/function/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ where
// old value.
if let Some(old_memo) = &opt_old_memo {
self.backdate_if_appropriate(old_memo, &mut revisions, &value);
self.diff_outputs(zalsa, db, database_key_index, old_memo, &mut revisions);
self.diff_outputs(
zalsa,
db.zalsa_local(),
db,
database_key_index,
old_memo,
&mut revisions,
);
}

tracing::debug!("{database_key_index:?}: read_upgrade: result.revisions = {revisions:#?}");
Expand Down
9 changes: 8 additions & 1 deletion src/function/specify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ where
let memo_ingredient_index = self.memo_ingredient_index(zalsa, key);
if let Some(old_memo) = self.get_memo_from_table_for(zalsa, key, memo_ingredient_index) {
self.backdate_if_appropriate(&old_memo, &mut revisions, &value);
self.diff_outputs(zalsa, db, database_key_index, &old_memo, &mut revisions);
self.diff_outputs(
zalsa,
zalsa_local,
db,
database_key_index,
&old_memo,
&mut revisions,
);
}

let memo = Memo {
Expand Down
7 changes: 5 additions & 2 deletions src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::accumulator::accumulated_map::{
};
use crate::active_query::ActiveQuery;
use crate::durability::Durability;
use crate::hash::FxHashSet;
use crate::key::{DatabaseKeyIndex, InputDependencyIndex, OutputDependencyIndex};
use crate::runtime::StampedValue;
use crate::table::PageIndex;
Expand Down Expand Up @@ -36,13 +37,15 @@ pub struct ZalsaLocal {
/// Stores the most recent page for a given ingredient.
/// This is thread-local to avoid contention.
most_recent_pages: RefCell<FxHashMap<IngredientIndex, PageIndex>>,
pub(crate) diff_outputs_scratch: RefCell<FxHashSet<OutputDependencyIndex>>,
}

impl ZalsaLocal {
pub(crate) fn new() -> Self {
ZalsaLocal {
query_stack: RefCell::new(vec![]),
most_recent_pages: RefCell::new(FxHashMap::default()),
query_stack: RefCell::default(),
most_recent_pages: RefCell::default(),
diff_outputs_scratch: RefCell::default(),
}
}

Expand Down

0 comments on commit a58a51a

Please sign in to comment.