From 76c314fd0bc1a0c6c6f37d402f6f4c8e9d87bc8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 15 Aug 2026 00:39:15 +0200 Subject: [PATCH] Make ShardedHashMap::with_capacity split capacity between shards --- compiler/rustc_data_structures/src/sharded.rs | 3 ++- compiler/rustc_middle/src/dep_graph/graph.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_data_structures/src/sharded.rs b/compiler/rustc_data_structures/src/sharded.rs index adb4516e7a54d..022d837415833 100644 --- a/compiler/rustc_data_structures/src/sharded.rs +++ b/compiler/rustc_data_structures/src/sharded.rs @@ -144,7 +144,8 @@ pub type ShardedHashMap = Sharded>; impl ShardedHashMap { pub fn with_capacity(cap: usize) -> Self { - Self::new(|| HashTable::with_capacity(cap)) + let per_shard_cap = cap.div_ceil(shards()); + Self::new(|| HashTable::with_capacity(per_shard_cap)) } pub fn len(&self) -> usize { self.lock_shards().map(|shard| shard.len()).sum() diff --git a/compiler/rustc_middle/src/dep_graph/graph.rs b/compiler/rustc_middle/src/dep_graph/graph.rs index cba7c14533484..08bb5efc685ee 100644 --- a/compiler/rustc_middle/src/dep_graph/graph.rs +++ b/compiler/rustc_middle/src/dep_graph/graph.rs @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicU32, Ordering}; use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint}; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::profiling::QueryInvocationId; -use rustc_data_structures::sharded::{self, ShardedHashMap}; +use rustc_data_structures::sharded::ShardedHashMap; use rustc_data_structures::stable_hash::{StableHash, StableHasher}; use rustc_data_structures::sync::{AtomicU64, Lock, WorkerLocal}; use rustc_data_structures::unord::UnordMap; @@ -1231,7 +1231,7 @@ impl CurrentDepGraph { encoder: GraphEncoder::new(session, encoder, prev_index_space_len, previous), anon_node_to_index: ShardedHashMap::with_capacity( // FIXME: The count estimate is off as anon nodes are only a portion of the nodes. - new_node_count_estimate / sharded::shards(), + new_node_count_estimate, ), anon_id_seed, #[cfg(debug_assertions)]