HeadlinesBriefing favicon HeadlinesBriefing.com

Fast C++ hopscotch-map library beats std::unordered_map

Hacker News •
×

GitHub hosts Tessil’s hopscotch-map library, a header‑only C++ collection that implements fast hash maps and sets via hopscotch hashing. By using open addressing and cache‑friendly probing, it outperforms the standard std::unordered_map in most benchmarks and rivals Google’s dense_hash_map while consuming less memory. The package exports four primary containers: tsl::hopscotch_map, tsl::hopscotch_set, and their prime‑growth variants, and provides extensive documentation with benchmarks.

Two growth policies govern bucket allocation. The default power‑of‑two policy enables fast modulo via bit masking, ideal when hash functions distribute bits uniformly. When lower‑bit patterns repeat—common with pointer identity hashes—the prime‑growth policy reduces collisions by using a prime‑sized table and a lookup table for modulo. Selecting the appropriate policy can prevent DoS‑style hash attacks for workloads that stress hash distribution.

The library also offers tsl::bhopscotch_map and set variants that require keys to be Less‑Than comparable but guarantee O(log n) lookup and deletion, mitigating worst‑case performance spikes. It supports move‑only types, heterogeneous lookups, and builds without exceptions. Integration is trivial: add the include directory or link the exported CMake target, then drop the containers into existing codebases, making it suitable for high‑performance services.