Skip to content

Commit

Permalink
Auto merge of #305 - nnethercote:add-is_empty-checks, r=Amanieu
Browse files Browse the repository at this point in the history
Don't hash the key when searching in an empty table.

In rustc, approximately one third of all non-modifying lookups are on an
empty table!
  • Loading branch information
bors committed Jan 6, 2022
2 parents 5590aeb + dec8e8c commit 61d70a1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,12 @@ where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
self.table.get(hash, equivalent_key(k))
if self.table.is_empty() {
None
} else {
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
self.table.get(hash, equivalent_key(k))
}
}

/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
Expand Down Expand Up @@ -1204,8 +1208,12 @@ where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
self.table.get_mut(hash, equivalent_key(k))
if self.table.is_empty() {
None
} else {
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
self.table.get_mut(hash, equivalent_key(k))
}
}

/// Attempts to get mutable references to `N` values in the map at once.
Expand Down

0 comments on commit 61d70a1

Please sign in to comment.