avoid checking for downcasters in ingredient cache slow-path

This commit is contained in:
Ibraheem Ahmed 2025-06-25 18:54:57 -04:00
parent 2e818365dd
commit 93b152d9de
2 changed files with 16 additions and 2 deletions

View file

@ -151,8 +151,14 @@ macro_rules! setup_tracked_fn {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
let zalsa = db.zalsa();
$FN_CACHE.get_or_create(zalsa, || {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
zalsa.add_or_lookup_jar_by_type::<$Configuration>()
// If the ingredient has already been inserted, we know that the downcaster
// has also been registered. This is a fast-path for multi-database use cases
// that bypass the ingredient cache and will always execute this closure.
zalsa.lookup_jar_by_type::<$Configuration>()
.unwrap_or_else(|| {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
zalsa.add_or_lookup_jar_by_type::<$Configuration>()
})
})
}

View file

@ -296,6 +296,14 @@ impl Zalsa {
.expect("should have the ingredient index available")
}
/// **NOT SEMVER STABLE**
#[doc(hidden)]
#[inline]
pub fn lookup_jar_by_type<J: Jar>(&self) -> Option<IngredientIndex> {
let jar_type_id = TypeId::of::<J>();
self.jar_map.get(&jar_type_id, &self.jar_map.guard())
}
/// **NOT SEMVER STABLE**
#[doc(hidden)]
#[inline]