Repository navigation
exchange: keep the quote-only update_state path inlinable - #31
Merged
Merged
Conversation
Merging the multi-asset upgrade (f5bc9e3..9ef9851) made `Exchange/update_state_quotes/1000000` 2.3x slower (1.71 ms -> 3.85 ms). Measured with perf on a 7950X: 20 -> 77 retired instructions and 2 -> 14 branches per update, while L1 misses stayed at 0.38 per update and branch misses at ~0.001. The regression was retire-bound, not memory-bound: the new stop / liquidation / cross-margin paths made LLVM outline `update_state` (664 B frame, six callee-saved pushes), so everything the base commit kept in registers became L1-hit reload traffic (11.5 -> 31.8 loads per update). Split the public `update_state` into a small inline fast path and an outlined `update_state_checked` that keeps the previous behavior verbatim. The fast path covers what can provably do nothing but refresh the market state and clear the event buffers: an update that cannot fill, no pending stops, isolated margin and a flat account (which cannot be liquidated or force-cancel anything). Everything else, including the maintenance-margin check of an open position, stays in the slow path. New tests in `src/tests/update_state.rs` pin both sides of that boundary; dropping the flat-account condition fails `quote_update_liquidates_an_open_isolated_position`. Also move the `stop_order_events` capacity restore out of the per-update path. It only matters for an exchange whose `Clone`/`Deserialize` dropped the spare capacity, so it now runs on the first stop event instead of comparing on every update. Criterion, `benches/update_state.rs`, one target dir per revision (base f5bc9e3 / parent 9ef9851 / this revision): `update_state_quotes/1000000`: 1.71 ms / 3.85 ms / 0.96 ms `update_state_trade/1000000`: 3.80 ms / 4.81 ms / 3.62 ms `SmartCandle/update_state` (parent -> this revision): 115 us -> 105 us. An out-of-tree harness that only replays flat-account quote updates measured base / parent / this revision at 1.00 / 4.23 / 1.15 ns per update (20 / 77 / 25 instructions, 5.8 / - / 7.0 cycles). There this revision stays about 15% behind the base, while Criterion shows it 1.8x ahead of it. The two harnesses disagree, so treat the Criterion numbers as the reproducible ones.
SummarySplits IssuesNone |
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.
Merging the multi-asset upgrade (f5bc9e3..9ef9851) made
Exchange/update_state_quotes/10000002.3x slower (1.71 ms -> 3.85 ms). Measured with perf on a 7950X: 20 -> 77 retired instructions and 2 -> 14 branches per update, while L1 misses stayed at 0.38 per update and branch misses at ~0.001. The regression was retire-bound, not memory-bound: the new stop / liquidation / cross-margin paths made LLVM outlineupdate_state(664 B frame, six callee-saved pushes), so everything the base commit kept in registers became L1-hit reload traffic (11.5 -> 31.8 loads per update).Split the public
update_stateinto a small inline fast path and an outlinedupdate_state_checkedthat keeps the previous behavior verbatim. The fast path covers what can provably do nothing but refresh the market state and clear the event buffers: an update that cannot fill, no pending stops, isolated margin and a flat account (which cannot be liquidated or force-cancel anything). Everything else, including the maintenance-margin check of an open position, stays in the slow path. New tests insrc/tests/update_state.rspin both sides of that boundary; dropping the flat-account condition failsquote_update_liquidates_an_open_isolated_position.Also move the
stop_order_eventscapacity restore out of the per-update path. It only matters for an exchange whoseClone/Deserializedropped the spare capacity, so it now runs on the first stop event instead of comparing on every update.Criterion,
benches/update_state.rs, one target dir per revision (base f5bc9e3 / parent 9ef9851 / this revision):update_state_quotes/1000000: 1.71 ms / 3.85 ms / 0.96 msupdate_state_trade/1000000: 3.80 ms / 4.81 ms / 3.62 msSmartCandle/update_state(parent -> this revision): 115 us -> 105 us.An out-of-tree harness that only replays flat-account quote updates measured base / parent / this revision at 1.00 / 4.23 / 1.15 ns per update (20 / 77 / 25 instructions, 5.8 / - / 7.0 cycles). There this revision stays about 15% behind the base, while Criterion shows it 1.8x ahead of it. The two harnesses disagree, so treat the Criterion numbers as the reproducible ones.