Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)

if(NOT WIN32)
hashmap_executable(HashMapBenchmark src/HashMapBenchmark.cpp)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
target_compile_options(HashMapBenchmark PRIVATE -msse4.2)
if(NOT CMAKE_CROSSCOMPILING)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-march=native" HASHMAP_HAS_MARCH_NATIVE)
if(HASHMAP_HAS_MARCH_NATIVE)
target_compile_options(HashMapBenchmark PRIVATE -march=native)
endif()
endif()

option(HASHMAP_BENCHMARK_WITH_ABSL "Use installed Abseil when available" ON)
Expand All @@ -67,6 +71,16 @@ if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
endif()
endif()

option(HASHMAP_BENCHMARK_WITH_BOOST "Use installed Boost.Unordered when available" ON)
if(HASHMAP_BENCHMARK_WITH_BOOST)
find_path(HASHMAP_BOOST_INCLUDE_DIR NAMES boost/unordered/unordered_flat_map.hpp)
mark_as_advanced(HASHMAP_BOOST_INCLUDE_DIR)
if(HASHMAP_BOOST_INCLUDE_DIR)
target_include_directories(HashMapBenchmark SYSTEM PRIVATE "${HASHMAP_BOOST_INCLUDE_DIR}")
target_compile_definitions(HashMapBenchmark PRIVATE HASHMAP_BENCHMARK_HAS_BOOST)
endif()
endif()

add_test(NAME HashMapBenchmarkSmoke COMMAND HashMapBenchmark -t 4 -c 1000 -i 1000)
endif()
endif()
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ and deleted.

The benchmark uses `std::chrono::steady_clock` and requires no benchmark
framework or vendored dependencies. CMake enables comparisons against installed
Abseil and sparsehash when found. Set `HASHMAP_BENCHMARK_WITH_ABSL=OFF` or
`HASHMAP_BENCHMARK_WITH_SPARSEHASH=OFF` to disable either comparison. Abseil must
provide its CMake package; sparsehash must provide `google/dense_hash_map`.
No dependencies are downloaded during configuration.
Abseil, sparsehash, and Boost.Unordered when found. Set
`HASHMAP_BENCHMARK_WITH_ABSL=OFF`, `HASHMAP_BENCHMARK_WITH_SPARSEHASH=OFF`, or
`HASHMAP_BENCHMARK_WITH_BOOST=OFF` to disable any comparison. Abseil must
provide its CMake package; sparsehash must provide `google/dense_hash_map`;
Boost must provide `boost/unordered/unordered_flat_map.hpp`. No dependencies are
downloaded during configuration.

The benchmark is built on Linux and macOS; Windows builds the example and tests.
All containers in a run use the same hash: hardware CRC32 on x86-64 and
MurmurHash3's `fmix64` integer mixer on ARM and other architectures. Hash choices
differ across architectures, so compare containers within the same run.
Each container uses its default hash (`HashMap` automatically mixes hashes that
do not define `is_avalanching`), and `google::dense_hash_map` uses `HashMap`'s
mixer.

I ran this benchmark on the following configuration:

Expand Down
44 changes: 43 additions & 1 deletion include/rigtorp/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ probing.

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>

#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
#include <intrin.h>
#endif

namespace rigtorp {

namespace detail {
Expand All @@ -53,6 +58,39 @@ namespace detail {
}

#undef RIGTORP_HASHMAP_NOINLINE

template <typename Hash>
inline constexpr bool is_avalanching_v =
requires { typename Hash::is_avalanching; };

inline size_t mix_hash(size_t h) noexcept {
if constexpr (sizeof(size_t) >= 8) {
#if defined(__SIZEOF_INT128__)
__extension__ using uint128_t = unsigned __int128;
const uint128_t r =
static_cast<uint128_t>(h) * UINT64_C(0x9ddfea08eb382d69);
return static_cast<size_t>(r) ^ static_cast<size_t>(r >> 64);
#elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
unsigned __int64 hi = 0;
const unsigned __int64 lo = _umul128(h, 0x9ddfea08eb382d69ULL, &hi);
return static_cast<size_t>(lo ^ hi);
#else
h ^= h >> 30;
h *= 0xbf58476d1ce4e5b9ULL;
h ^= h >> 27;
h *= 0x94d049bb133111ebULL;
h ^= h >> 31;
return h;
#endif
} else {
h ^= h >> 16;
h *= 0x85ebca6bU;
h ^= h >> 13;
h *= 0xc2b2ae35U;
h ^= h >> 16;
return h;
}
}
} // namespace detail

template <typename Key, typename T, typename Hash = std::hash<Key>,
Expand Down Expand Up @@ -350,8 +388,12 @@ class HashMap {

template <typename K>
size_t key_to_idx(const K &key) const noexcept(noexcept(hasher()(key))) {
size_t h = hasher()(key);
if constexpr (!detail::is_avalanching_v<hasher>) {
h = detail::mix_hash(h);
}
const size_t mask = buckets_.size() - 1;
return hasher()(key) & mask;
return h & mask;
}

size_t probe_next(size_t idx) const noexcept {
Expand Down
Loading
Loading