|
ADS
Algorithms and Data Structures
|
A hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of ADS [see ADT.h], the one that the benchmark of the dictionaries compares with boost::unordered_flat_map, while OpenHashTable [see OpenHashTable.h] stays the table of section 7.2.2. More...
#include "ADS/Abi.h"#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/SimdInline.h"#include "ADS/Portable.h"#include "ADS/Wide.h"#include "ADS/dictionary/FastHash.h"#include "ADS/memory/AllocatorAware.h"#include "ADS/memory/Prefetch.h"#include <array>#include <bit>#include <cstddef>#include <cstdint>#include <cstring>#include <functional>#include <memory>#include <new>#include <optional>#include <tuple>#include <type_traits>#include <utility>#include <vector>#include "ADS/dictionary/impl/FlatHashTable.tpp"Go to the source code of this file.
Classes | |
| class | ADS::FlatHashTable< K, V, H, Eq, A > |
| a hash table with open addressing and control bytes in groups of 15 More... | |
| class | ADS::FlatHashTable< K, V, H, Eq, A >::const_iterator |
| the iterator on the full places, by its group, the slot of the first place of the group and the mask of the full places of the group from its own on, the lowest bit its place: a step clears that bit and keeps the places still full in the control bytes, so that the next place depends on the previous one by two operations on the mask and a place emptied meanwhile is skipped; past the group it skips the empty groups whole, up to a sentinel group after the last, whose byte 0 is full and whose first place is the end, so that a step tests no bound More... | |
Namespaces | |
| namespace | ADS |
Macros | |
| #define | ADS_FLAT_INLINE ADS_ALWAYS_INLINE |
| a function inlined even where the compiler would not | |
| #define | ADS_FLAT_NOINLINE ADS_NOINLINE |
| a function never inlined, the rare path out of a loop | |
A hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of ADS [see ADT.h], the one that the benchmark of the dictionaries compares with boost::unordered_flat_map, while OpenHashTable [see OpenHashTable.h] stays the table of section 7.2.2.
The places come in groups of 15, after the Boost table: beside the vector of the pairs, each group has 16 bytes aligned to 16, one control byte per place, empty (0) or full, in which case the byte is a fingerprint of 8 bits of the hash of its key, never 0, and a 16th byte of overflow. The 15 control bytes are compared with the fingerprint of a key all at once: with ADS_WITH_HIGHWAY by one comparison of the vectors of Google Highway at the instructions of the compiler [see SimdInline.h] (one of SSE2 on x86-64, of NEON on ARM64), and otherwise by broadword operations (SWAR, SIMD within a register) on two words of 64 bits; ADS_FLAT_SIMD, defined as 0 before the header, selects the broadword operations with Highway too. A key is compared only in the places whose fingerprint matches, a false match costing a comparison and nothing else. When some fingerprint matches, the first line of cache of the pairs of the group is prefetched, as in Boost: the address depends only on the group, so that the processor issues the prefetch on the predicted branch while the control bytes are still on their way, and the two misses of a search overlap in part.
The hash \( h( k ) \) is mixed as in Boost: the product of \( h( k ) \) by \( \lfloor 2^{64} / \phi \rfloor \) on 128 bits, its two halves XORed, so that every bit of the result depends on every bit of \( h( k ) \) and even the identity hash of the integers of std::hash spreads over the table; a hash that declares the member type is_avalanching (the convention of Boost) is taken as it is, on 64 bits (by its hash64() where it has one, which keeps them where a std::size_t has 32 [see detail::hash64()]), as the default one, DefaultHash< K > [see FastHash.h], is for the integers and the strings, whose hash by FastHash costs one product per 16 bytes. The top bits of the mixed hash give the first group of the key, its low 8 bits the fingerprint (1 in place of 0) and its low 3 bits the bit of the key in the bytes of overflow, as in Boost, which costs no shift. The number of groups is a power of 2 and the groups are inspected in the triangular order \( g_i = ( g_0 + i ( i + 1 ) / 2 ) \bmod G \), a permutation of the \( G \) groups.
An insertion that goes past a full group sets in its byte of overflow the bit of its key, one of 8. A search stops at the first group whose overflow lacks the bit of its key, after the places of that group whose fingerprint matches: a search for an absent key reads one group of 16 bytes, one line of cache, in the common case and never touches the pairs. remove() empties the place, with no deleted marks; but a place emptied in a group whose overflow is not 0 still counts as full, since the bits stay set until the next rebuild and the searches keep going past the group. A removal in the first group of its key writes the 16 control bytes of the group whole, at an address that depends only on the hash: the store of the single byte would have an address known only once the control bytes arrive from memory, and the loads of the next operations could not pass it before then, so that the removals of random keys would pay their misses one after the other instead of overlapping them (on a Xeon Gold 5120, 55 ns against 33 ns per removal from \( 10^6 \) keys). For the same reason a removal counts the places it may leave counted by a bound \( d \), one more per removal, with no load: when \( n + d \) reaches the bound of the full places, the places counted are counted exactly, in \( O( m / 15 ) \) time, and the table is rebuilt only if fewer than \( m / 16 \) places are left, so that the next count comes at least \( m / 16 \) operations later and costs \( O( 1 ) \) amortized. An insertion takes the first empty place of the inspection of its key. The full places and those still counted stay at most \( 7 / 8 \) of the places: past it the table is rebuilt, twice as large if the pairs exceed half of that bound, of the same size otherwise (only the bits of overflow and the places counted go away), which costs \( O( 1 ) \) amortized per insertion; the new pair is built in the new places before the old pairs move, so that a key or a value that lives in the table is still there to copy. When the hash of a key reads memory that the key points to, as for the strings, the rebuild prefetches the memory of each key when its walk of the places reaches it and moves the key 8 places later, from a ring on the stack, so that the misses of the keys overlap instead of coming one at a time. The rebuild only fills the new places, each group in the order of its places, so that it keeps the count of the full places of each group in a byte apart (on the stack up to 256 groups) and never reads the control bytes it has just written (a load of a group right after the store of one of its bytes waits for the store); a small table, rebuilt often as it grows, then calls the allocator only for its new places. So some place is always empty, and each inspection ends, since it visits each group at most once. find() runs the whole inspection in line, as Boost does: a call out of line would take the registers of the loop of the caller, and the compiler would no longer let a search for an absent key, which stops at the first group, fall through to the top of that loop (on a Xeon Gold 5120, 5.4 ns against 5.9 ns per absent key among \( 10^5 \)). The search of an insertion is the one of find() on the first group, which gives the place of a new key when it has an empty one, the common case; the groups after the first, and the rebuild, are out of line.
The iterators visit the pairs in the order of the places, as constant pairs of references \( ( \mathit{key} , \mathit{value} ) \), a group at a time by the mask of its full places [see const_iterator]; find() gives a value to change in place, and try_emplace() the value of a key found or inserted by one search, as that of std::unordered_map. A removal moves no other pair and keeps the iterators valid but the one on the removed pair (erase( it ) returns the next one), an insertion of a new key that rebuilds the table invalidates all of them (one that does not rebuild it keeps them, the new pair visited or not), and an insertion of a key already there replaces its value in place.
With ADS_FLATHASH_BOOST defined to 1 (and ADS_WITH_BOOST, which the option of CMake of the same name defines whenever the headers of Boost 1.83 or later are found, and the makefile as well), the pairs are those of boost::unordered_flat_map, the table that the one above follows; by default the code of ADS is kept, since on the benchmarks it is on a par with Boost (the ratio of the times a median of 0.93 on the dictionaries and of 1.03 on the hash maps). The interface and the contracts stay those above, with three differences: capacity() is the number of places of Boost, \( 15 \) times a power of 2, less 1 (one place is its sentinel), and Boost fills all of them up to 29 places; deleted() counts the places that Boost no longer offers to the insertions, the difference between its maximum load after a rebuild and its maximum load now, in \( O( 1 ) \) time; and the iterators are forward, those of Boost.
The places come from the allocator A, rebound to lines of ALIGN bytes, with the rules of the standard [see AllocatorAware.h]; with the default one, std::allocator, they come from operator new aligned to ALIGN, and with Boost the allocator is that of boost::unordered_flat_map.
The contracts [see Contract.h]: after insert() the key is in the table and after remove() it is not; the invariant is that each full place is reached by the inspection of its key, the bit of the key set in the overflow of each group before it, that the control bytes match the count of the pairs, and that the full places and those still counted stay at most \( 7 / 8 \) of the places. On a table of more than 1024 places the invariant is checked on the inspection of the key of the operation only, up to its first group whose overflow lacks the bit of the key. The operations are defined in FlatHashTable.tpp.