Skip chash computation in insert/emplace if the unconstrained hash matches.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@276549 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2016-07-24 06:22:25 +00:00
parent 8b5233f11c
commit d7570906b1
4 changed files with 78 additions and 1 deletions

View File

@@ -34,6 +34,38 @@ void BM_InsertValueRehash(benchmark::State& st, Container c, GenInputs gen) {
} }
} }
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> template <class Container, class GenInputs>
static void BM_Find(benchmark::State& st, Container c, GenInputs gen) { static void BM_Find(benchmark::State& st, Container c, GenInputs gen) {
auto in = gen(st.range_x()); auto in = gen(st.range_x());

View File

@@ -130,4 +130,11 @@ inline std::vector<std::string> getReverseSortedStringInputs(size_t N) {
return inputs; 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 #endif // BENCHMARK_GENERATE_INPUT_HPP

View File

@@ -265,4 +265,42 @@ BENCHMARK_CAPTURE(BM_FindRehash,
std::unordered_set<std::string>{}, std::unordered_set<std::string>{},
getRandomStringInputs)->Arg(TestNumInputs); 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() BENCHMARK_MAIN()

View File

@@ -1961,7 +1961,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const&
if (__nd != nullptr) if (__nd != nullptr)
{ {
for (__nd = __nd->__next_; __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_) __nd = __nd->__next_)
{ {
if (key_eq()(__nd->__upcast()->__value_, __k)) if (key_eq()(__nd->__upcast()->__value_, __k))