1
Fork 0

Finally beat STL. Trying improvement around cuckoo hashing idea.

This commit is contained in:
Davi Reis 2012-03-14 01:29:13 -03:00
parent aa5fa26b49
commit 86797b6402
4 changed files with 87 additions and 26 deletions

View File

@ -49,6 +49,7 @@ class BM_SearchUrls : public SearchUrlsBenchmark {
mymap_[*it] = *it;
}
mymap_.rehash(mymap_.bucket_count());
fprintf(stderr, "Occupation: %f\n", static_cast<float>(mymap_.size())/mymap_.bucket_count());
return true;
}
MapType mymap_;
@ -57,7 +58,7 @@ class BM_SearchUrls : public SearchUrlsBenchmark {
template <class MapType>
class BM_SearchUint64 : public SearchUint64Benchmark {
public:
BM_SearchUint64() : SearchUint64Benchmark(10000, 10*1000*1000) { }
BM_SearchUint64() : SearchUint64Benchmark(100000, 10*1000*1000) { }
virtual bool SetUp() {
if (!SearchUint64Benchmark::SetUp()) return false;
for (int i = 0; i < values_.size(); ++i) {
@ -88,15 +89,13 @@ using namespace cxxmph;
int main(int argc, char** argv) {
srandom(4);
/*
Benchmark::Register(new BM_CreateUrls<mph_map<StringPiece, StringPiece>>("URLS100k"));
Benchmark::Register(new BM_CreateUrls<unordered_map<StringPiece, StringPiece>>("URLS100k"));
Benchmark::Register(new BM_SearchUrls<mph_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0));
Benchmark::Register(new BM_SearchUrls<unordered_map<StringPiece, StringPiece, Murmur3StringPiece>>("URLS100k", 10*1000 * 1000, 0));
Benchmark::Register(new BM_SearchUrls<mph_map<StringPiece, StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
Benchmark::Register(new BM_SearchUrls<unordered_map<StringPiece, StringPiece, Murmur3StringPiece>>("URLS100k", 10*1000 * 1000, 0.9));
*/
Benchmark::Register(new BM_SearchUint64<unordered_map<uint64_t, uint64_t>>);
Benchmark::Register(new BM_SearchUint64<mph_map<uint64_t, uint64_t>>);
Benchmark::Register(new BM_SearchUint64<unordered_map<uint64_t, uint64_t>>);
Benchmark::RunAll();
}

View File

@ -25,6 +25,7 @@
#include <stdint.h>
#include <cassert>
#include <climits>
#include <cmath>
#include <unordered_map> // for std::hash
#include <vector>
@ -63,6 +64,12 @@ class MPHIndex {
template <class SeededHashFcn, class Key> // must agree with Reset
uint32_t minimal_perfect_hash(const Key& x) const;
// Crazy functions. Ignore.
template <class SeededHashFcn, class Key> // must agree with Reset
uint32_t cuckoo_hash(const Key& x, const uint32_t* h, uint8_t nest) const;
template <class SeededHashFcn, class Key> // must agree with Reset
void hash_vector(const Key& x, uint32_t* h) const;
// Serialization for mmap usage - not tested well, ping me if you care.
// Serialized tables are not guaranteed to work across versions or different
// endianness (although they could easily be made to be).
@ -94,6 +101,8 @@ class MPHIndex {
// Partition vertex count, derived from c parameter.
uint32_t r_;
uint32_t nest_displacement_[3]; // derived from r_
// The array containing the minimal perfect hash function graph. Do not use
// c++ vector to make mmap based backing easier.
const uint8_t* g_;
@ -118,6 +127,16 @@ class MPHIndex {
};
template <class T>
T nexthigher(T k) {
if (k == 0)
return 1;
k--;
for (int i=1; i<sizeof(T)*CHAR_BIT; i<<=1)
k = k | k >> i;
return k+1;
}
// Template method needs to go in the header file.
template <class SeededHashFcn, class ForwardIterator>
bool MPHIndex::Reset(
@ -129,6 +148,13 @@ bool MPHIndex::Reset(
m_ = size;
r_ = static_cast<uint32_t>(ceil((c_*m_)/3));
if ((r_ % 2) == 0) r_ += 1;
nest_displacement_[0] = 0;
nest_displacement_[1] = r_;
nest_displacement_[2] = (r_ << 1);
// This can be used to speed mods, but increases occupation too much.
// Needs to try http://gmplib.org/manual/Integer-Exponentiation.html instead
// r_ = nexthigher(r_);
n_ = 3*r_;
k_ = 1U << b_;
@ -174,15 +200,24 @@ bool MPHIndex::Mapping(
return false;
}
template <class SeededHashFcn, class Key>
uint32_t MPHIndex::cuckoo_hash(const Key& key, const uint32_t* h, uint8_t nest) const {
return (h[nest] % r_) + nest_displacement_[nest];
}
template <class SeededHashFcn, class Key>
void MPHIndex::hash_vector(const Key& key, uint32_t* h) const {
SeededHashFcn().hash64(key, hash_seed_[0], reinterpret_cast<uint32_t*>(&h));
}
template <class SeededHashFcn, class Key>
uint32_t MPHIndex::perfect_hash(const Key& key) const {
uint32_t h[4];
SeededHashFcn().hash64(key, hash_seed_[0], reinterpret_cast<uint32_t*>(&h));
// for (int i = 0; i < 3; ++i) h[i] = SeededHashFcn()(key, hash_seed_[i]);
assert(r_);
h[0] = h[0] % r_;
h[1] = h[1] % r_ + r_;
h[2] = h[2] % r_ + (r_ << 1);
h[0] = (h[0] % r_) + nest_displacement_[0];
h[1] = (h[1] % r_) + nest_displacement_[1];
h[2] = (h[2] % r_) + nest_displacement_[2];
assert(g_size_);
// cerr << "g_.size() " << g_size_ << " h0 >> 2 " << (h[0] >> 2) << endl;
assert((h[0] >> 2) <g_size_);

View File

@ -73,7 +73,7 @@ class mph_map {
data_type& operator[](const key_type &k);
const data_type& operator[](const key_type &k) const;
size_type bucket_count() const { return index_.size() + slack_.bucket_count(); }
size_type bucket_count() const { return index_.perfect_hash_size() + slack_.bucket_count(); }
void rehash(size_type nbuckets /*ignored*/);
protected: // mimicking STL implementation
@ -100,9 +100,17 @@ class mph_map {
return hollow_const_iterator<std::vector<value_type>>(&values_, &present_, it);
}
static void set_2bit_value(uint8_t *d, uint32_t i, uint8_t v) {
d[(i >> 2)] &= ((v << ((i & 3) << 1)) | valuemask[i & 3]);
}
static uint32_t get_2bit_value(const uint8_t* d, uint32_t i) {
return (d[(i >> 2)] >> (((i & 3) << 1)) & 3);
}
void pack();
std::vector<value_type> values_;
std::vector<bool> present_;
const uint8_t* nests_;
SimpleMPHIndex<Key, typename seeded_hash<HashFcn>::hash_function> index_;
// TODO(davi) optimize slack to no hold a copy of the key
typedef unordered_map<Key, uint32_t, HashFcn, EqualKey, Alloc> slack_type;
@ -187,28 +195,48 @@ MPH_MAP_METHOD_DECL(void_type, erase)(const key_type& k) {
}
MPH_MAP_METHOD_DECL(const_iterator, find)(const key_type& k) const {
if (__builtin_expect(!slack_.empty(), 0)) {
auto it = slack_.find(k);
if (it != slack_.end()) return make_iterator(values_.begin() + it->second);
uint32_t h[4];
auto nest = nests_[index_.hash_vector(k, reinterpret_cast<uint32_t*>(&h))];
if (nest != kNestCollision) {
auto vit = values_.begin() + h[nest];
if (equal_(k, vit->first)) return make_iterator(vit);
}
if (__builtin_expect(index_.size() == 0, 0)) return end();
return slow_find(k);
}
MPH_MAP_METHOD_DECL(const_iterator, slow_find)(const key_type& k) const {
auto id = index_.perfect_hash(k);
if (!present_[id]) return end();
auto it = make_iterator(values_.begin() + id);
if (__builtin_expect(equal_(k, it->first), 1)) return it;
auto vit = values_.begin() + id;
if (equal_(k, vit->first)) return make_iterator(vit);
if (__builtin_expect(!slack_.empty(), 0)) {
auto sit = slack_.find(k);
if (it != slack_.end()) return make_iterator(values_.begin() + sit->second);
}
return end();
}
MPH_MAP_METHOD_DECL(iterator, find)(const key_type& k) {
if (__builtin_expect(!slack_.empty(), 0)) {
auto it = slack_.find(k);
if (it != slack_.end()) return make_iterator(values_.begin() + it->second);
uint32_t h[4];
auto nest = nests_[index_.hash_vector(k, reinterpret_cast<uint32_t*>(&h))];
if (nest != kNestCollision) {
auto vit = values_.begin() + h[nest];
if (equal_(k, vit->first)) return make_iterator(vit);
}
if (__builtin_expect(index_.size() == 0, 0)) return end();
return slow_find(k);
}
MPH_MAP_METHOD_DECL(iterator, slow_find)(const key_type& k) {
auto id = index_.perfect_hash(k);
if (!present_[id]) return end();
auto it = make_iterator(values_.begin() + id);
if (__builtin_expect(equal_(k, it->first), 1)) return it;
auto vit = values_.begin() + id;
if (equal_(k, vit->first)) return make_iterator(vit);
if (__builtin_expect(!slack_.empty(), 0)) {
auto sit = slack_.find(k);
if (it != slack_.end()) return make_iterator(values_.begin() + sit->second);
}
return end();
}

View File

@ -65,9 +65,8 @@ struct Murmur3Fmix64bitsType {
}
template <class Key>
void hash64(const Key& k, uint32_t* out) const {
uint64_t h = fmix(*reinterpret_cast<uint64_t*>(&k));
*reinterpret_cast<uint64_t*>(out) = h;
*reinterpret_cast<uint64_t*>(out + 2) = h;
*reinterpret_cast<uint64_t*>(out) = fmix(k);
*(out + 2) = fmix(*out);
}
};
@ -145,9 +144,9 @@ template <> struct seeded_hash<std::hash<long> >
template <> struct seeded_hash<std::hash<unsigned long> >
{ typedef seeded_hash_function<Murmur3> hash_function; };
template <> struct seeded_hash<std::hash<long long> >
{ typedef seeded_hash_function<Murmur3Fmix64bitsType> hash_function; };
{ typedef seeded_hash_function<Murmur3> hash_function; };
template <> struct seeded_hash<std::hash<unsigned long long> >
{ typedef seeded_hash_function<Murmur3Fmix64bitsType> hash_function; };
{ typedef seeded_hash_function<Murmur3> hash_function; };
} // namespace cxxmph