Skip to content

Commit

Permalink
Use named struct for disambiguation key
Browse files Browse the repository at this point in the history
  • Loading branch information
puuuuh committed Oct 15, 2024
1 parent fd8f1a1 commit 47c318f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
22 changes: 13 additions & 9 deletions src/active_query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use rustc_hash::FxHashMap;

use crate::{accumulator::accumulated_map::AccumulatedMap, durability::Durability, hash::FxIndexSet, key::{DatabaseKeyIndex, DependencyIndex}, tracked_struct::{Disambiguator, KeyStruct}, zalsa_local::EMPTY_DEPENDENCIES, Cycle, Id, IngredientIndex, Revision};

use super::zalsa_local::{EdgeKind, QueryEdges, QueryOrigin, QueryRevisions};
use crate::tracked_struct::DisambiguationKey;
use crate::{
accumulator::accumulated_map::AccumulatedMap,
durability::Durability,
hash::FxIndexSet,
key::{DatabaseKeyIndex, DependencyIndex},
tracked_struct::{Disambiguator, KeyStruct},
zalsa_local::EMPTY_DEPENDENCIES,
Cycle, Id, Revision,
};

#[derive(Debug)]
pub(crate) struct ActiveQuery {
Expand Down Expand Up @@ -37,7 +45,7 @@ pub(crate) struct ActiveQuery {
/// This table starts empty as the query begins and is gradually populated.
/// Note that if a query executes in 2 different revisions but creates the same
/// set of tracked structs, they will get the same disambiguator values.
disambiguator_map: FxHashMap<(IngredientIndex, u64), Disambiguator>,
disambiguator_map: FxHashMap<DisambiguationKey, Disambiguator>,

/// Map from tracked struct keys (which include the hash + disambiguator) to their
/// final id.
Expand Down Expand Up @@ -147,14 +155,10 @@ impl ActiveQuery {
self.input_outputs.clone_from(&cycle_query.input_outputs);
}

pub(super) fn disambiguate(
&mut self,
ingredient_index: IngredientIndex,
hash: u64,
) -> Disambiguator {
pub(super) fn disambiguate(&mut self, key: DisambiguationKey) -> Disambiguator {
let disambiguator = self
.disambiguator_map
.entry((ingredient_index, hash))
.entry(key)
.or_insert(Disambiguator(0));
let result = *disambiguator;
disambiguator.0 += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
// 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 {
ingredient_index: k.ingredient_index,
ingredient_index: k.ingredient_index(),
key_index: Some(*value),
})
});
Expand Down
41 changes: 29 additions & 12 deletions src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,34 @@ where
/// struct and later moved to the [`Memo`](`crate::function::memo::Memo`).
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
pub(crate) struct KeyStruct {
/// IngredientIndex of the tracked struct
pub(crate) ingredient_index: IngredientIndex,

/// The hash of the `#[id]` fields of this struct.
/// Note that multiple structs may share the same hash.
data_hash: u64,
/// Tracked struct key
disambiguation_key: DisambiguationKey,

/// The unique disambiguator assigned within the active query
/// to distinguish distinct tracked structs with the same hash.
/// to distinguish distinct tracked structs with the same key.
disambiguator: Disambiguator,
}

impl KeyStruct {
pub(crate) fn ingredient_index(&self) -> IngredientIndex {
self.disambiguation_key.ingredient_index
}
}

/// Stores the data that (almost) uniquely identifies a tracked struct.
/// This includes the ingredient index of that struct type plus the hash of its id fields.
/// This is mapped to a disambiguator -- a value that starts as 0 but increments each round,
/// allowing for multiple tracked structs with the same hash and ingredient_index
/// created within the query to each have a unique id.
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
pub struct DisambiguationKey {
/// Index of the tracked struct ingredient.
ingredient_index: IngredientIndex,

/// Hash of the id fields.
hash: u64,
}

// ANCHOR: ValueStruct
#[derive(Debug)]
pub struct Value<C>
Expand Down Expand Up @@ -258,15 +274,16 @@ where
) -> C::Struct<'db> {
let (zalsa, zalsa_local) = db.zalsas();

let data_hash = crate::hash::hash(&(C::id_fields(&fields)));
let disambiguation_key = DisambiguationKey {
ingredient_index: self.ingredient_index,
hash: crate::hash::hash(&C::id_fields(&fields)),
};

let (current_deps, disambiguator) =
zalsa_local.disambiguate(self.ingredient_index, data_hash);
let (current_deps, disambiguator) = zalsa_local.disambiguate(disambiguation_key);

let key_struct = KeyStruct {
ingredient_index: self.ingredient_index,
disambiguation_key,
disambiguator,
data_hash,
};

let current_revision = zalsa.current_revision();
Expand Down
10 changes: 3 additions & 7 deletions src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::runtime::StampedValue;
use crate::table::PageIndex;
use crate::table::Slot;
use crate::table::Table;
use crate::tracked_struct::Disambiguator;
use crate::tracked_struct::KeyStruct;
use crate::tracked_struct::{DisambiguationKey, Disambiguator};
use crate::zalsa::IngredientIndex;
use crate::Accumulator;
use crate::Cancelled;
Expand Down Expand Up @@ -262,19 +262,15 @@ impl ZalsaLocal {
/// * the current dependencies (durability, changed_at) of current query
/// * the disambiguator index
#[track_caller]
pub(crate) fn disambiguate(
&self,
ingredient_index: IngredientIndex,
data_hash: u64,
) -> (StampedValue<()>, Disambiguator) {
pub(crate) fn disambiguate(&self, key: DisambiguationKey) -> (StampedValue<()>, Disambiguator) {
assert!(
self.query_in_progress(),
"cannot create a tracked struct disambiguator outside of a tracked function"
);

self.with_query_stack(|stack| {
let top_query = stack.last_mut().unwrap();
let disambiguator = top_query.disambiguate(ingredient_index, data_hash);
let disambiguator = top_query.disambiguate(key);
(
StampedValue {
value: (),
Expand Down

0 comments on commit 47c318f

Please sign in to comment.