Skip to content
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

Don't hash the key when searching in an empty table. #305

Merged
merged 1 commit into from
Jan 6, 2022
Merged
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
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))
}
Comment on lines +1099 to +1104
Copy link

Choose a reason for hiding this comment

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

it may make sense, if the map contains a single element and the table is small, to skip hashing and compare directly the key with the key of the single entry in the table

}

/// 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