Skip to content

Commit

Permalink
Don't hash the key when searching in an empty table.
Browse files Browse the repository at this point in the history
In rustc, approximately one third of all non-modifying lookups are on an
empty table!
  • Loading branch information
nnethercote committed Dec 24, 2021
1 parent 5590aeb commit dec8e8c
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 dec8e8c

Please sign in to comment.