[libcxx] r276549 - Skip chash computation in insert/emplace if the unconstrained hash matches.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 23 23:22:25 PDT 2016


Author: ericwf
Date: Sun Jul 24 01:22:25 2016
New Revision: 276549

URL: http://llvm.org/viewvc/llvm-project?rev=276549&view=rev
Log:
Skip chash computation in insert/emplace if the unconstrained hash matches.


Modified:
    libcxx/trunk/benchmarks/ContainerBenchmarks.hpp
    libcxx/trunk/benchmarks/GenerateInput.hpp
    libcxx/trunk/benchmarks/unordered_set_operations.bench.cpp
    libcxx/trunk/include/__hash_table

Modified: libcxx/trunk/benchmarks/ContainerBenchmarks.hpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/benchmarks/ContainerBenchmarks.hpp?rev=276549&r1=276548&r2=276549&view=diff
==============================================================================
--- libcxx/trunk/benchmarks/ContainerBenchmarks.hpp (original)
+++ libcxx/trunk/benchmarks/ContainerBenchmarks.hpp Sun Jul 24 01:22:25 2016
@@ -34,6 +34,38 @@ void BM_InsertValueRehash(benchmark::Sta
     }
 }
 
+
+template <class Container, class GenInputs>
+void BM_InsertDuplicate(benchmark::State& st, Container c, GenInputs gen) {
+    auto in = gen(st.range_x());
+    const auto end = in.end();
+    c.insert(in.begin(), in.end());
+    benchmark::DoNotOptimize(&c);
+    benchmark::DoNotOptimize(&in);
+    while (st.KeepRunning()) {
+        for (auto it = in.begin(); it != end; ++it) {
+            benchmark::DoNotOptimize(&(*c.insert(*it).first));
+        }
+        benchmark::ClobberMemory();
+    }
+}
+
+
+template <class Container, class GenInputs>
+void BM_EmplaceDuplicate(benchmark::State& st, Container c, GenInputs gen) {
+    auto in = gen(st.range_x());
+    const auto end = in.end();
+    c.insert(in.begin(), in.end());
+    benchmark::DoNotOptimize(&c);
+    benchmark::DoNotOptimize(&in);
+    while (st.KeepRunning()) {
+        for (auto it = in.begin(); it != end; ++it) {
+            benchmark::DoNotOptimize(&(*c.emplace(*it).first));
+        }
+        benchmark::ClobberMemory();
+    }
+}
+
 template <class Container, class GenInputs>
 static void BM_Find(benchmark::State& st, Container c, GenInputs gen) {
     auto in = gen(st.range_x());

Modified: libcxx/trunk/benchmarks/GenerateInput.hpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/benchmarks/GenerateInput.hpp?rev=276549&r1=276548&r2=276549&view=diff
==============================================================================
--- libcxx/trunk/benchmarks/GenerateInput.hpp (original)
+++ libcxx/trunk/benchmarks/GenerateInput.hpp Sun Jul 24 01:22:25 2016
@@ -130,4 +130,11 @@ inline std::vector<std::string> getRever
     return inputs;
 }
 
+inline std::vector<const char*> getRandomCStringInputs(size_t N) {
+    static std::vector<std::string> inputs = getRandomStringInputs(N);
+    std::vector<const char*> cinputs;
+    for (auto const& str : inputs)
+        cinputs.push_back(str.c_str());
+    return cinputs;
+}
 #endif // BENCHMARK_GENERATE_INPUT_HPP

Modified: libcxx/trunk/benchmarks/unordered_set_operations.bench.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/benchmarks/unordered_set_operations.bench.cpp?rev=276549&r1=276548&r2=276549&view=diff
==============================================================================
--- libcxx/trunk/benchmarks/unordered_set_operations.bench.cpp (original)
+++ libcxx/trunk/benchmarks/unordered_set_operations.bench.cpp Sun Jul 24 01:22:25 2016
@@ -265,4 +265,42 @@ BENCHMARK_CAPTURE(BM_FindRehash,
     std::unordered_set<std::string>{},
     getRandomStringInputs)->Arg(TestNumInputs);
 
+///////////////////////////////////////////////////////////////////////////////
+BENCHMARK_CAPTURE(BM_InsertDuplicate,
+    unordered_set_int,
+    std::unordered_set<int>{},
+    getRandomIntegerInputs<int>)->Arg(TestNumInputs);
+BENCHMARK_CAPTURE(BM_InsertDuplicate,
+    unordered_set_string,
+    std::unordered_set<std::string>{},
+    getRandomStringInputs)->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(BM_EmplaceDuplicate,
+    unordered_set_int,
+    std::unordered_set<int>{},
+    getRandomIntegerInputs<int>)->Arg(TestNumInputs);
+BENCHMARK_CAPTURE(BM_EmplaceDuplicate,
+    unordered_set_string,
+    std::unordered_set<std::string>{},
+    getRandomStringInputs)->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(BM_InsertDuplicate,
+    unordered_set_int_insert_arg,
+    std::unordered_set<int>{},
+    getRandomIntegerInputs<int>)->Arg(TestNumInputs);
+BENCHMARK_CAPTURE(BM_InsertDuplicate,
+    unordered_set_string_insert_arg,
+    std::unordered_set<std::string>{},
+    getRandomStringInputs)->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(BM_EmplaceDuplicate,
+    unordered_set_int_insert_arg,
+    std::unordered_set<int>{},
+    getRandomIntegerInputs<unsigned>)->Arg(TestNumInputs);
+
+BENCHMARK_CAPTURE(BM_EmplaceDuplicate,
+    unordered_set_string_arg,
+    std::unordered_set<std::string>{},
+    getRandomCStringInputs)->Arg(TestNumInputs);
+
 BENCHMARK_MAIN()

Modified: libcxx/trunk/include/__hash_table
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__hash_table?rev=276549&r1=276548&r2=276549&view=diff
==============================================================================
--- libcxx/trunk/include/__hash_table (original)
+++ libcxx/trunk/include/__hash_table Sun Jul 24 01:22:25 2016
@@ -1961,7 +1961,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>
         if (__nd != nullptr)
         {
             for (__nd = __nd->__next_; __nd != nullptr &&
-                                       __constrain_hash(__nd->__hash(), __bc) == __chash;
+                (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
                                                            __nd = __nd->__next_)
             {
                 if (key_eq()(__nd->__upcast()->__value_, __k))




More information about the cfe-commits mailing list