Skip to content

Use if SHARDS == 1 in a couple more places. #94111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions compiler/rustc_data_structures/src/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl<T> Sharded<T> {

#[inline]
pub fn get_shard_by_hash(&self, hash: u64) -> &Lock<T> {
&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<T> {
&self.shards[i].0
if SHARDS == 1 { &self.shards[0].0 } else { &self.shards[i].0 }
}

pub fn lock_shards(&self) -> Vec<LockGuard<'_, T>> {
Expand Down Expand Up @@ -142,9 +142,13 @@ fn make_hash<K: Hash + ?Sized>(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::<usize>();
// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://godbolt.org/z/cq9nMvz1q This change is maybe redudant, since the optimizer will see that bits % 1 has to equal zero?

0
} else {
let hash_len = mem::size_of::<usize>();
// 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
}
}