r/programming • u/gilnett • 1d ago
Learned Index Lab: Replacing B-Trees with streaming linear models in pure Rust (-99% RAM, 3x faster)
https://github.com/gilnett/learned_index_labInstead 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.
14
7
u/GreatCosmicMoustache 17h ago
cool idea, but does it require you to solve the big optimization problem every time you add a single row?
10
u/gilnett 16h ago
Nah, no solver or ML training at all. It's just a single O(N) streaming pass using basic slope math—takes ~3ms for 1M keys.
For inserts, you wouldn't mutate the sorted array in-place anyway. You either buffer writes in a memtable and build the index on immutable SSTables (LSM style), or you use gapped arrays where only the local segment splits on overflow.
7
u/Kwantuum 18h ago
Ai;dr.
-12
1
u/Sounlligen 15h ago
Maybe I'm a noob, but I'll ask anyway - is it something that can be used as a spatial representation? For e.g. 3D points? Because sometimes B-trees are used as containers.
6
u/gilnett 14h ago
Directly, no, because PGM models 1D sorted keys.
But you can map 3D points (x, y, z) into a single 1D scalar using a space-filling curve like a Z-order curve (Morton code) or Hilbert curve. Once your 3D points are sorted along that curve, you can run a learned index on top of it.
Researchers actually built multidimensional learned indexes using this approach (like Flood and Tsunami from MIT), and they surprisingly outperform R-Trees and KD-Trees on read-heavy spatial queries. For complex 3D bounding-box queries or nearest-neighbor searches though, dedicated spatial trees (Octree, R-Tree) are still more straightforward to work with.
1
u/a_marklar 14h ago
Sure just order your data in some space filling curve like a Morton or Hilbert curve and then use a B-tree over the 1-D index
0
u/Schnickatavick 14h ago
The link to the full paper in the readme seems to be broken. Is there another paper link anywhere?
2
0
-6
u/AutoModerator 1d ago
r/programming is not the place to post a project to get feedback, ask for help, or otherwise promote it.
Technical write-ups on what makes a project technically challenging, interesting, or educational are allowed and encouraged, but just a link to a GitHub page or a list of features is not allowed.
The technical write-up must be the focus of the post, not just a tickbox-checking exercise to get us to allow it. This is a technical subreddit.
We don't care what you built, we care how you build it.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
45
u/Keeyzar 20h ago edited 20h ago
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).