Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Learned Index: Piecewise Geometric Model (PGM-Index) in Rust (v0.2.0 Archive)

CI Crates.io Docs.rs License: Apache-2.0 Ko-fi GitHub Sponsors

Note

Historical Archive: This document preserves the state of the documentation for v0.2.0 of learned-index, which introduced comparison against Flat slice::binary_search, lower_bound, and range scans.


A zero-dependency, production-grade implementation of Piecewise Geometric Model (PGM) Learned Index Structures in pure Rust.

This repository demonstrates the paradigm shift away from traditional comparison-based pointer hierarchies (such as B-Trees and LSM block indexes) to mathematical function approximations of Empirical Cumulative Distribution Functions (CDFs) with provable $\epsilon$ error bounds.

Note

Academic Attribution & Acknowledgments: I am not the original creator of Learned Indexes or the Piecewise Geometric Model (PGM-index).

This repository (learned-index) is an independent, clean-room educational and production-oriented implementation in pure safe Rust, built to explore, benchmark, and evaluate learned indexing mechanics within the modern systems ecosystem.


Architectural Comparison & Hardware Dynamics

Starting with v0.2.0, learned-index evaluates PGM against both common baseline approaches:

  1. std::collections::BTreeMap: The standard library mutable, dynamic pointer-based search tree.
  2. Flat slice::binary_search: The standard library immutable baseline (contiguous array binary search).
DimensionStandard BTreeMapFlat slice::binary_searchPGM-Index (Learned)Microarchitectural Advantage
Index StructureDynamic nodes across heapContiguous raw bufferCompact array of $(a, b, k_0)$ segmentsMetadata fits entirely inside L1/L2 cache
Lookup Mechanism4–6 node hops across RAM$\sim 20$ binary jumps across 8 MB1 in-register math eval + local 4 cache linesEliminates DRAM stalls & cache pollution
Index Metadata$\sim 45\text{ MB}$ per $10^6$ keys$0\text{ KB}$ (data must stay resident)3.1 KB – 48 KB per $10^6$ keys-99.9% index memory vs B-Tree
Lookup Latency$220 - 290\text{ ns}$$140 - 160\text{ ns}$48 - 70 ns3x faster vs B-Tree, ~2.2x vs Slice
Construction Time$\sim 70\text{ ms}$ ($O(N \log N)$)$0\text{ ms}$ (pre-sorted input)$3.5\text{ ms}$ ($O(N)$ streaming)~18x faster construction than B-Tree

Statistical Benchmark Suite (10-Run Rigorous Analysis)

All benchmarks were measured locally on x86_64 hardware using Rust in --release mode. Each scenario was executed over 10 consecutive runs with distinct randomized seeds to eliminate CPU scheduling noise and warm-cache anomalies.

  • Dataset Size: 1,000,000 keys (64-bit integers)
  • Queries per Run: 100,000 queries (50% existing keys / hits, 50% non-existent keys / misses)

1. Dense Sequential Time-Series Data

Distribution: Dense Timestamp | Tolerance: epsilon = 32
MetricBTreeMapFlat slice::binary_searchPGM-Index (Learned)Improvement Factor
Mean Build Time67.94 ms0.00 ms (pre-sorted)3.65 ms18.6x faster than BTree
Index-Only Memory45.78 MB0 KB3.1 KB14,700x compression
Mean Hit Latency221.8 ns/op152.7 ns/op63.5 ns/op3.50x vs BTree / 2.41x vs Slice
Mean Miss Latency10.0 ns/op23.7 ns/op0.7 ns/op14.3x faster negative rejection
Segments CountN/AN/A83 segments12,048x factor
Verification100.0% match100.0% match100.0% match0 discrepancies across 10 runs

2. Uniform Sparse Random Keys

Distribution: Uniform Sparse Random | Tolerance: epsilon = 16
MetricBTreeMapFlat slice::binary_searchPGM-Index (Learned)Improvement Factor
Mean Build Time68.79 ms0.00 ms (pre-sorted)3.82 ms18.0x faster than BTree
Index-Only Memory45.78 MB0 KB48.1 KB-99.89% memory reduction
Mean Hit Latency223.0 ns/op150.9 ns/op70.5 ns/op3.16x vs BTree / 2.14x vs Slice
Mean Miss Latency214.6 ns/op104.0 ns/op68.1 ns/op3.15x vs BTree / 1.53x vs Slice
Segments CountN/AN/A1,406 segments711x compression
Verification100.0% match100.0% match100.0% match0 discrepancies across 10 runs

3. Clustered Heavy-Tailed Lognormal Distribution

Distribution: Lognormal Clustered | Tolerance: epsilon = 16
MetricBTreeMapFlat slice::binary_searchPGM-Index (Learned)Improvement Factor
Mean Build Time69.75 ms0.00 ms (pre-sorted)3.90 ms17.9x faster than BTree
Index-Only Memory45.78 MB0 KB12.1 KB-99.97% memory reduction
Mean Hit Latency216.4 ns/op141.0 ns/op48.6 ns/op4.45x vs BTree / 2.90x vs Slice
Mean Miss Latency61.4 ns/op39.8 ns/op38.2 ns/op1.61x vs BTree / 1.04x vs Slice
Segments CountN/AN/A455 segments2,197x compression
Verification100.0% match100.0% match100.0% match0 discrepancies across 10 runs