TL;DR
A new C++ library implements a hopscotch hashing-based hash map and set, offering faster performance and lower memory usage than traditional std::unordered_map. The library supports move-only types, heterogeneous lookups, and multiple growth policies.
A new C++ header-only library implementing a fast hash map and hash set based on hopscotch hashing has been released, promising significant performance improvements over std::unordered_map and std::unordered_set.
The library, known as hopscotch-map, uses open-addressing and hopscotch hashing techniques to resolve collisions, resulting in a cache-friendly structure that outperforms traditional hash maps in most benchmarks, according to the developer’s reports and community testing.
It provides classes such as tsl::hopscotch_map and tsl::hopscotch_set, which are optimized for speed and memory efficiency. The implementation supports move-only types, heterogeneous lookups, and optional precomputed hashes, making it versatile for various use cases. It also offers different growth policies, including power-of-two and prime-based strategies, to better handle poor hash functions or specific data patterns.
Compared to the standard library, this implementation aims to provide faster lookups and insertions, with the added benefit of lower memory consumption, especially in large datasets. The library is compatible with CMake and can be used with exceptions disabled, making it suitable for performance-critical applications.
Performance and Memory Benefits of Hopscotch Hashing
This library introduces a high-performance alternative to std::unordered_map and std::unordered_set, especially beneficial in scenarios requiring fast access and low memory usage. Its cache-friendly design and collision resolution method reduce latency, which can significantly enhance the efficiency of data-intensive applications, such as databases, real-time systems, and high-frequency trading platforms.
By supporting move-only types and heterogeneous lookups, it broadens the applicability of hash tables in modern C++ programming, including use with smart pointers and custom key types. The optional hash precomputation feature further accelerates repeated lookups when the hash is known beforehand.
C++ hopscotch hashing library
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Background on Hash Map Implementations and Hopscotch Hashing
Traditional hash maps like std::unordered_map rely on chaining or open addressing with linear probing, which can lead to cache misses and performance bottlenecks. Hopscotch hashing, first introduced in academic research, offers a collision resolution method that maintains elements within a small neighborhood, improving cache locality.
Recent developments in C++ libraries have explored alternative collision strategies, but practical, high-performance implementations are still emerging. The hopscotch-map library consolidates these advances into a header-only, easy-to-integrate package, with benchmarks indicating performance gains over standard containers, especially in large datasets.
Community feedback and benchmarking reports suggest that this approach is gaining traction among developers seeking optimized hash tables for demanding applications.
“Our hopscotch-based hash map offers a significant speed advantage over std::unordered_map in most typical workloads, with lower memory overhead.”
— Library author
high performance hash map C++
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Performance Claims and Compatibility Clarifications
While benchmark results are promising, comprehensive, peer-reviewed performance comparisons across diverse workloads are still pending. Compatibility with all standard C++ features and edge cases, such as concurrent access or extremely poor hash functions, requires further testing.
It is also not yet clear how the library performs under multi-threaded conditions or in highly constrained environments, as detailed concurrency benchmarks are not available at this stage.
cache friendly hash set C++
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Future Developments and Community Adoption of hopscotch-map
The next steps include broader community testing, integration into larger projects, and potential development of thread-safe variants. Developers are encouraged to evaluate the library in their specific use cases and contribute feedback or improvements.
Further benchmarking, especially against other high-performance hash tables, and exploration of custom growth policies are anticipated to enhance the library’s robustness and applicability.
header-only hash map C++
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Key Questions
How does hopscotch hashing improve performance over std::unordered_map?
Hopscotch hashing maintains elements close to their original hash position, reducing cache misses and speeding up lookups and insertions compared to chaining or linear probing methods used in std::unordered_map.
Is the library suitable for multi-threaded applications?
The library supports multiple readers with no writers, similar to std::unordered_map, but does not currently include built-in thread safety for concurrent modifications. Developers should implement external synchronization for multi-threaded writes.
Can I use this library with custom key types or smart pointers?
Yes, the library supports move-only types, heterogeneous lookups, and can handle custom key types such as smart pointers, provided they meet the necessary requirements.
What are the main differences between the power-of-two and prime growth policies?
The power-of-two policy offers faster rehashing but may cause more collisions with poor hash functions. The prime policy provides better distribution in such cases, at the cost of slightly slower growth.
Is the library compatible with exception-disabled builds?
Yes, the library can be used without exceptions by defining TSL_NO_EXCEPTIONS, replacing exception throws with std::terminate.
Source: Hacker News