From dec8e8cccbb6e2742b9f551bb9b95d4a8a9d0b63 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 24 Dec 2021 09:58:40 +1100 Subject: [PATCH] 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! --- src/map.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/map.rs b/src/map.rs index 086abbc514..d0592cb7fa 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1096,8 +1096,12 @@ where K: Borrow, Q: Hash + Eq, { - let hash = make_hash::(&self.hash_builder, k); - self.table.get(hash, equivalent_key(k)) + if self.table.is_empty() { + None + } else { + let hash = make_hash::(&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. @@ -1204,8 +1208,12 @@ where K: Borrow, Q: Hash + Eq, { - let hash = make_hash::(&self.hash_builder, k); - self.table.get_mut(hash, equivalent_key(k)) + if self.table.is_empty() { + None + } else { + let hash = make_hash::(&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.