Skip to content

Commit

Permalink
Split over queries without accumulated values
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Nov 17, 2024
1 parent 81d34b9 commit 60fdbc8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/accumulator/accumulated_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use super::{accumulated::Accumulated, Accumulator, AnyAccumulated};
#[derive(Default, Debug)]
pub struct AccumulatedMap {
map: FxHashMap<IngredientIndex, Box<dyn AnyAccumulated>>,
/// `true` if any query called by this query has any accumulated values.
///
/// Tracking whether any dependency has any accumulated values helps
/// skipping over entire subtrees when collecting accumulated values.
dependency_with_accumulated_values: bool,
}

impl AccumulatedMap {
Expand All @@ -17,6 +22,16 @@ impl AccumulatedMap {
.accumulate(value);
}

/// Marks the accumulator result that some nested query has some accumulated values.
pub fn add_dependency_with_accumulated_values(&mut self) {
self.dependency_with_accumulated_values = true;
}

/// Returns `true` if this query itself or any of its dependencies have any accumulated values.
pub fn has_any(&self) -> bool {
self.dependency_with_accumulated_values || !self.map.is_empty()
}

pub fn extend_with_accumulated<A: Accumulator>(
&self,
index: IngredientIndex,
Expand All @@ -41,6 +56,7 @@ impl Clone for AccumulatedMap {
.iter()
.map(|(&key, value)| (key, value.cloned()))
.collect(),
dependency_with_accumulated_values: self.dependency_with_accumulated_values,
}
}
}
4 changes: 4 additions & 0 deletions src/function/accumulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ where

// Extend `output` with any values accumulated by `k`.
if let Some(accumulated_map) = k.accumulated(db) {
if !accumulated_map.has_any() {
continue;
}

accumulated_map.extend_with_accumulated(accumulator.index(), &mut output);
}

Expand Down
7 changes: 6 additions & 1 deletion src/function/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ 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).into(),
durability,
changed_at,
memo.revisions.accumulated.has_any(),
);

value
}
Expand Down
1 change: 1 addition & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl<C: Configuration> IngredientImpl<C> {
},
stamp.durability,
stamp.changed_at,
false,
);
&value.fields
}
Expand Down
1 change: 1 addition & 0 deletions src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ where
DependencyIndex::for_table(self.ingredient_index),
Durability::MAX,
self.reset_at,
false,
);

// Optimisation to only get read lock on the map if the data has already
Expand Down
1 change: 1 addition & 0 deletions src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ where
},
data.durability,
field_changed_at,
false,
);

unsafe { self.to_self_ref(&data.fields) }
Expand Down
7 changes: 7 additions & 0 deletions src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl ZalsaLocal {
input: DependencyIndex,
durability: Durability,
changed_at: Revision,
accumulated: bool,
) {
debug!(
"report_tracked_read(input={:?}, durability={:?}, changed_at={:?})",
Expand Down Expand Up @@ -203,6 +204,12 @@ impl ZalsaLocal {
if let Some(cycle) = &top_query.cycle {
cycle.clone().throw()
}

if accumulated {
top_query
.accumulated
.add_dependency_with_accumulated_values();
}
}
})
}
Expand Down

0 comments on commit 60fdbc8

Please sign in to comment.