A C++ implementation of a fast hash map and hash set using hopscotch hashing
AIThis post was created with the assistance of artificial intelligence (AI).

TL;DR

A C++ library implementing hopscotch hashing offers a high-performance, cache-friendly alternative to std::unordered_map. It supports move-only types, heterogeneous lookups, and various growth policies. The library aims to improve speed and memory efficiency.

A new C++ library implementing hopscotch hashing has been released, providing a fast, memory-efficient alternative to std::unordered_map and std::unordered_set. The library claims to outperform traditional hash maps in most scenarios, especially in cache performance, and introduces multiple growth policies to handle different use cases. This development matters because it could influence the choice of data structures in performance-critical C++ applications.

Before you orderOffer from Amazon

Get the latest gadgets delivered free with Prime

  • Fast, free delivery on millions of items
  • Prime Video, Amazon Music and more included
  • Member-only deals all year
Start your free Prime trial Free trial for eligible customers · Cancel anytime
As an affiliate, we earn on qualifying purchases.

The library, called hopscotch-map, is header-only, easy to integrate with CMake, and supports features such as move-only types, heterogeneous lookups, and storing hash values for faster rehashing. It offers several classes including tsl::hopscotch_map, tsl::hopscotch_set, and their prime-growth variants, designed to handle poor hash functions better. Benchmarks suggest that tsl::hopscotch_map performs faster than std::unordered_map in many cases, with lower memory consumption.

Additionally, the library provides tsl::bhopscotch_map and tsl::bhopscotch_set, which offer worst-case O(log n) lookups and deletions, making them resistant to hash DoS attacks. The API is similar to standard C++ unordered containers, with some differences in iterator invalidation and mutability. The library supports exception-disabled builds and can handle move-only types efficiently, provided their move constructors are noexcept.

Impact on C++ Performance and Memory Management

This library could influence how developers choose hash tables in performance-sensitive applications. Its cache-friendly design and flexible growth policies may lead to faster data access and reduced memory footprint compared to std::unordered_map. The support for move-only types and heterogeneous lookups also broadens its applicability in modern C++ codebases, potentially improving overall system efficiency.

Products Worth Considering

Apple Watch Series 11 GPS + Cellular, 46mm Jet Black Aluminum Case with Black Sport Band (M/L)

Apple Watch Series 11 GPS + Cellular, 46mm Jet Black Aluminum Case with Black Sport Band (M/L)

EcoFlow DELTA 3 Max Portable Power Station, 2048Wh LFP Battery

EcoFlow DELTA 3 Max Portable Power Station, 2048Wh LFP Battery

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 separate chaining or open addressing, with performance depending on hash quality and load factor. Hopscotch hashing, introduced in the early 2000s, offers a way to maintain close-to-constant time lookups by keeping elements within a small neighborhood of their original hash bucket. Recent interest in cache efficiency and memory optimization has led to renewed focus on hopscotch-based structures, with this library representing a modern, optimized implementation tailored for C++.

“Our hopscotch-map library offers a significant performance boost over std::unordered_map, especially in cache-sensitive scenarios.”

— Library author

Unverified Performance Claims and Usage Scenarios

While initial benchmarks are promising, comprehensive testing across diverse workloads and real-world applications is still ongoing. It is not yet confirmed how the library performs under heavy concurrency or with highly irregular hash functions. Compatibility with all C++ compilers and integration into large codebases remains to be tested.

Next Steps: Broader Testing and Community Adoption

Developers and researchers are expected to test the library in various environments, compare it with other high-performance hash structures, and evaluate its robustness. Future updates may include additional features, optimizations, and expanded documentation. Monitoring adoption in open-source projects will help determine its impact on C++ programming practices.

Key Questions

How does hopscotch hashing differ from standard hashing methods?

Hopscotch hashing maintains elements within a small neighborhood of their original hash bucket, enabling faster lookups and better cache performance than traditional open addressing or chaining methods.

Can this library handle thread-safe operations?

The library supports multiple readers and single writers similar to std::unordered_map, but does not provide built-in thread safety for concurrent modifications.

Is this library suitable for all hash functions?

It performs best with good hash functions; however, the prime growth policy can mitigate poor hash distributions, making it versatile for various scenarios.

Does the library support move-only types?

Yes, but move-only types must have noexcept move constructors to ensure proper operation.

When will the library be widely adopted?

Adoption depends on further testing, community feedback, and integration into popular projects. It is currently in early release stages.

Source: Hacker News


You May Also Like

Why Your Phone Storage Fills Up So Fast (And the Fix)

Meta description: Many users wonder why their phone storage fills up so quickly and how to fix it—discover simple tips to reclaim space before it’s too late.

Virginia Bans Sale Of Geolocation Data

Virginia enacts a law prohibiting the sale of geolocation data, marking a significant step in privacy regulation. Details on scope and enforcement remain unclear.

Golang Proposal: Container/: Generic Collection Types

The Go language proposal adds ‘container/’ packages for generic collection types, marking a step toward more flexible data structures in Go.

Silk: Open-source cooperative fiber scheduler

Silk is an open-source fiber scheduler for Linux featuring per-CPU threads, io_uring integration, and topology-aware work-stealing, enhancing concurrency and efficiency.