diff --git a/compiler/rustc_data_structures/src/sharded.rs b/compiler/rustc_data_structures/src/sharded.rs index 417c61242a539..78b5ba7f2fd86 100644 --- a/compiler/rustc_data_structures/src/sharded.rs +++ b/compiler/rustc_data_structures/src/sharded.rs @@ -47,12 +47,12 @@ impl Sharded { #[inline] pub fn get_shard_by_hash(&self, hash: u64) -> &Lock { - &self.shards[get_shard_index_by_hash(hash)].0 + if SHARDS == 1 { &self.shards[0].0 } else { &self.shards[get_shard_index_by_hash(hash)].0 } } #[inline] pub fn get_shard_by_index(&self, i: usize) -> &Lock { - &self.shards[i].0 + if SHARDS == 1 { &self.shards[0].0 } else { &self.shards[i].0 } } pub fn lock_shards(&self) -> Vec> { @@ -142,9 +142,13 @@ fn make_hash(val: &K) -> u64 { /// consistently for each `Sharded` instance. #[inline] pub fn get_shard_index_by_hash(hash: u64) -> usize { - let hash_len = mem::size_of::(); - // Ignore the top 7 bits as hashbrown uses these and get the next SHARD_BITS highest bits. - // hashbrown also uses the lowest bits, so we can't use those - let bits = (hash >> (hash_len * 8 - 7 - SHARD_BITS)) as usize; - bits % SHARDS + if SHARDS == 1 { + 0 + } else { + let hash_len = mem::size_of::(); + // Ignore the top 7 bits as hashbrown uses these and get the next SHARD_BITS highest bits. + // hashbrown also uses the lowest bits, so we can't use those + let bits = (hash >> (hash_len * 8 - 7 - SHARD_BITS)) as usize; + bits % SHARDS + } }