[libcxx-commits] [PATCH] D140779: Use hash value checks optimizations consistently

Dmitry Ilvokhin via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 28 11:48:49 PDT 2023


ilvokhin updated this revision to Diff 535481.
ilvokhin added a comment.

Rabase + format (as file was reformatted).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140779/new/

https://reviews.llvm.org/D140779

Files:
  libcxx/benchmarks/GenerateInput.h
  libcxx/benchmarks/unordered_set_operations.bench.cpp
  libcxx/include/__hash_table


Index: libcxx/include/__hash_table
===================================================================
--- libcxx/include/__hash_table
+++ libcxx/include/__hash_table
@@ -1808,10 +1808,12 @@
         if (__ndptr != nullptr)
         {
             for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
-                                             std::__constrain_hash(__ndptr->__hash(), __bc) == __chash;
+                    (__ndptr->__hash() == __hash ||
+                     std::__constrain_hash(__ndptr->__hash(), __bc) == __chash);
                                                      __ndptr = __ndptr->__next_)
             {
-                if (key_eq()(__ndptr->__upcast()->__value_, __value))
+                if ((__ndptr->__hash() == __hash) &&
+                    key_eq()(__ndptr->__upcast()->__value_, __value))
                     return __ndptr;
             }
         }
@@ -2023,7 +2025,8 @@
                 (__nd->__hash() == __hash || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
                                                            __nd = __nd->__next_)
             {
-                if (key_eq()(__nd->__upcast()->__value_, __k))
+                if ((__nd->__hash() == __hash) &&
+                    key_eq()(__nd->__upcast()->__value_, __k))
                     goto __done;
             }
         }
Index: libcxx/benchmarks/unordered_set_operations.bench.cpp
===================================================================
--- libcxx/benchmarks/unordered_set_operations.bench.cpp
+++ libcxx/benchmarks/unordered_set_operations.bench.cpp
@@ -178,6 +178,17 @@
 BENCHMARK_CAPTURE(BM_InsertValueRehash, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
     ->Arg(TestNumInputs);
 
+// Prefixed String //
+BENCHMARK_CAPTURE(
+    BM_InsertValue, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
+    ->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(BM_InsertValueRehash,
+                  unordered_set_prefixed_string,
+                  std::unordered_set<std::string>{},
+                  getPrefixedRandomStringInputs)
+    ->Arg(TestNumInputs);
+
 //----------------------------------------------------------------------------//
 //                         BM_Find
 // ---------------------------------------------------------------------------//
@@ -259,6 +270,15 @@
 BENCHMARK_CAPTURE(BM_FindRehash, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
     ->Arg(TestNumInputs);
 
+// Prefixed String //
+BENCHMARK_CAPTURE(
+    BM_Find, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
+    ->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(
+    BM_FindRehash, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
+    ->Arg(TestNumInputs);
+
 //----------------------------------------------------------------------------//
 //                         BM_Rehash
 // ---------------------------------------------------------------------------//
Index: libcxx/benchmarks/GenerateInput.h
===================================================================
--- libcxx/benchmarks/GenerateInput.h
+++ libcxx/benchmarks/GenerateInput.h
@@ -111,6 +111,16 @@
   return inputs;
 }
 
+inline std::vector<std::string> getPrefixedRandomStringInputs(size_t N) {
+  std::vector<std::string> inputs;
+  constexpr int kSuffixLength = 32;
+  const std::string prefix    = getRandomString(1024 - kSuffixLength);
+  for (size_t i = 0; i < N; ++i) {
+    inputs.push_back(prefix + getRandomString(kSuffixLength));
+  }
+  return inputs;
+}
+
 inline std::vector<std::string> getSortedStringInputs(size_t N) {
   std::vector<std::string> inputs = getRandomStringInputs(N);
   std::sort(inputs.begin(), inputs.end());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140779.535481.patch
Type: text/x-patch
Size: 3853 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230628/e4d4f8e7/attachment.bin>


More information about the libcxx-commits mailing list