From 5c666d372dd7bc61a0cd9131ef2dd610e5cc6cbd Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sun, 28 Jun 2026 15:44:39 +0000 Subject: [PATCH 1/2] refactor: reduce interned constructor monomorphization --- src/interned.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/interned.rs b/src/interned.rs index 5c8a08997..e18dae20b 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -329,26 +329,42 @@ where C: Configuration, { pub fn new(ingredient_index: IngredientIndex) -> Self { - static SHARDS: OnceLock = OnceLock::new(); - let shards = *SHARDS.get_or_init(|| { - let num_cpus = std::thread::available_parallelism() - .map(usize::from) - .unwrap_or(1); - - (num_cpus * 4).next_power_of_two() - }); + let shards = new_shards(); + let shift = usize::BITS - shards.len().trailing_zeros(); Self { ingredient_index, hasher: FxBuildHasher, memo_table_types: Arc::new(MemoTableTypes::default()), revision_queue: RevisionQueue::new(C::REVISIONS), - shift: usize::BITS - shards.trailing_zeros(), - shards: (0..shards).map(|_| Default::default()).collect(), + shift, + shards, _marker: PhantomData, } } +} + +/// Creates the sharded storage outside of the generic [`IngredientImpl::new`] context. +/// +/// Keeping this helper non-generic avoids monomorphizing the `OnceLock` and iterator machinery for +/// every interned struct configuration. +fn new_shards() -> Box<[CachePadded>]> { + static SHARDS: OnceLock = OnceLock::new(); + let shards = *SHARDS.get_or_init(|| { + let num_cpus = std::thread::available_parallelism() + .map(usize::from) + .unwrap_or(1); + + (num_cpus * 4).next_power_of_two() + }); + + (0..shards).map(|_| Default::default()).collect() +} +impl IngredientImpl +where + C: Configuration, +{ /// Returns the shard for a given hash. /// /// Note that this value is guaranteed to be in-bounds for `self.shards`. From a134f7030265d885cd8258e9d6804f0464a933af Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 9 Jul 2026 08:29:35 +0000 Subject: [PATCH 2/2] refactor: keep interned ingredient implementation together --- src/interned.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/interned.rs b/src/interned.rs index e18dae20b..89f00e069 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -342,29 +342,6 @@ where _marker: PhantomData, } } -} - -/// Creates the sharded storage outside of the generic [`IngredientImpl::new`] context. -/// -/// Keeping this helper non-generic avoids monomorphizing the `OnceLock` and iterator machinery for -/// every interned struct configuration. -fn new_shards() -> Box<[CachePadded>]> { - static SHARDS: OnceLock = OnceLock::new(); - let shards = *SHARDS.get_or_init(|| { - let num_cpus = std::thread::available_parallelism() - .map(usize::from) - .unwrap_or(1); - - (num_cpus * 4).next_power_of_two() - }); - - (0..shards).map(|_| Default::default()).collect() -} - -impl IngredientImpl -where - C: Configuration, -{ /// Returns the shard for a given hash. /// /// Note that this value is guaranteed to be in-bounds for `self.shards`. @@ -1064,6 +1041,23 @@ where } } +/// Creates the sharded storage outside of the generic [`IngredientImpl::new`] context. +/// +/// Keeping this helper non-generic avoids monomorphizing the `OnceLock` and iterator machinery for +/// every interned struct configuration. +fn new_shards() -> Box<[CachePadded>]> { + static SHARDS: OnceLock = OnceLock::new(); + let shards = *SHARDS.get_or_init(|| { + let num_cpus = std::thread::available_parallelism() + .map(usize::from) + .unwrap_or(1); + + (num_cpus * 4).next_power_of_two() + }); + + (0..shards).map(|_| Default::default()).collect() +} + /// Inserts an ID while keeping the hasher and rehashing logic independent of `C`. fn insert_unique_erased( shard: &mut IngredientShard,