Skip to content

Benchmark the dense-array book against BTreeMap and fix complexity claims#126

Merged
weiminglong merged 1 commit into
mainfrom
feat/arraybook-benchmark
Jul 6, 2026
Merged

Benchmark the dense-array book against BTreeMap and fix complexity claims#126
weiminglong merged 1 commit into
mainfrom
feat/arraybook-benchmark

Conversation

@weiminglong

Copy link
Copy Markdown
Owner

Summary

ADR-0002 dismissed alternatives without measurement and repeated an incorrect "O(1) best bid/ask" claim in four places (BTreeMap first-element access is O(log n)). Worse, it never evaluated the design any order-book discussion on a bounded grid starts from: a dense array indexed by tick with a best-price cursor.

  • New array_book benchmark (pb-book): implements the dense-array side at full strength — epoch-stamped cells so a snapshot rebuild is O(levels) rather than an 80 KB clear — and runs it against L2Book on identical workloads (snapshot rebuild, resting-level delta, top-of-book read, top-10 depth, best-level churn).
  • Measured (Apple M3, 50 levels, 10-tick spacing): array wins writes 10–19× (59 ns vs 1.11 µs snapshot; 0.73 ns vs 7.7 ns delta; 3.8 ns vs 26 ns churn), ties top-of-book (2.6 vs 2.8 ns), loses top-10 iteration slightly (39 vs 31 ns).
  • ADR-0002 rewritten with the measured table and the explicit retention rationale: book operations are not the pipeline bottleneck (~1 µs worst case against ~0.5 µs deserialize + ~0.5 µs WAL per event), the array pins ~320 KB per asset and couples the book to the venue's tick grid, and near-resolution sparse books degrade its top-k scans. The crossover condition for switching is recorded, and the benchmark keeps the comparison re-runnable.
  • O(1) claims corrected in README, docs/architecture.md, and docs/latency.md to the measured ~3 ns figure.

Verification

  • Benchmark compiles and runs (cargo bench -p pb-book --bench array_book); results above from a full run
  • pb-book tests green, clippy (--all-targets -D warnings-clean), fmt clean, hygiene gate clean

…aims

ADR-0002 previously dismissed alternatives without measurement and
claimed O(1) best bid/ask — BTreeMap first-element access is O(log n),
and the obvious design for a bounded 10,001-tick price grid (a dense
array indexed by tick with a best-price cursor) was never evaluated. A
new array_book benchmark implements that alternative at full strength
(epoch-stamped cells so snapshot rebuilds cost O(levels), not an 80 KB
clear) and runs it against L2Book on identical workloads.

Measured on the committed run: the array wins the write path 10-19x
(59 ns vs 1.11 us snapshot rebuild, 0.73 ns vs 7.7 ns delta) and ties
top-of-book reads (~2.7 ns vs ~2.8 ns). ADR-0002 now records the table
and why BTreeMap is retained regardless — book operations are not the
pipeline bottleneck, the array pins ~320 KB per asset and couples the
book to the venue's tick grid, and sparse books degrade its top-k scans
— together with the crossover condition for switching. The incorrect
O(1) claims in the README, architecture overview, and latency doc now
cite the measured figure.
@weiminglong
weiminglong merged commit 1c5081b into main Jul 6, 2026
19 checks passed
@weiminglong
weiminglong deleted the feat/arraybook-benchmark branch July 6, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant