Skip to content

Commit dec8e8c

Browse files
committed
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!
1 parent 5590aeb commit dec8e8c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/map.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,12 @@ where
10961096
K: Borrow<Q>,
10971097
Q: Hash + Eq,
10981098
{
1099-
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
1100-
self.table.get(hash, equivalent_key(k))
1099+
if self.table.is_empty() {
1100+
None
1101+
} else {
1102+
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
1103+
self.table.get(hash, equivalent_key(k))
1104+
}
11011105
}
11021106

11031107
/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
@@ -1204,8 +1208,12 @@ where
12041208
K: Borrow<Q>,
12051209
Q: Hash + Eq,
12061210
{
1207-
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
1208-
self.table.get_mut(hash, equivalent_key(k))
1211+
if self.table.is_empty() {
1212+
None
1213+
} else {
1214+
let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
1215+
self.table.get_mut(hash, equivalent_key(k))
1216+
}
12091217
}
12101218

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

0 commit comments

Comments
 (0)