A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion.
The classes ankerl::unordered_dense::map and ankerl::unordered_dense::set are (almost) drop-in replacements of std::unordered_map and std::unordered_set. While they don't have as strong iterator / reference stability guaranties, they are typically much faster.
- 1. Overview
- 2. Installation
- 3. Extensions
- 4. Design
The chosen design has a few advantages over std::unordered_map:
- Perfect iteration speed - Data is stored in a
std::vector, all data is contiguous! - Very fast insertion & lookup speed, in the same ballpark as
absl::flat_hash_map - Low memory usage
- Full support for
std::allocators, and polymorphic allocators. There areankerl::unordered_dense::pmrtypedefs available - Customizeable storage type: with a template parameter you can e.g. switch from
std::vectortoboost::interprocess::vectoror any other compatible random-access container. - Better debugging: the underlying data can be easily seen in any debugger that can show an
std::vector.
There's no free lunch, so there are a few disadvantages:
- Deletion speed is relatively slow. This needs two lookups: one for the element to delete, and one for the element that is moved onto the newly empty spot.
- no
const Keyinstd::pair<Key, Value> - Iterators are not stable on insert/erase
The default installation location is /usr/local.
Clone the repository and run these commands in the cloned folder:
mkdir build && cd build
cmake ..
cmake --build . --target installConsider setting an install prefix if you do not want to install unordered_dense system wide, like so:
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=${HOME}/unordered_dense_install ..
cmake --build . --target installTo make use of the installed library, add this to your project:
find_package(unordered_dense CONFIG REQUIRED)
target_link_libraries(your_project_name unordered_dense::unordered_dense)In addition to the standard std::unordered_map API (see https://en.cppreference.com/w/cpp/container/unordered_map) we have additional API leveraging the fact that we're using a random access container internally:
Extracts the internally used container. *this is emptied.
Exposes the underlying values container.
Discards the internally held container and replaces it with the one passed. Non-unique elements are removed, and the container will be partly reordered when non-unique elements are found.
unordered_dense accepts a custom allocator, but you can also specify a custom container for that template argument. That way it is possible to replace the internally used std::vector with e.g. std::deque or any other container like boost::interprocess::vector. This supports fancy pointers (e.g. offset_ptr), so the container can be used with e.g. shared memory provided by boost::interprocess.
The map/set supports two different bucket types. The default should be good for pretty much everyone.
- Up to 2^32 = 4.29 billion elements.
- 8 bytes overhead per bucket.
- up to 2^63 = 9223372036854775808 elements.
- 12 bytes overhead per bucket.
The map/set has two data structures:
std::vector<value_type>which holds all data. map/set iterators are juststd::vector<value_type>::iterator!- An indexing structure (bucket array), which is a flat array with 8-byte buckets.
Whenever an element is added it is emplace_back to the vector. The key is hashed, and an entry (bucket) is added at the
corresponding location in the bucket array. The bucket has this structure:
struct Bucket {
uint32_t dist_and_fingerprint;
uint32_t value_idx;
};Each bucket stores 3 things:
- The distance of that value from the original hashed location (3 most significant bytes in
dist_and_fingerprint) - A fingerprint; 1 byte of the hash (lowest significant byte in
dist_and_fingerprint) - An index where in the vector the actual data is stored.
This structure is especially designed for the collision resolution strategy robin-hood hashing with backward shift deletion.
The key is hashed and the bucket array is searched if it has an entry at that location with that fingerprint. When found, the key in the data vector is compared, and when equal the value is returned.
Since all data is stored in a vector, removals are a bit more complicated:
- First, lookup the element to delete in the index array.
- When found, replace that element in the vector with the last element in the vector.
- Update two locations in the bucket array: First remove the bucket for the removed element
- Then, update the
value_idxof the moved element. This requires another lookup.