r/programming • • Oct 16 '26

Learned Index Lab: Replacing B-Trees with streaming linear models in pure Rust (-99% RAM, 3x faster)

https://github.com/gilnett/learned_index_lab

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.

191 Upvotes

49 comments sorted by

View all comments

67

u/Keeyzar Oct 16 '26 edited Oct 16 '26

If you're as dumb as I am: this could replace underlying logic in e.g. an index of a database. Faster lookup, less space used compared to e.g. a BTree. Though there's more to it, e.g. concurrency, locking... 

(that's what I understood, but as I stated, am dumb, probably false). 

25

u/CrackerJackKittyCat Oct 16 '26

Am dumb too. Seems to do so through replacing btree node pointer hops with a formula application to calculate an offset, then linear search.

One thing unclear to me is if the data set indexed must be static and pre-known or not.

24

u/gilnett Oct 16 '26

Yes, this implementation is static. The formula predicts an offset with a strict +/- epsilon error bound, so the final search is tiny and fits entirely in L1 cache.

For updates, you either buffer writes or rebuild segments. Because building is linear (O(N), about 3ms for 1M keys), rebuilding is very cheap.

Why traditional DBs have not switched: B-Trees are still better for frequent random in-place writes, row locks, and transactional OLTP workloads (like Postgres or MySQL).

Where this shines: Immutable, read-heavy data like LSM SSTables (RocksDB) or columnar formats (DuckDB, Parquet) where you want zero pointer-chasing and massive RAM savings.

3

u/VeeFu Oct 16 '26

Immutable, read-heavy data like...

Like earlier respondants I am also dumb, but wonder if it is best suited to LDAP databases, as that is their typical use-case. Any insight on that?

1

u/brunogadaleta Oct 16 '26

LDAP probably fits entirely in memory. We are talking about huge datasets (read bigger than ram).

4

u/gilnett Oct 16 '26

Even when data fits in RAM, B-Tree pointer hopping causes constant CPU cache misses. The main benefit here is replacing unpredictable pointer lookups with a fast mathematical calculation (y = ax + b) that keeps the index tiny enough to stay in CPU cache

10

u/Keeyzar Oct 16 '26

There are dynamic versions in the wild. This here is static. But rebuilding is fast.

I think most big database etc would've switched to it, as this is nothing new per se.(if it would benefit them a lot.)

1

u/daidoji70 Oct 16 '26

I think the data can be dynamic but the generating function of the distribution must be stable (or else the regression function will decay quickly). That being said, large static datasets will clearly benefit.

2

u/gilnett Oct 16 '26

That applies to neural net models. Here it is deterministic math with strict epsilon bounds: when the distribution changes, it just closes the current segment and starts a new line

1

u/daidoji70 Oct 16 '26

??

2

u/gilnett Oct 16 '26

To clarify: it is a piecewise linear model, not a single global regression across the whole dataset. If a new range of keys has a completely different slope or distribution, it just gets its own local segment. The error bound remains guaranteed everywhere regardless of how the distribution shifts

0

u/gilnett Oct 16 '26

you probably seens my message before edit wich wasn't addressed to you sorry

1

u/brunogadaleta Oct 16 '26

Static. learned index is useful for immutable datasets. Each time you add a key, you must recompute the totality of the index AFAIK.

2

u/gilnett Oct 16 '26

You are right that learned indexes shine brightest on immutable, read-heavy data. B-Trees are still better for frequent random in-place updates.

That said, no system reindexes from scratch on every insert. In practice, you either buffer writes in a memtable and index immutable blocks (LSM-style), or use dynamic variants where inserts are local and only split the affected segment, just like a B-Tree leaf split