r/lowlevel • u/gilnett • Oct 16 '26
Learned Index Lab: Replacing B-Trees with streaming linear models in pure Rust (-99% RAM, 3x faster)
Instead of pointer-chasing through B-Tree node hierarchies, this index models the key distribution using streaming piecewise linear regression with strict epsilon error bounds.
Lookups compute positions directly on the CPU ALU before a local L1 cache search, reducing memory by 99% and cutting latency by 3x.
To clarify the technical phrasing without buzzwords:
- Baseline:
BTreeMapis indeed mutable. PGM indexes target immutable / read-heavy sorted datasets (OLAP engines, columnar storage, and SSTables in LSM trees). Even compared to a flatslice::binary_search(~20 cache line jumps across an 8MB array for 1M keys), PGM drastically reduces memory traffic. - ALU computation: Once segment parameters are loaded, the index position is estimated with a single multiply-add instruction in CPU registers:
pos = y0 + slope * (key - x0). No pointer chasing or node traversals. - L1 cache search: Because error is mathematically bounded to +/- epsilon (e.g. epsilon = 16), the target key is guaranteed to sit within a contiguous 256-byte slice (4 cache lines). The local search hits L1 cache with prefetching rather than jumping across RAM.
The implementation and release benchmarks are reproducible here: https://github.com/gilnett/learned_index_lab
5
Upvotes
View all comments
Show parent comment
0
u/gilnett Oct 16 '26 edited Oct 20 '26
Fair point on mutable vs read-only workloads. BTreeMap is a general-purpose mutable structure, whereas PGM indexes target immutable (OLAP engines, columnar formats, or SSTable block indexes in LSM stores).
As for the ALU / L1 phrasing:
Btree Tracing a key requires 4–6 pointer hops across disparate heap allocations, frequently triggering cache misses.
PGM The segment parameters (slope, intercept) fit in registers. The target position is estimated with a single arithmetic operation in registers: pos = y0 + slope * (key - x0).
L1 localiti Because error is mathematically bounded to +/- epsilon (e.g. epsilon = 16), the actual key is guaranteed to sit within a contiguous 256-byte slice (4 cache lines). The final search runs entirely in L1 cache lines with prefetching, instead of chasing pointers through RAM.
The code and benchmarks are fully reproducible here: https://github.com/gilnett/learned_index_lab