|
ADS
Algorithms and Data Structures
|
The cuckoo hashing with buckets of 4 places and a search in breadth of Li, Andersen, Kaminsky and Freedman (the table of MemC3 and libcuckoo), beyond the book: the engineered cuckoo table of ADS [see CuckooHashing.h for the cuckoo hashing of Pagh and Rodler, one key per place]. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/dictionary/FastHash.h"#include "ADS/memory/AllocatorAware.h"#include "ADS/memory/Prefetch.h"#include <bit>#include <cstddef>#include <cstdint>#include <functional>#include <iterator>#include <optional>#include <utility>#include <vector>#include "ADS/dictionary/impl/BucketCuckooTable.tpp"Go to the source code of this file.
Classes | |
| class | ADS::BucketCuckooTable< K, V, H, Eq, A > |
| a DICTIONARY on the cuckoo hashing with buckets of 4 places More... | |
| struct | ADS::BucketCuckooTable< K, V, H, Eq, A >::Places |
| the places, for SlotIterator More... | |
Namespaces | |
| namespace | ADS |
The cuckoo hashing with buckets of 4 places and a search in breadth of Li, Andersen, Kaminsky and Freedman (the table of MemC3 and libcuckoo), beyond the book: the engineered cuckoo table of ADS [see CuckooHashing.h for the cuckoo hashing of Pagh and Rodler, one key per place].
A key with the hash \( h \) may be in any of the 4 places of two buckets, so that a search looks at 8 places in the worst case, and the table reaches a load of 95 % and more before an insertion fails, against less than 50 % with one key per place.
Each place has a tag of 8 bits, 0 when empty and otherwise 8 bits of the hash of its key (1 in place of 0); the 4 tags of a bucket are a word of 32 bits, compared with the tag of a key all at once by broadword operations, so that a key is compared only in the places whose tag matches. The first bucket of a key is given by the top bits of \( h \), the second by the first and the tag, \( b_2 = b_1 \oplus ( g( t ) \wedge ( B - 1 ) \vee 1 ) \) for \( B \) buckets (a power of 2) and a mixing \( g \) of the tag (the partial-key cuckoo hashing of MemC3), an involution, so that a key moves to its other bucket with no need to hash it again.
An insertion puts a new key in a free place of its two buckets if any; otherwise it searches in breadth, from the places of its buckets, a path of at most 5 moves that ends in a bucket with a free place (each key of a bucket of the path moving to its other bucket), and moves the keys along it from the end, so that the new key takes the first place of the path. The search in breadth finds a shorter path than the random walk of the cuckoo hashing, and moves fewer keys. When no path is found within 5 moves and 1024 buckets (or past the load of 98 %), the table doubles and every key is placed again, which costs \( O( 1 ) \) amortized per insertion; when the hash of a key reads memory that the key points to, as for the strings, the doubling hashes all the keys first, prefetching the memory of the key 8 places ahead, and then places them, prefetching the first bucket of the key 8 places ahead, so that the misses of the keys overlap. A removal clears the tag of the place.
No doubling separates keys whose hashes are equal, and more than 8 of them find no place at any size of the table. A key that still finds no place after 2 doublings of its insertion, or in a table at most half full, therefore goes to a stash, a vector of pairs searched in order after the two buckets (the cuckoo hashing with a stash of Kirsch, Mitzenmacher and Wieder, whose stash has a constant size, here any); a doubling places the pairs of the stash again with the others, and those that find no place go to the new stash. A table that has held at most \( n \) keys thus has at most \( \max( P_0 , 4 n ) \) places, \( P_0 \) those of the empty table, an insertion makes at most 2 doublings, and a search of a key that is not in its buckets costs one test while the stash is empty (as it is with any but a degenerate hash) and a scan of the stash otherwise. A removal from the stash leaves a hole there, which the next key of the stash fills.
The iterators visit the pairs in the order of the places, the stash last, as constant pairs of references \( ( \mathit{key} , \mathit{value} ) \); find() gives a value to change in place. An insertion of a new key may move other keys and invalidates the iterators; a removal keeps them valid but the one on the removed pair.
The hash of a key is h( k ), taken as it is if H declares is_avalanching [see FastHash.h] and mixed by hashWord() otherwise. The keys and the values must be default constructible and movable.
The tags and the places come from the allocator A, std::allocator by default, with the rules of the standard [see AllocatorAware.h].
The contracts [see Contract.h]: after insert() the key is in the table and after remove() it is not; the invariant is that each key is once in one of its two buckets with its tag or in the stash, and that the tags and the stash count the pairs, checked on the buckets of the key of the operation only past 1024 places. The operations are defined in BucketCuckooTable.tpp.