Improve performance of unordered_set<uint32_t>::find by 45%. Add benchmarks.

This patch improves the performance of unordered_set's find by 45% when
the value exists within the set. __hash_tables find method
needs to check if it's reached the end of the bucket by constraining the
hash of the current node and checking it against the bucket index. However
constraining the hash is an expensive operations and it can be avoided if the
two unconstrained hashes are equal. This patch applies that optimization.

This patch also adds a top level directory called benchmarks. 'benchmarks/'
is intended to store any/all benchmarks written for the standard library.
Currently nothing is done with files under 'benchmarks/' but I would like
to move towards introducing a formal format and test runner.

llvm-svn: 274423
This commit is contained in:
Eric Fiselier 2016-07-02 05:19:59 +00:00
parent 4f905b8daa
commit f977598bb6
2 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1,29 @@
#include <unordered_set>
#include <vector>
#include <cstdint>
#include "benchmark/benchmark_api.h"
template <class IntT>
std::vector<IntT> getInputs(size_t N) {
std::vector<IntT> inputs;
for (size_t i=0; i < N; ++i) {
inputs.push_back(i);
}
return inputs;
}
template <class Container, class Inputs>
void BM_SetLookup(benchmark::State& st, Container c, Inputs const& in) {
c.insert(in.begin(), in.end());
const auto end = in.end();
while (st.KeepRunning()) {
for (auto it = in.begin(); it != end; ++it) {
benchmark::DoNotOptimize(c.find(*it++));
}
}
}
BENCHMARK_CAPTURE(BM_SetLookup, uint32_lookup,
std::unordered_set<uint32_t>{}, getInputs<uint32_t>(1024));
BENCHMARK_MAIN()

View File

@ -2201,7 +2201,8 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
if (__nd != nullptr)
{
for (__nd = __nd->__next_; __nd != nullptr &&
__constrain_hash(__nd->__hash_, __bc) == __chash;
(__hash == __nd->__hash_
|| __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
if (key_eq()(__nd->__value_, __k))
@ -2230,7 +2231,8 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
if (__nd != nullptr)
{
for (__nd = __nd->__next_; __nd != nullptr &&
__constrain_hash(__nd->__hash_, __bc) == __chash;
(__hash == __nd->__hash_
|| __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
if (key_eq()(__nd->__value_, __k))