[libcxx] r274857 - [libc++] Check hash before calling __hash_table key_eq function

Kwasi Mensah via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 8 08:34:28 PDT 2016


Author: kmensah
Date: Fri Jul  8 10:34:28 2016
New Revision: 274857

URL: http://llvm.org/viewvc/llvm-project?rev=274857&view=rev
Log:
[libc++] Check hash before calling __hash_table key_eq function

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

Modified:
    libcxx/trunk/include/__hash_table

Modified: libcxx/trunk/include/__hash_table
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__hash_table?rev=274857&r1=274856&r2=274857&view=diff
==============================================================================
--- libcxx/trunk/include/__hash_table (original)
+++ libcxx/trunk/include/__hash_table Fri Jul  8 10:34:28 2016
@@ -2205,7 +2205,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>
                     || __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
@@ -2235,7 +2235,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>
                     || __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




More information about the cfe-commits mailing list