-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Early exit for empty HashMap (issue #38880) #48035
Early exit for empty HashMap (issue #38880) #48035
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
fc823fd
to
6a9dbd2
Compare
6a9dbd2
to
dcdd2c4
Compare
Thanks! @bluss would you be ok reviewing this? |
src/libstd/collections/hash/map.rs
Outdated
@@ -550,17 +572,25 @@ impl<K, V, S> HashMap<K, V, S> | |||
where K: Borrow<Q>, | |||
Q: Eq + Hash | |||
{ | |||
let hash = self.make_hash(q); | |||
search_hashed(&self.table, hash, |k| q.eq(k.borrow())) | |||
if self.table.capacity() != 0 { |
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.
Shouldn't this be checking for size() for the optimization to apply for any empty map? My understanding is that checking for 0 capacity only works for not yet allocated maps.
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.
This seems to be the explanation for that: #38880 (comment)
As it is written, it's a rearrangement of code that should result in no extra branches.
If I understand correctly, self.table.capacity() != 0
is a precondition. We can simplify this by using .size()
instead of it (with a comment), and still have equivalent code — but not in the path that we use to find a VacantEntry in an empty map. It would be good to use the .size() != 0
check where possible. Here's it's also important to keep the cross-method logical dependencies clear.
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.
Shouldn't this be checking for size() for the optimization to apply for any empty map?
This is what I wanted to do, but as @bluss points out, the problem is that when the map is empty but allocated (as opposed to empty and unallocated) and it doesn't find a key, it is supposed to return InternalEntry::Vacant
. The crucial problem here is that InternalEntry::Vacant
requires the hash. This means that you can't get away from computing the hash when the HashMap
is allocated, because otherwise you can't return an InternalEntry::Vacant
.
This is also a problem for linear search as suggested in #38880. (I did not fully realise this problem until after writing this pull request.) Since an InternalEntry::Vacant
requires the hash value, a linear search cannot avoid computing the hash value, so it is not possible to achieve a speedup by avoiding the computation of a hash value. I think this only way to fix this is to change the semantics of InternalEntry::Vacant
. (Which would require a not-insignificant reworking, but would allow for the size() == 0
that @arthurprs is considering.)
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.
N.B. I did actually try the size() == 0
aka is_empty()
check first, before I realised about the InternalEntry::Vacant
semantics: caaabe
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.
Makes sense. HashMaps are hard 😄
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.
Regarding the linear search, for that to yield a net win the backing table would need to support real fast iteration (packed KVs), otherwise iterating the buckets gets more expensive than the hashing. Potentially of interest for https://github.com/bluss/ordermap
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.
I did some benchmarks for a linear search, and I think it might still be advantageous for some cases even if the map is not densely packed. Here's my reasoning – tell me what you think:
Let m be the allocated size of the hashmap and let n be the number of items it contains. Let S be the cost of accessing the next bucket in the hashmap (regardless of if said bucket is empty), let E be the cost of calculating equality for two elements of a given type T, and let H be the cost of calculating the hash for an element of a given type T. Assume that E and H are constant across all elements of type T (this is not true for all types, e.g. strings).
If mS + nE < H then it is faster to perform a linear search lookup for some key than to compute the hash and perform a direct lookup. The average case of a linear search for a key that exists in the map is half of the worst case.
Now if you have a simple type (e.g. integers) then equality is very cheap and constant time, and computing the hash is (surprisingly) expensive (perhaps because of the DoS resilience?), so a linear search is faster for integer-keyed hashmaps with size <= ~ 20 items.
It gets more complicated for other types, such as strings, because the equality operation doesn't take constant time and/or the hash operation doesn't take constant time. (Although for strings I think it is still the case that the hash operation always takes longer than the equality operation.)
Of course all of this is moot if InternalEntry::Vacant
requires returning the hash value.
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.
Maybe, it depends on a lot of variables, specially an expensive hasher.
You can try something like this, but I'm afraid calculating VacantEntryState on misses is too expensive (edit: or not, as you don't need to compare keys anymore).
if self.table.capacity() < CC {
if table.capacity() == 0:
return InternalEntry::TableIsEmpty;
} else {
// search_linear returns Option<InternalEntry::Occupied>
search_linear(&self.table, |k| q.eq(k.borrow())).unwrap_or_else(|| {
self.make_hash(q);
InternalEntry::Vacant { hash, VacantEntryState::...{...} }
})
} else {
let hash = self.make_hash(q);
search_hashed_nonempty(&mut self.table, hash, |k| q.eq(k.borrow()))
}
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.
So then it would be faster when searching for keys that exist, but slower when searching for keys that don't exist, right? I don't think that optimisation is worth it if it slows down the case when a key is not found.
Nice work. Especially that your rearrangement shows that this is a win we don't really have to pay anything for. My suggestions are in the previous comment. |
src/libstd/collections/hash/map.rs
Outdated
|
||
/// The body of the search_hashed[_nonempty] functions | ||
#[inline] | ||
fn search_hashed_body<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) |
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.
Can we make this search_hashed_nonempty
? Avoiding the extra function.
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.
Yes, you're right
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.
LGTM
I think there is a refactoring we can do to get the full
rust/src/libstd/collections/hash/map.rs Lines 2658 to 2664 in fd78621
|
I think I see what you mean – I'll take a look. |
Edit: I'm being silly. Please ignore the last commit. The check can stay inside Edit 2: Deleted that commit now, gonna rework it |
9158ce2
to
fd78621
Compare
@bluss good observation. The Furthermore, this means that there is hope for a linear search optimisation, since we don't actually need to return an |
fd72616
to
94c3c84
Compare
src/libstd/collections/hash/map.rs
Outdated
search_hashed_nonempty(&self.table, hash, |k| q.eq(k.borrow())) | ||
} else { | ||
InternalEntry::TableIsEmpty | ||
if !self.is_empty() { |
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 😊
src/libstd/collections/hash/map.rs
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
There's a remaining self.table.size() == 0
here that is now redundant
I think there are a few more things that can be streamlined if one goes over all uses of search_mut, search_hashed etc. For example |
@bors delegate=arthurprs |
✌️ @arthurprs can now approve this pull request |
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.
LGTM
@bors r+ |
📌 Commit e034ddd has been approved by |
Thanks @bluss @technicalguy |
…ap-38880, r=arthurprs Early exit for empty HashMap (issue rust-lang#38880) Addresses issue rust-lang#38880 by checking if the HashMap is empty before computing the value of the hash. Before (integer keys) ``` running 4 tests test empty_once ... bench: 13 ns/iter (+/- 0) test empty_100 ... bench: 1,367 ns/iter (+/- 35) test exist_once ... bench: 14 ns/iter (+/- 0) test exist_100 ... bench: 1,518 ns/iter (+/- 40) ``` After ``` running 4 tests test empty_once ... bench: 2 ns/iter (+/- 0) test empty_100 ... bench: 221 ns/iter (+/- 0) test exist_once ... bench: 15 ns/iter (+/- 0) test exist_100 ... bench: 1,515 ns/iter (+/- 92) ``` When the HashMap is not empty, the performance remains the same, and when it is empty the performance is significantly improved.
Addresses issue #38880 by checking if the HashMap is empty before computing the value of the hash.
Before (integer keys)
After
When the HashMap is not empty, the performance remains the same, and when it is empty the performance is significantly improved.