From 2cae1ef5bb36b7eedae89179fe0effbe8f1fc0ee Mon Sep 17 00:00:00 2001 From: Shoyu Vanilla Date: Fri, 15 Nov 2024 01:40:45 +0900 Subject: [PATCH] Modify struct ingredient lookups --- .../src/setup_input_struct.rs | 3 + .../src/setup_interned_struct.rs | 3 + .../salsa-macro-rules/src/setup_tracked_fn.rs | 8 +-- .../src/setup_tracked_struct.rs | 3 + src/ingredient.rs | 2 +- src/salsa_struct.rs | 6 +- src/zalsa.rs | 67 ++++++++++--------- 7 files changed, 55 insertions(+), 37 deletions(-) diff --git a/components/salsa-macro-rules/src/setup_input_struct.rs b/components/salsa-macro-rules/src/setup_input_struct.rs index 51cd482b..b506b28b 100644 --- a/components/salsa-macro-rules/src/setup_input_struct.rs +++ b/components/salsa-macro-rules/src/setup_input_struct.rs @@ -124,6 +124,9 @@ macro_rules! setup_input_struct { } impl $zalsa::SalsaStructInDb for $Struct { + fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> { + aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default()) + } } impl $Struct { diff --git a/components/salsa-macro-rules/src/setup_interned_struct.rs b/components/salsa-macro-rules/src/setup_interned_struct.rs index bf9d98f5..e8b8af18 100644 --- a/components/salsa-macro-rules/src/setup_interned_struct.rs +++ b/components/salsa-macro-rules/src/setup_interned_struct.rs @@ -141,6 +141,9 @@ macro_rules! setup_interned_struct { } impl $zalsa::SalsaStructInDb for $Struct<'_> { + fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> { + aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default()) + } } unsafe impl $zalsa::Update for $Struct<'_> { diff --git a/components/salsa-macro-rules/src/setup_tracked_fn.rs b/components/salsa-macro-rules/src/setup_tracked_fn.rs index 1a23ebea..ce5d313d 100644 --- a/components/salsa-macro-rules/src/setup_tracked_fn.rs +++ b/components/salsa-macro-rules/src/setup_tracked_fn.rs @@ -99,6 +99,9 @@ macro_rules! setup_tracked_fn { $zalsa::IngredientCache::new(); impl $zalsa::SalsaStructInDb for $InternedData<'_> { + fn lookup_ingredient_index(_aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> { + None + } } impl $zalsa::interned::Configuration for $Configuration { @@ -203,10 +206,7 @@ macro_rules! setup_tracked_fn { if $needs_interner { first_index.successor(0) } else { - aux - .lookup_struct_ingredient_index( - core::any::TypeId::of::<$InternedData<'static>>() - ) + <$InternedData as $zalsa::SalsaStructInDb>::lookup_ingredient_index(aux) .expect( "Salsa struct is passed as an argument of a tracked function, but its ingredient hasn't been added!" ) diff --git a/components/salsa-macro-rules/src/setup_tracked_struct.rs b/components/salsa-macro-rules/src/setup_tracked_struct.rs index d0d42c6d..a783e376 100644 --- a/components/salsa-macro-rules/src/setup_tracked_struct.rs +++ b/components/salsa-macro-rules/src/setup_tracked_struct.rs @@ -152,6 +152,9 @@ macro_rules! setup_tracked_struct { } impl $zalsa::SalsaStructInDb for $Struct<'_> { + fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> { + aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default()) + } } impl $zalsa::TrackedStructInDb for $Struct<'_> { diff --git a/src/ingredient.rs b/src/ingredient.rs index 12601b16..f20c95ef 100644 --- a/src/ingredient.rs +++ b/src/ingredient.rs @@ -29,7 +29,7 @@ pub trait Jar: Any { } pub trait JarAux { - fn lookup_struct_ingredient_index(&self, type_id: TypeId) -> Option; + fn lookup_jar_by_type(&self, jar: &dyn Jar) -> Option; fn next_memo_ingredient_index( &self, diff --git a/src/salsa_struct.rs b/src/salsa_struct.rs index fcf7920a..8674dc12 100644 --- a/src/salsa_struct.rs +++ b/src/salsa_struct.rs @@ -1 +1,5 @@ -pub trait SalsaStructInDb {} +use crate::{plumbing::JarAux, IngredientIndex}; + +pub trait SalsaStructInDb { + fn lookup_ingredient_index(aux: &dyn JarAux) -> Option; +} diff --git a/src/zalsa.rs b/src/zalsa.rs index fbe14eed..0d7ccddc 100644 --- a/src/zalsa.rs +++ b/src/zalsa.rs @@ -119,10 +119,10 @@ pub struct Zalsa { nonce: Nonce, - /// Map from the [`IngredientIndex`][] of a salsa struct to a list of - /// [ingredient-indices](`IngredientIndex`)for tracked functions that have this salsa struct + /// Map from the [`IngredientIndex::as_usize`][] of a salsa struct to a list of + /// [ingredient-indices](`IngredientIndex`) for tracked functions that have this salsa struct /// as input. - memo_ingredients: RwLock>>, + memo_ingredient_indices: RwLock>>, /// Map from the type-id of an `impl Jar` to the index of its first ingredient. /// This is using a `Mutex` (versus, say, a `FxDashMap`) @@ -132,9 +132,6 @@ pub struct Zalsa { /// adding new kinds of ingredients. jar_map: Mutex>, - /// Map from the type-id of a salsa struct to the index of its first ingredient. - salsa_struct_map: Mutex>, - /// Vector of ingredients. /// /// Immutable unless the mutex on `ingredients_map` is held. @@ -154,11 +151,10 @@ impl Zalsa { views_of: Views::new::(), nonce: NONCE.nonce(), jar_map: Default::default(), - salsa_struct_map: Default::default(), ingredients_vec: AppendOnlyVec::new(), ingredients_requiring_reset: AppendOnlyVec::new(), runtime: Runtime::default(), - memo_ingredients: Default::default(), + memo_ingredient_indices: Default::default(), } } @@ -192,11 +188,14 @@ impl Zalsa { { let jar_type_id = jar.type_id(); let mut jar_map = self.jar_map.lock(); - *jar_map - .entry(jar_type_id) - .or_insert_with(|| { - let index = IngredientIndex::from(self.ingredients_vec.len()); - let ingredients = jar.create_ingredients(self, index); + let mut should_create = false; + let index = *jar_map.entry(jar_type_id).or_insert_with(|| { + should_create = true; + IngredientIndex::from(self.ingredients_vec.len()) + }); + if should_create { + let aux = JarAuxImpl(self, &jar_map); + let ingredients = jar.create_ingredients(&aux, index); for ingredient in ingredients { let expected_index = ingredient.ingredient_index(); @@ -204,9 +203,7 @@ impl Zalsa { self.ingredients_requiring_reset.push(expected_index); } - let actual_index = self - .ingredients_vec - .push(ingredient); + let actual_index = self.ingredients_vec.push(ingredient); assert_eq!( expected_index.as_usize(), actual_index, @@ -215,13 +212,10 @@ impl Zalsa { expected_index, actual_index, ); + } + } - } - if let Some(type_id) = jar.salsa_struct_type_id() { - self.salsa_struct_map.lock().insert(type_id, index); - } - index - }) + index } } @@ -302,16 +296,16 @@ impl Zalsa { struct_ingredient_index: IngredientIndex, memo_ingredient_index: MemoIngredientIndex, ) -> IngredientIndex { - self.memo_ingredients.read()[&struct_ingredient_index][memo_ingredient_index.as_usize()] + self.memo_ingredient_indices.read()[struct_ingredient_index.as_usize()] + [memo_ingredient_index.as_usize()] } } -impl JarAux for Zalsa { - fn lookup_struct_ingredient_index(&self, type_id: TypeId) -> Option { - self.salsa_struct_map - .lock() - .get(&type_id) - .map(ToOwned::to_owned) +struct JarAuxImpl<'a>(&'a Zalsa, &'a FxHashMap); + +impl<'a> JarAux for JarAuxImpl<'a> { + fn lookup_jar_by_type(&self, jar: &dyn Jar) -> Option { + self.1.get(&jar.type_id()).map(ToOwned::to_owned) } fn next_memo_ingredient_index( @@ -319,8 +313,19 @@ impl JarAux for Zalsa { struct_ingredient_index: IngredientIndex, ingredient_index: IngredientIndex, ) -> MemoIngredientIndex { - let mut memo_ingredients = self.memo_ingredients.write(); - let memo_ingredients = memo_ingredients.entry(struct_ingredient_index).or_default(); + let mut memo_ingredients = self.0.memo_ingredient_indices.write(); + let memo_ingredients = if let Some(memo_ingredients) = + memo_ingredients.get_mut(struct_ingredient_index.as_usize()) + { + memo_ingredients + } else { + while memo_ingredients.len() <= struct_ingredient_index.as_usize() { + memo_ingredients.push(Vec::new()); + } + memo_ingredients + .get_mut(struct_ingredient_index.as_usize()) + .unwrap() + }; let mi = MemoIngredientIndex(u32::try_from(memo_ingredients.len()).unwrap()); memo_ingredients.push(ingredient_index); mi