Skip to content

exchange: keep the quote-only update_state path inlinable - #31

Merged
MathisWellmann merged 1 commit into
mainfrom
perf
Nov 25, 2026
Merged

MathisWellmann merged 1 commit into
mainfrom
perf

Conversation

@MathisWellmann

Copy link
Copy Markdown
Owner

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.

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.
@github-actions

Copy link
Copy Markdown

Summary

Splits Exchange::update_state into an inlined quote-only fast path (flat, isolated-margin account, no pending stops, CAN_FILL_LIMIT_ORDERS == false) and an #[inline(never)] full path, moves stop_order_events capacity restoration lazily into push_stop_event, and adds boundary tests. The fast path is behavior-preserving: I verified the isolated check_maintenance_margin returns Ok(()) for a zero position (risk_engine/isolated_margin.rs:157) and fill_active_orders already no-ops when !U::CAN_FILL_LIMIT_ORDERS (exchange.rs:1480), so the only skipped work is provably event-free; the lazy capacity restore correctly triggers only on len == capacity and always yields capacity ≥ max_num_open_orders before push_within_capacity.

Issues

None

@MathisWellmann
MathisWellmann merged commit d233dbd into main Nov 25, 2026
2 checks passed
@MathisWellmann
MathisWellmann deleted the perf branch November 25, 2026 19:07
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