|
ADS
Algorithms and Data Structures
|
The engineered searches of a sorted array that does not change, beyond the book [see BinarySearch.h for the binary search of section 1.2]: the place of the first item not smaller than a key, the lower bound, in the fewest misses of the cache and with no branch that the processor may mispredict. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/memory/HugePageAllocator.h"#include "ADS/memory/Prefetch.h"#include "ADS/search/CountLess.h"#include <algorithm>#include <cstddef>#include <functional>#include <limits>#include <memory>#include <new>#include <type_traits>#include <vector>#include "ADS/search/impl/StaticSearch.tpp"Go to the source code of this file.
Classes | |
| class | ADS::SPlusTree< T, Less, Bytes > |
| the static B+-tree of Slotin on a sorted array, by the order less() More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| template<class T, class C = std::less<>> | |
| std::size_t | ADS::branchlessLowerBound (const std::vector< T > &A, const T &v, C less=C()) |
| the first place of an item not smaller than \( v \) in the sorted \( A \), \( n \) if none, by the binary search with no branch | |
The engineered searches of a sorted array that does not change, beyond the book [see BinarySearch.h for the binary search of section 1.2]: the place of the first item not smaller than a key, the lower bound, in the fewest misses of the cache and with no branch that the processor may mispredict.
\[ H = 1 + \Big\lceil \log_{B+1} \Big\lceil \frac{n}{B} \Big\rceil \Big\rceil \]
levels, and a search reads one line per level, \( H \) in all (5 for a million keys of 32 bits, against the 20 probes of the binary search, of which the last 4 share a line). In a node the search counts the keys smaller than the key looked for, which is the child to take: on the arithmetic keys under std::less the count runs on the whole line with no branch, the places past the last item holding the largest value of the type, as one or two comparisons of vectors [see CountLess.h]; on the others it is a scan of the keys in use. The space is\[ B \sum_\ell s_\ell \approx n \Big( 1 + \frac{1}{B+1} + \frac{1}{(B+1)^2} + \cdots \Big) = n \frac{B+1}{B} \]
items, on huge pages when they are at least 2 MB [see HugePageAllocator.h], so that a search misses the TLB at most once, and the building takes \( O( n ) \) time.Compared with the literature: the layout of Eytzinger (a heap order of the items) also reads one line for the last 4 levels of the binary search, and with the prefetch of the 16 descendants 4 levels down it hides the latency of the misses rather than avoiding them, at the price of the bandwidth; Khuong and Morin found it the fastest of the layouts of the binary search, and the B-tree layout, with a node per line, faster on the arrays that do not fit in the cache. The S+ tree removes from the B-tree of Khuong and Morin the comparison with the key at every level (the leaves are the array, and the internal keys only route), and counts instead of searching in a node; the FAST trees of Kim et al. lay out a binary tree in blocks of lines and pages and search a block with SIMD comparisons, and the comparative study of Bellomo et al. finds the B-trees with SIMD searches the fastest in a lookup among the classic, the compressed and the learned indexes [see PGMIndex.h] of integers.
The contracts [see Contract.h]: the array sorted, an audit; each result \( r \) is such that the items before \( r \) are smaller than the key and those from \( r \) on are not, a postcondition checked as an audit. The functions are defined in StaticSearch.tpp. The items of an SPlusTree are trivially copyable.