r/compsci • • 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?

62 Upvotes

24 comments sorted by

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."

1

u/gilnett Oct 18 '26

Fair point on the dynamic vs. static tradeoff—comparing an immutable index to a mutable B-Tree naturally favors the static structure.

That is why I just released v0.2.0 with standard `slice::binary_search` as the primary baseline. A contiguous slice has 0 KB of metadata and zero heap indirection—the purest static baseline possible.

Interestingly, PGM is still 2.1x to 2.9x faster than a flat binary search (48–70 ns vs. 140–152 ns). The win comes down to cache lines: standard binary search makes ~20 jumps across 8 MB of DRAM, whereas PGM predicts the offset and restricts the search to a 32-element window (4 cache lines).

The target use-case isn't OLTP transactional tables, but immutable sorted runs like RocksDB SSTables or columnar blocks (DuckDB, ClickHouse) where data is flushed once and queried millions of times.

Appreciate the feedback

9

u/CrackerJackKittyCat Oct 18 '26

Title of this post: an unqualified -- "Replacing B-Trees ..."

No, this simply does not replace B-Trees.

2

u/gilnett Oct 18 '26

The title borrows the phrasing from the original literature (Kraska et al.), but the post body explicitly scoped this: transactional OLTP engines keep B-Trees for in-place updates, while learned models specifically target immutable or read-heavy layers (SSTables, columnar blocks) where tree pointer overhead is unnecessary.

1

u/SkiFire13 23d ago

The title borrows the phrasing from the original literature (Kraska et al.)

Just because someone made a mistake doesn't mean you should follow.

3

u/nightcracker Oct 18 '26

Compare to binary search in Eytzinger layout at the very least.

3

u/dividebyzero14 Oct 19 '26

What makes you think anyone on here wants to talk to your LLM?

1

u/Even-Wolverine-4269 Oct 20 '26

I do. so “anyone on here” is false. I learned something new from this post. Couldn’t care less if it was written with AI or not

2

u/Bahatur Oct 18 '26

As a matter of analysis this is true, but consider that as a matter of practice the swarms of programmers who just make everything a hashmap by reflex.

1

u/RowettaPoulter-90 26d ago

tbh that last line sums up about half of all database benchmarking posts these days

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.

2

u/gilnett Oct 22 '26

For the test data, I actually added skewed data (lognormal) in v0.3.0 to see how it would react. Surprisingly, it handled the messier data even better than uniform keys.

You are totally right about the multi-level tree. Right now I just put all the segments in a flat array and binary search them. I wanted to keep the first version simple to test the core idea, but building a real tree of models on top is definitely the next step when datasets get huge.

Splines sound like a good idea on paper, but a straight line y = ax + b is just a basic multiply and add for the CPU. Fitting curves on the fly while streaming data is also a lot more painful to implement than simple lines, and the main goal is just to get close enough so the remaining keys fit in CPU cache.

fair point on the tone and documentation. English is not my native language, so I probably leaned too much on formal phrasing and Google Translate, which makes it sound artificial. I also spent too much time talking about benchmarks instead of how the code is actually built, so I am writing a guide to explain the memory layout and how the pieces fit together

but anyway, nice fedback by the way I also added an Eytzinger search layout in v0.3.0 after someone mentioned it earlier in the thread

1

u/gilnett Oct 22 '26

Honestly at first I just wanted to hack on PGMs for fun and check if the math worked on my machine. Given the feedback on this post though, I decided to take it more seriously instead of just dropping it. That is why I added the Eytzinger layout and benchmarked skewed datasets in v0.3.0 to compare different ways to play with CPU cache.

What you said about multi-level segments and code architecture makes a lot of sense for my semi-roadmap. Making a real recursive tree for bigger data and writing doc on how the code and memory are actually laid out are pretty much the next two things I want to build.

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/gilnett 23d ago

GP. I kept a separate buffer only to preserve the sorted array for range queries, but you're completely right that for pure point lookups it can be done in-place with zero extra memory. As for the "flavor", it's just the standard binary layout with branchless search, not N-ary or SIMD yet. I'm definitely taking your notes into account, and they might make their way in later.

The project has actually moved forward quite a bit since this post 16 days ago. The focus has also shifted: less about claiming it replaces B-Trees, and more about honestly comparing different search structures (BTreeMap, Eytzinger, PGM, flat slices) on real-world data to see where each one actually works best and where it falls behind. Right now I'm mainly focusing on cleaning up and clarifying the documentation around those trade-offs.

1

u/Wieuvhio Oct 18 '26

practical, we swapped from b-trees to pgm at work last year.

1

u/gilnett Oct 18 '26

Interesting. If you don't mind sharing (without breaching any NDAs), what was the exact use case—in-memory index or SSTable blocks? And did you manage to support dynamic writes, or is it strictly read-only/immutable runs? Wondering how epsilon tuning and build overhead held up in prod.

6

u/[deleted] Oct 18 '26

[removed] — view removed comment

2

u/gilnett Oct 18 '26

Spot on regarding the cache miss flamegraphs. That splitting logic you described is actually how the streaming construction works here: tracking the slope bounds on the fly and cutting a new segment the moment the error breaches epsilon. Because of that, epsilon doesn't need to guess the distribution—I tune it purely based on hardware constraints, typically 16 or 32 keys so the final binary search fits neatly inside an L1 cache line. The distribution then naturally dictates the number of segments produced rather than the error bound itself.

1

u/RefrigeratorRare3527 Oct 21 '26

"Spot on" is another AI tell nowadays. Good job shitting up the web

1

u/gilnett Oct 21 '26

spot on is the exact translation of "plein dans le mille avec google trad no IA just google trad xd

2

u/RefrigeratorRare3527 Oct 21 '26

do you feel accomplished when you spam the internet with garbage?