Stable radix ordering with O(√n) auxiliary memory.
One linear-time engine for stable sorting, argsort and single-rank selection, safe for records that own resources.
Note
0.2 adds cached-key sorting, argsort, selection and more key types, and renames the 0.1 sorts; see versions to migrate. 0.x minor releases may change the API; see the roadmap.
An LSD radix sort is stable and runs in linear time, but it needs a second buffer as large as the input. Comparison sorts avoid that buffer and pay O(n log n) comparisons instead. Tessera keeps the radix sort's linear time and stability and bounds the extra memory to O(√n).
The same engine sorts records, sorts index payloads for argsort, and can skip further work on bands that do not contain the rank requested by select_nth. Cached-key sorting can reuse the engine's stable distribution when the keys differ only in a narrow bit window.
- Stable, also for descending keys.
- O(√n) sorting scratch, through a stable in-place block partition and a bounded tile buffer. Cached keys and argsort's index output use O(n) space; parallel sorting uses O(P√n) scratch for P participating threads.
- Adaptive. Sorted, reverse-sorted, nearly sorted and heavily repeated inputs take shortcuts, and constant key bits are skipped.
- Any record type. No
Copy,Clone,DefaultorOrdneeded. Records are moved, never duplicated, even if the key function misbehaves or panics. - Fixed-width keys. Supports fixed-width keys encoded as unsigned integers; see key encoding for supported types and limits.
- One portable backend. Uses
fearless_simdfor vector scans and prefix sums, selecting supported instructions automatically. Requiresstdand Rust 1.89; Rayon is optional.
[dependencies] tessera-order = "0.2"
SIMD is always enabled; there is no portable-simd or std feature. Disabling default features does not change the backend or remove the std requirement.
use std::cmp::Reverse; let mut values: Vec<u64> = vec![42, 7, 19, 7, 3]; tessera_order::sort(&mut values); assert_eq!(values, [3, 7, 7, 19, 42]); // Sort records by a key; equal keys keep their input order. let mut rows = vec![(2u32, "first"), (1, "middle"), (2, "last")]; tessera_order::sort_by_key(&mut rows, |row| Reverse(row.0)); assert_eq!(rows, [(2, "first"), (2, "last"), (1, "middle")]);
0.1 called the two sorts stable_sort and stable_sort_by_key; 0.2 keeps them as deprecated aliases. Every operation resolves equal keys as a full stable sort would.
| API | Use |
|---|---|
sort(&mut [K]) |
Sort keys themselves |
sort_by_key(&mut [T], F) |
Sort any records by a key computed by F |
sort_by_cached_key(&mut [T], F) |
Sort by a key computed once per record on nontrivial inputs |
select_nth / _by_key |
Place one ranked record and partition around it |
argsort / _by_key |
Return sorted indices without moving source records |
See the guide for examples, multi-field sorting for tuple keys, and versions for what 0.1 names differently and how to migrate from earlier development snapshots.
Tessera does not take a comparator: an arbitrary Ordering cannot be turned into radix digits.
The optional rayon feature adds par_sort and par_sort_by_key (0.1: par_stable_sort and par_stable_sort_by_key), plus par_argsort and par_argsort_by_key. Large inputs partition in the caller's Rayon pool through the shared root engine, while small inputs and one-thread pools use the serial engine. See parallel usage and the parallel contract for record and key-function bounds.
Track Rust benchmark trends on the public Bencher dashboard. These continuous measurements flag changes to investigate; use the benchmark protocol for paired implementation comparisons and source-change attribution.
The implementation comparison ranks Tessera among other sorts by speed and extra memory. The preview below tracks Tessera alone on main.
Main trend: the geometric mean of each mode's run-time change since the first run on the current runner specification; lower is faster. The interactive page filters cases by type, distribution, length and mode, compares cases over any time range and lists absolute times.
- Orders defined by a comparator, strings, or other variable-length keys: use
slice::sort_by. - Parallel sorting without the optional
rayonfeature: Tessera is single-threaded. - No allocator: Tessera needs O(√n) scratch memory, so it cannot run without
alloc(or, in C++, without a heap).
Each language package is versioned and released separately (RELEASING.md).
| Capability | Rust 0.2 | C++ |
|---|---|---|
| Stable sort | Implemented | Planned |
| Unstable sort | Not in 0.2 | Planned |
| Cached-key sort, argsort and selection | Implemented | Not planned |
| 128-bit keys | Signed and unsigned | Planned |
Records that own resources or hold &mut |
Supported | Trivially copyable records only (planned) |
A Python binding is planned as well; see the roadmap.
| Topic | Document |
|---|---|
| Ordering, key functions, floats, panics and memory bounds | conformance/semantics.md |
| How the algorithm works, with interactive figures | Algorithm explanation |
| How the engine works | docs/design/engine.md |
| Why user key functions cache bucket IDs | Record handling |
| All documentation | Documentation site · Source index |
Bug reports, benchmark results from other machines, and integration reports are welcome, and so are AI-assisted contributions, as long as a person has reviewed the change and takes responsibility for it. Please read CONTRIBUTING.md first. Commits are signed off under the Developer Certificate of Origin, and everyone in the project follows the code of conduct. Report security issues privately as described in SECURITY.md.
The stable block partition is adapted from Robert Clausecker's radixsort_permuted.c, described in Parallel O(√n) Overhead LSD Radix Sort by Robert Clausecker and Florian Schintke. The paper also suggests the hybrid Tessera builds on: splitting on the most significant digit first, then sorting each bucket's remaining digits least significant first.
Tessera was developed and measured with Lukas Bergdoll's sort-research-rs benchmark harness.
Tessera is licensed under the BSD 2-Clause License. This covers the code adapted from radixsort_permuted.c, which is distributed under the same license.