From a6ce0835ca961f4e590694357a9527688ce6d2b2 Mon Sep 17 00:00:00 2001 From: Gregory Popovitch Date: Sat, 2 Nov 2024 18:06:56 -0400 Subject: [PATCH] Add preprocessor macro `PHMAP_DISABLE_MIX` (#259) * Add preporcessor define to disable mixing (`PHMAP_DISABLE_MIX`). * Update `README.md`. --- README.md | 2 +- parallel_hashmap/phmap.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3504d10..2fe0926 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ When an ordering is not needed, a hash container is typically a better choice th - The Abseil hash tables internally randomize a hash seed, so that the table iteration order is non-deterministic. This can be useful to prevent *Denial Of Service* attacks when a hash table is used for a customer facing web service, but it can make debugging more difficult. The *phmap* hashmaps by default do **not** implement this randomization, but it can be enabled by adding `#define PHMAP_NON_DETERMINISTIC 1` before including the header `phmap.h` (as is done in raw_hash_set_test.cc). -- Unlike the Abseil hash maps, we do an internal mixing of the hash value provided. This prevents serious degradation of the hash table performance when the hash function provided by the user has poor entropy distribution. The cost in performance is very minimal, and this helps provide reliable performance even with *imperfect* hash functions. +- Unlike the Abseil hash maps, we do an internal mixing of the hash value provided. This prevents serious degradation of the hash table performance when the hash function provided by the user has poor entropy distribution. The cost in performance is very minimal, and this helps provide reliable performance even with *imperfect* hash functions. Disabling this mixing is possible by defining the preprocessor macro `PHMAP_DISABLE_MIX=1` before `phmap.h` is included, but it is not recommended. ## Memory usage diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index b2367ee..5397d76 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -1892,7 +1892,11 @@ private: { template size_t operator()(const K& key, Args&&...) const { +#if PHMAP_DISABLE_MIX + return h(key); +#else return phmap_mix()(h(key)); +#endif } const hasher& h; }; @@ -3749,7 +3753,11 @@ private: { template size_t operator()(const K& key, Args&&...) const { +#if PHMAP_DISABLE_MIX + return h(key); +#else return phmap_mix()(h(key)); +#endif } const hasher& h; };