Skip to content

Commit

Permalink
[libc++] Check hash before calling __hash_table key_eq function
Browse files Browse the repository at this point in the history
Summary: The current implementations of __hash_table::find used by std::unordered_set/unordered_map call key_eq on each key that lands in the same bucket as the key you're looking for. However, since equal objects mush hash to the same value, you can short-circuit the possibly expensive call to key_eq by checking the hashes first.

Reviewers: EricWF

Subscribers: kmensah, cfe-commits

Differential Revision: http://reviews.llvm.org/D21510

llvm-svn: 274857
  • Loading branch information
kwasimensah committed Jul 8, 2016
1 parent 3202f06 commit 318d35a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libcxx/include/__hash_table
Expand Up @@ -2205,7 +2205,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
|| __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
if (key_eq()(__nd->__value_, __k))
if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
#if _LIBCPP_DEBUG_LEVEL >= 2
return iterator(__nd, this);
#else
Expand Down Expand Up @@ -2235,7 +2235,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
|| __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
if (key_eq()(__nd->__value_, __k))
if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
#if _LIBCPP_DEBUG_LEVEL >= 2
return const_iterator(__nd, this);
#else
Expand Down

0 comments on commit 318d35a

Please sign in to comment.