Benchmark the dense-array book against BTreeMap and fix complexity claims#126
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
array_bookbenchmark (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 againstL2Bookon identical workloads (snapshot rebuild, resting-level delta, top-of-book read, top-10 depth, best-level churn).Verification
cargo bench -p pb-book --bench array_book); results above from a full run--all-targets -D warnings-clean), fmt clean, hygiene gate clean