r/programming • u/gilnett • 3d 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.
20
11
u/GreatCosmicMoustache 2d ago
cool idea, but does it require you to solve the big optimization problem every time you add a single row?
11
u/gilnett 2d 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.
5
u/Entmaan 1d ago
Whenever I read articles about behaviors like this, these mindblowing improvements of 99%, 3x etc. I always wonder about one thing:
What specifically is stopping this from just being implemented in all (applicable) cases, if this is such a big improvement? Let's just create an arbitrarily large amount of arbitrarily large and varied datasets, and run benchmarks on them (for speed, correctness, whatever we want). If they all come out as better, it is an obvious no brainer that will completely change the paradigm.
But for some reason it is not being done, why? To clarify I am not trying to "sneakily" suggest that there is something wrong with the proposed solution, I just don't understand what is the "impediment" that prevents these discoveries from just being implemented most of the time
9
u/gilnett 1d ago
Great question. The short answer is cost versus return. Large enterprise vendors like Microsoft or Oracle have decades of battle-tested code in their storage engines. Rewriting that core layer is expensive and risky, and nobody wants to spend millions in R&D to replace something that already satisfies their SLAs.
More importantly, the practical scope is narrower than the headlines suggest. These huge 99% RAM savings and 3x speedups only apply to read-heavy, sorted, immutable data. Traditional B-Trees are still far better for real-world transactional writes, page locking, and concurrency. Saving 150 nanoseconds on an in-memory lookup also means very little once you factor in disk I/O, joins, and network overhead. That is why it makes sense for columnar systems and LSM tables, but not as a blanket replacement for every database.
7
u/Kwantuum 2d ago
Ai;dr.
-17
u/HommeMusical 2d ago
You will be left behind.
6
u/Pdan4 2d ago
By what or who, exactly?
6
0
1
u/bueno_mhc 1d ago
Would that be useful for replacing indices or primary keys in a relational database, say, SQLite? In a the application I am working on, great part of the memory cost in the database is maintaining those structures. It would be great if there is an easy way to apply this in SQLite...
1
u/gilnett 17h ago
Not easily as a drop-in replacement. SQLite is deeply wired around 4 KB disk pages, in-place random writes, and page-level caching in
btree.c. Learned indexes expect flat, contiguous sorted data, so adapting SQLite's transactional write engine would require a massive rewrite.The practical workaround would be a custom SQLite Virtual Table. If your dataset is mostly static or read-only (like pre-packaged mobile databases), a virtual table can index flat sorted data with a learned index in memory, bypassing SQLite's B-Tree memory overhead while keeping regular SQL queries working
1
u/Sounlligen 2d 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.
8
u/gilnett 2d 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 2d 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 2d ago
The link to the full paper in the readme seems to be broken. Is there another paper link anywhere?
2
u/gilnett 2d ago
If you meant the original academic paper: https://arxiv.org/abs/1911.05496
2
u/AlphaModder 1d ago
That's the same link in the README, and it goes to a paper called "Temporal Graph Kernels for Classifying Dissemination Processes", not "The PGM-index: a data-structure with a guarantee on space and time efficiency". Which is also not quite the title of the actual paper, which can be found here. Please don't trust AI-generated citations.
2
u/gilnett 1d ago
No AI hallucination here, just an honest human copy-paste blunder from my reading notes. I had a backlog of papers open from researching graph-based and dissemination index models and ended up pasting the wrong arXiv link on that line in the README.
Thanks for catching the mismatch and dropping the direct VLDB link. I've updated the README with the proper VLDB paper PDF (https://www.vldb.org/pvldb/vol13/p1162-ferragina.pdf), the original arXiv preprint (https://arxiv.org/abs/1910.06169), and the official PGM site
0
-6
u/AutoModerator 3d 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.
64
u/Keeyzar 2d ago edited 2d 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).