r/compsci • u/gilnett • Oct 18 '26
Replacing B-Trees with streaming linear models (PGM): cache locality and bounds in practice
Traditional B-Trees waste substantial CPU cycles on pointer chasing across node levels, causing avoidable cache misses on modern architectures. The PGM-index approach fixes this by modeling key distributions through streaming piecewise linear regression with strict +/- epsilon error guarantees.
Instead of dereferencing tree pointers, the position is predicted directly on the ALU (y = ax + b), leaving only a small local search window that fits entirely within L1 cache. The construction runs in a single O(N) streaming pass with zero external solvers.
I built a pure Rust implementation with zero dependencies to benchmark this against standard B-Trees and verify the theoretical bounds: https://github.com/gilnett/learned_index_lab
While traditional transactional engines stick to B-Trees for in-place page updates, learned models completely outclass them on read-heavy or immutable storage (like LSM SSTables and columnar stores) where pointer overhead is pure waste.
Has anyone here benchmarked or deployed learned data structures in production storage layers? How do you approach optimal epsilon tuning when key distributions are unpredictable?
28
u/CrackerJackKittyCat Oct 18 '26
As from the prior discussion, I think your emphasis on comparison against btrees is not entirely in good faith. Btrees are as they are to optimize for secondary storage and for online inserts, deletes, rebalancing, etc.. Operations which this PGM implementation does not support.
While the approach is interesting and educational, the emphasis vs btrees and lack of emphasis on this being a static-dataset-only index implementation seems off.
"Static dataset optimized data structure outperforms dynamic structure used statically, film at 11."
3
u/maj160 Oct 22 '26
I think it's neat, OP. I'd never heard about PGM or learned index structures before.
Some things I wonder about:
Is your test / benchmark data representative? It seems to me that, almost by definition, random data will be modelled well.. but real data often has multiple very weird skews, hotspots on single keys, etc. which your model might struggle with?
If your CDF is smooth then PGM will work well. You should benchmark cases when it isn't.
It seems that in the PGM paper they use a multi level hierarchy of models to find the right segment, while your code just uses a binary search on a flat array of segments. I expect their method scales better, especially with messier data.
Another way to look at it: You've said PGM is faster than binary search, so why not replace the binary search on segments with a smaller PGM indexing your segments, and a smaller one indexing that, and... Etc
It does feel kinda AI... If it's just Google Translate giving you an AI sounding voice, that's unfortunate. Perhaps providing the documentation in your own words in your native language might help? I don't know
One thing I don't like about AI generated documentation is that it never actually explains the structure of the code - It's always about the method or the benchmarks but there's no architecture guide to how the code is laid out or why particular decisions were made. I understand why a PGM is good in general, but what makes your code interesting?
Random thought - What if you used cubic splines instead of linear segments? There's definitely a tradeoff to be made there but I have no idea if it's a good one. Higher cost per segment but maybe fewer segments if they can fit the distribution better.
Thanks, I learned something new today.
1
u/SkiFire13 23d ago
I'm confused by how you consider Eytzinger to use time and memory for the "index" while binary search doesn't. Eytzinger can be performed in place, so you can reuse the same input slice without using additional memory. Moreover which flavour of Eytzinger have you used? It can be made N-ary instead of binary to improve cache usage.
1
u/Wieuvhio Oct 18 '26
practical, we swapped from b-trees to pgm at work last year.
6
u/[deleted] Oct 18 '26
[removed] — view removed comment