book: cache top-kDepthLevels Level* per side to cut publish_depth cost - #29
Merged
Conversation
The cache buys ~60 ns/event on the observability=true path, but its maintenance on Book::add and Book::remove_by_id cost roughly 9 ns/event on the matching loop, which surfaced as a +12% p99 latency regression against bench/baseline.json on the CI runner. Threading an observability flag through BookRegistry into Book lets the bench skip the cache update entirely, matching the existing MatchingEngine observability flag. Live demo (meridian-server) keeps the default true and the cache stays live for publish_depth.
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
Measurement-driven follow-up to v1.1. With
observability=true, the bench drops from 4.79 to 2.79 M evt/s on the desktop reference, a 142 ns/event tax. A per-piece bisection (toggle each side-effect, re-bench) attributes 90 ns of that toBook::publish_depth: an 8-stepstd::mapiteration over the bid and ask price maps on every accepted event, plus a ~400 byteDepthSnapshotcopy through the seqlock.This PR makes
Bookcarry a top-kDepthLevelsLevel*cache per side, updated byBook::addwhen a newLevelis emplaced and byBook::remove_by_id/Book::erase_empty_levelwhen aLevelis erased.Book::publish_depthwalks the cache (cache-warm objects the matching thread just touched) instead of the map. No wire format change; no thread-safety change; the existing depth seqlock still anchors reader correctness.Measured paired runs on the desktop reference (same seed, same 1M events, same 100k warmup):
observability=false(matching only)observability=true(depth + trades + histogram)Remaining observability overhead drops from 142 ns/event to 78 ns/event (about 45 percent reduction). p50 latency on the instrumented path drops from 288 to 230 ns. The
observability=falseregression is the cache maintenance cost on theBook::add/Book::remove_by_idhot path (linear scan plus shift, at mostkDepthLevelsoperations) and remains comfortably above the 4.10 M evt/s CI baseline floor.Cache correctness is exercised by seven new unit tests in
tests/unit/test_book_depth_cache.cpp, including a 2000-step random-sequence test that audits the cache after every mutation via a newBook::audit_depth_cache_for_test()accessor (walks the maps and asserts cached pointers match the firstkDepthLevelsentries of each side). All 221 C++ tests pass on release build; existing property invariants P11 and P12, the byte-for-byte Differential test against the Python reference, and the SeqlockDepth concurrency test all continue to pass against the newpublish_depthpath.Test plan
ctest --test-dir build-release221/221.bench/baseline.json: throughput -1.2%, p50 +9.4%, p99 +9.9%, p99.9 -41% (all within tolerance).docs/perf/budget.md.