-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Early exit for empty HashMap (issue #38880) #48035
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
Merged
kennytm
merged 8 commits into
rust-lang:master
from
technicalguy:Early-exit-empty-hashmap-38880
Feb 15, 2018
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9bc5986
38880 don't compute hash when searching an empty HashMap
technicalguy dcdd2c4
38880 use search_mut function rather than search_hashed
technicalguy 29f7148
38880 remove redundant extra function
technicalguy fd78621
38880 fixup add missing mut
technicalguy a295ec1
38880 restore original entry(key) method
technicalguy 94c3c84
38880 hashmap check size=0, not just capacity=0
technicalguy f3330ce
38880 fix incorrect negation
technicalguy e034ddd
38880 remove unnecessary self.table.size check
technicalguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,8 +398,9 @@ pub struct HashMap<K, V, S = RandomState> { | |
} | ||
|
||
/// Search for a pre-hashed key. | ||
/// If you don't already know the hash, use search or search_mut instead | ||
#[inline] | ||
fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> InternalEntry<K, V, M> | ||
fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, is_match: F) -> InternalEntry<K, V, M> | ||
where M: Deref<Target = RawTable<K, V>>, | ||
F: FnMut(&K) -> bool | ||
{ | ||
|
@@ -410,6 +411,18 @@ fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> Inter | |
return InternalEntry::TableIsEmpty; | ||
} | ||
|
||
search_hashed_nonempty(table, hash, is_match) | ||
} | ||
|
||
/// Search for a pre-hashed key when the hash map is known to be non-empty. | ||
#[inline] | ||
fn search_hashed_nonempty<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) | ||
-> InternalEntry<K, V, M> | ||
where M: Deref<Target = RawTable<K, V>>, | ||
F: FnMut(&K) -> bool | ||
{ | ||
// Do not check the capacity as an extra branch could slow the lookup. | ||
|
||
let size = table.size(); | ||
let mut probe = Bucket::new(table, hash); | ||
let mut displacement = 0; | ||
|
@@ -543,24 +556,36 @@ impl<K, V, S> HashMap<K, V, S> | |
} | ||
|
||
/// Search for a key, yielding the index if it's found in the hashtable. | ||
/// If you already have the hash for the key lying around, use | ||
/// search_hashed. | ||
/// If you already have the hash for the key lying around, or if you need an | ||
/// InternalEntry, use search_hashed or search_hashed_nonempty. | ||
#[inline] | ||
fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> InternalEntry<K, V, &'a RawTable<K, V>> | ||
fn search<'a, Q: ?Sized>(&'a self, q: &Q) | ||
-> Option<FullBucket<K, V, &'a RawTable<K, V>>> | ||
where K: Borrow<Q>, | ||
Q: Eq + Hash | ||
{ | ||
if !self.is_empty() { | ||
return None; | ||
} | ||
|
||
let hash = self.make_hash(q); | ||
search_hashed(&self.table, hash, |k| q.eq(k.borrow())) | ||
search_hashed_nonempty(&self.table, hash, |k| q.eq(k.borrow())) | ||
.into_occupied_bucket() | ||
} | ||
|
||
#[inline] | ||
fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> InternalEntry<K, V, &'a mut RawTable<K, V>> | ||
fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) | ||
-> Option<FullBucket<K, V, &'a mut RawTable<K, V>>> | ||
where K: Borrow<Q>, | ||
Q: Eq + Hash | ||
{ | ||
if self.is_empty() { | ||
return None; | ||
} | ||
|
||
let hash = self.make_hash(q); | ||
search_hashed(&mut self.table, hash, |k| q.eq(k.borrow())) | ||
search_hashed_nonempty(&mut self.table, hash, |k| q.eq(k.borrow())) | ||
.into_occupied_bucket() | ||
} | ||
|
||
// The caller should ensure that invariants by Robin Hood Hashing hold | ||
|
@@ -1118,7 +1143,7 @@ impl<K, V, S> HashMap<K, V, S> | |
where K: Borrow<Q>, | ||
Q: Hash + Eq | ||
{ | ||
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1) | ||
self.search(k).map(|bucket| bucket.into_refs().1) | ||
} | ||
|
||
/// Returns true if the map contains a value for the specified key. | ||
|
@@ -1145,7 +1170,7 @@ impl<K, V, S> HashMap<K, V, S> | |
where K: Borrow<Q>, | ||
Q: Hash + Eq | ||
{ | ||
self.search(k).into_occupied_bucket().is_some() | ||
self.search(k).is_some() | ||
} | ||
|
||
/// Returns a mutable reference to the value corresponding to the key. | ||
|
@@ -1174,7 +1199,7 @@ impl<K, V, S> HashMap<K, V, S> | |
where K: Borrow<Q>, | ||
Q: Hash + Eq | ||
{ | ||
self.search_mut(k).into_occupied_bucket().map(|bucket| bucket.into_mut_refs().1) | ||
self.search_mut(k).map(|bucket| bucket.into_mut_refs().1) | ||
} | ||
|
||
/// Inserts a key-value pair into the map. | ||
|
@@ -1234,11 +1259,7 @@ impl<K, V, S> HashMap<K, V, S> | |
where K: Borrow<Q>, | ||
Q: Hash + Eq | ||
{ | ||
if self.table.size() == 0 { | ||
return None; | ||
} | ||
|
||
self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1) | ||
self.search_mut(k).map(|bucket| pop_internal(bucket).1) | ||
} | ||
|
||
/// Removes a key from the map, returning the stored key and value if the | ||
|
@@ -1274,7 +1295,6 @@ impl<K, V, S> HashMap<K, V, S> | |
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a remaining |
||
|
||
self.search_mut(k) | ||
.into_occupied_bucket() | ||
.map(|bucket| { | ||
let (k, v, _) = pop_internal(bucket); | ||
(k, v) | ||
|
@@ -2632,15 +2652,11 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S> | |
|
||
#[inline] | ||
fn get(&self, key: &Q) -> Option<&K> { | ||
self.search(key).into_occupied_bucket().map(|bucket| bucket.into_refs().0) | ||
self.search(key).map(|bucket| bucket.into_refs().0) | ||
} | ||
|
||
fn take(&mut self, key: &Q) -> Option<K> { | ||
if self.table.size() == 0 { | ||
return None; | ||
} | ||
|
||
self.search_mut(key).into_occupied_bucket().map(|bucket| pop_internal(bucket).0) | ||
self.search_mut(key).map(|bucket| pop_internal(bucket).0) | ||
} | ||
|
||
#[inline] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is tis
!
correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops 😊