ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LearnedSort.h File Reference

The learned sort, beyond the book: learnedSort() distributes the numbers of 32 or 64 bits into buckets by a model of the cumulative distribution function (CDF) of their keys, learned from a sample, and sorts each bucket by vectorQuickSort() [see VectorQuickSort.h]; it falls back to sampleSort() [see SampleSort.h] when the sample shows a distribution that the model does not fit. More...

#include "ADS/Contract.h"
#include "ADS/sort/RadixSort.h"
#include "ADS/sort/SampleSort.h"
#include "ADS/sort/Sorted.h"
#include "ADS/sort/VectorQuickSort.h"
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <type_traits>
#include <vector>
#include "ADS/sort/impl/LearnedSort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::learnedSort (Store &&A)
 the learned sort of \( A \) in the natural order (the total order of IEEE 754 for the floating point numbers): the buckets of a model of the CDF of a sample, each sorted by vectorQuickSort(), and sampleSort() when the sample shows a distribution that the model does not fit; not stable
template<VectorKey T>
void ADS::learnedSort (std::vector< T > &A)
 learnedSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { learnedSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::learnedSort (R &&A, P &&... p)
 learnedSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses

Detailed description

The learned sort, beyond the book: learnedSort() distributes the numbers of 32 or 64 bits into buckets by a model of the cumulative distribution function (CDF) of their keys, learned from a sample, and sorts each bucket by vectorQuickSort() [see VectorQuickSort.h]; it falls back to sampleSort() [see SampleSort.h] when the sample shows a distribution that the model does not fit.

The parallel sorts of ParallelSort.h have the same model as the classifier of their distribution, in learnedSort( ex , A ) and learnedStableSort( ex , A ). The order is the natural one, the total order of IEEE 754 for the floating point numbers (\( -0 \) before \( +0 \), the NaN at the ends), as in sort() [see Sort.h], whose result learnedSort() gives bit for bit.

A distribution into \( k \) buckets is balanced when the bucket of \( x \) is \( \lfloor k F( x ) \rfloor \), \( F \) the CDF of the keys; the model replaces \( F \) by the empirical CDF \( F_s \) of a sample of \( s = 16 k \) keys at places drawn by a xorshift seeded by \( n \), which, were the places independent and uniform, the inequality of Dvoretzky, Kiefer and Wolfowitz would keep within \( c / \sqrt{s} \) of \( F \) with probability at least \( 1 - 2 e^{-2 c^2} \), whatever \( F \). The splitters \( t_1 \le \cdots \le t_{k-1} \) are the quantiles \( i / k \) of the sorted sample, and the bucket of \( x \) is the number of splitters less than \( x \), which samplesort finds by a search of \( \log_2 k \) steps. The model finds it instead by a grid of cells of equal width \( w \), a power of 2, on the range \( [ S_{\min} , S_{\max} ] \) of the sample \( S \): the cell of \( x \) is \( \lfloor ( x - S_{\min} ) / w \rfloor \), a shift, and each cell keeps the number of splitters before it, i.e., a histogram of the sample and its prefix sums, a piecewise constant model of the CDF. If every cell holds at most \( c_{\max} \) (kCellSplitters) splitters, the bucket of \( x \) is the count of its cell plus \( c_{\max} \) comparisons with no branch, \( O( 1 ) \) time instead of \( \log_2 k \) dependent loads, which is where the learned sorts of Kristo et al. and of Ferragina and Odorisio gain on samplesort (their models are linear splines and the recursive model index of the learned indexes; the grid is the simplest model with the same guarantee).

The model fails when the keys are not spread on the range of the sample, i.e., for the laws with a heavy tail (Zipf, Pareto) and for clusters far apart, where some cell of the finest grid allowed holds more than \( c_{\max} \) splitters, and when the sample repeats its quantiles, i.e., for few distinct keys. Both are seen on the sample before any key moves: with no grid of at most \( c_{\max} \) splitters per cell, or fewer than \( k / 2 \) distinct splitters, learnedSort() leaves \( A \) to sampleSort(), whose buckets of equal keys take care of the duplicates. A sample that repeats a splitter only in part gives each repeated splitter a bucket of the keys equal to it, which needs no sort (the buckets of equality of IPS4o, as in LearnedSort 2.0). The argument behind the model and its costs is on the theory page of the area.

Costs: \( O( k \log k ) \) for the sample and the model, \( O( n ) \) for the classification and the moves, and the sorts of the buckets, \( O( n \log( n / k ) ) \) in expectation when the model fits; the extra room is a buffer of \( n \) items and \( n \) bucket numbers of 16 bits.

sort() does not choose learnedSort(): on the measures of the benchmark vectorQuickSort(), which sort() uses for these keys when the vector registers are there, sorted the same inputs faster, since its passes in the registers cost less than the scattered moves of a distribution; nor do the parallel sorts take this classifier in place of their sample tree, which on the measures it did not beat beyond the noise. The learned sorts remain for the inputs where the model shines (keys of a smooth law, many buckets) and as the reference of the technique.

The contracts [see Contract.h]: the vector ends with the same items sorted, an audit. The functions are defined in LearnedSort.tpp.

See also
A. Kristo, K. Vaidya, U. Çetintemel, S. Misra, T. Kraska, The case for a learned sorting algorithm, Proceedings of SIGMOD 2020, 1001-1016, 2020, doi:10.1145/3318464.3389752
A. Kristo, K. Vaidya, T. Kraska, Defeating duplicates: a re-design of the LearnedSort algorithm, 2021, doi:10.48550/arXiv.2107.03290
P. Ferragina, M. Odorisio, Fast, robust, and learned distribution-based sorting, IEEE Access 13, 45198-45214, 2025, doi:10.1109/ACCESS.2025.3549626
I. Carvalho, Towards parallel learned sorting, 2022, doi:10.48550/arXiv.2208.06902
A. Sato, Y. Matsui, PCF learned sort: a learning augmented sort algorithm with \( O( n \log \log n ) \) expected complexity, 2024, doi:10.48550/arXiv.2405.07122
P. Massart, The tight constant in the Dvoretzky-Kiefer-Wolfowitz inequality, The Annals of Probability 18(3), 1269-1283, 1990, doi:10.1214/aop/1176990746
M. Axtmann, S. Witt, D. Ferizovic, P. Sanders, Engineering in-place (shared-memory) sorting algorithms, ACM Transactions on Parallel Computing 9(1), 1-62, 2022, doi:10.1145/3505286
J. Wassenberg, M. Blacher, J. Giesen, P. Sanders, Vectorized and performance-portable quicksort, Software: Practice and Experience 52(12), 2684-2699, 2022, doi:10.1002/spe.3142
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa