From aa5fa26b49d324b8dfe4c6e46362a7e8640eabcc Mon Sep 17 00:00:00 2001 From: Davi Reis Date: Tue, 13 Mar 2012 20:25:06 -0300 Subject: [PATCH] Strange optimizations for 64 bit integers. --- cxxmph/bm_map.cc | 2 ++ cxxmph/seeded_hash.h | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cxxmph/bm_map.cc b/cxxmph/bm_map.cc index a90b7b2..8195217 100644 --- a/cxxmph/bm_map.cc +++ b/cxxmph/bm_map.cc @@ -88,12 +88,14 @@ using namespace cxxmph; int main(int argc, char** argv) { srandom(4); + /* Benchmark::Register(new BM_CreateUrls>("URLS100k")); Benchmark::Register(new BM_CreateUrls>("URLS100k")); Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0)); Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0)); Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0.9)); Benchmark::Register(new BM_SearchUrls>("URLS100k", 10*1000 * 1000, 0.9)); + */ Benchmark::Register(new BM_SearchUint64>); Benchmark::Register(new BM_SearchUint64>); Benchmark::RunAll(); diff --git a/cxxmph/seeded_hash.h b/cxxmph/seeded_hash.h index e204d36..f0bab05 100644 --- a/cxxmph/seeded_hash.h +++ b/cxxmph/seeded_hash.h @@ -9,8 +9,9 @@ #include "MurmurHash3.h" #include "stringpiece.h" -// From murmur, only used naively to extend 32 bits functions to 64 bits. +// From murmur, only used naively to extend 32 bits functions to 128 bits. uint32_t fmix ( uint32_t h ); +uint64_t fmix ( uint64_t h ); namespace cxxmph { @@ -57,6 +58,19 @@ struct Murmur3StringPiece { } }; +struct Murmur3Fmix64bitsType { + template + uint32_t operator()(const Key& k) const { + return fmix(*reinterpret_cast(&k)); + } + template + void hash64(const Key& k, uint32_t* out) const { + uint64_t h = fmix(*reinterpret_cast(&k)); + *reinterpret_cast(out) = h; + *reinterpret_cast(out + 2) = h; + } +}; + template <> struct seeded_hash_function { template @@ -87,6 +101,20 @@ struct seeded_hash_function { } }; +template <> +struct seeded_hash_function { + template + uint32_t operator()(const Key& k, uint32_t seed) const { + return fmix(k + seed); + } + template + void hash64(const Key& k, uint32_t seed, uint32_t* out) const { + *reinterpret_cast(out) = fmix(k ^ seed); + *(out + 2) = fmix(*out); + } +}; + + template struct seeded_hash { typedef seeded_hash_function hash_function; }; // Use Murmur3 instead for all types defined in std::hash, plus @@ -117,9 +145,9 @@ template <> struct seeded_hash > template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; template <> struct seeded_hash > -{ typedef seeded_hash_function hash_function; }; +{ typedef seeded_hash_function hash_function; }; template <> struct seeded_hash > -{ typedef seeded_hash_function hash_function; }; +{ typedef seeded_hash_function hash_function; }; } // namespace cxxmph