Skip to content

Commit 0f93eba

Browse files
committed
[libcxx] Avoid hash key in __hash_table::find() if no buckets yet.
If the hash table has no buckets yet, it's empty and the find will do fast return end(). Then compute hash key is useless and can be avoided, since it could be expensive for some key types, such as long string. This is a small optimization but useful in cases like a checklist ( implemented as unordered_set) that is mostly empty.
1 parent 9fdb063 commit 0f93eba

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

libcxx/include/__hash_table

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,9 +1771,9 @@ template <class _Tp, class _Hash, class _Equal, class _Alloc>
17711771
template <class _Key>
17721772
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
17731773
__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) {
1774-
size_t __hash = hash_function()(__k);
17751774
size_type __bc = bucket_count();
17761775
if (__bc != 0) {
1776+
size_t __hash = hash_function()(__k);
17771777
size_t __chash = std::__constrain_hash(__hash, __bc);
17781778
__next_pointer __nd = __bucket_list_[__chash];
17791779
if (__nd != nullptr) {
@@ -1792,9 +1792,9 @@ template <class _Tp, class _Hash, class _Equal, class _Alloc>
17921792
template <class _Key>
17931793
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
17941794
__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const {
1795-
size_t __hash = hash_function()(__k);
17961795
size_type __bc = bucket_count();
17971796
if (__bc != 0) {
1797+
size_t __hash = hash_function()(__k);
17981798
size_t __chash = std::__constrain_hash(__hash, __bc);
17991799
__next_pointer __nd = __bucket_list_[__chash];
18001800
if (__nd != nullptr) {

0 commit comments

Comments
 (0)