Repository navigation
stop_orders, exchange: keep pre-allocated buffer capacity through Clone - #32
Conversation
`Exchange`, its per-market stores inside `PortfolioExchange` and the event buffers derive `Clone`, but `Vec::clone` keeps only the length while the pre-allocated capacity is load-bearing: - `PendingStops` admission uses the capacity as the configured bound, so a cloned exchange or market rejected every stop with `StopOrderError::MaxPendingStops(0)`. - The event and scratch buffers are pushed into with `push_within_capacity`, so the first fill, forced cancellation or stop event after a clone panicked. `PendingStops` now has a `Clone` that retains its capacity, the same pattern `SortedOrders` already uses, which fixes both `Exchange` and the portfolio's per-market stores. `Exchange` gets a manual `Clone` that restores its event and scratch buffers, sharing the capacity formulas with `new` through `BufferCapacities` so the two cannot drift apart. Clone is cold, so the push paths stay free of capacity checks. Tests: a cloned exchange accepts new stops, reports a triggered stop and a fill, and a cloned portfolio accepts new stops. All four fail without this change. Serializing and deserializing still drops the capacity (serde has no hook to restore it), the same pre-existing gap as `SortedOrders`.
The previous revision fixed `Exchange` and `PendingStops`, but `PortfolioExchange` still derived `Clone`. `Vec::clone` keeps only the length, so a cloned portfolio panicked in `push_within_capacity` on its first fill (`order_events`), stop election (`elected_stops_scratch`) or forced cancel (`forced_cancel_scratch`). Only `stop_events` was restored, lazily in `reset_stop_events`. `PortfolioExchange` now has a manual `Clone` that restores all four buffers through `reserve_preallocated_capacities`. Their sizes come from a new `BufferCapacities` shared by `new`, `add_market` and `clone`, so the bounds cannot drift apart. `add_market` grows to the absolute total instead of calling a relative `Vec::reserve`: the buffers are empty between calls, so `reserve(add)` only guaranteed `add`, not the old total plus `add`. `reserve_to_capacity` moves to `utils` so both exchanges share it. The `reset_stop_events` / `restore_stop_order_event_capacity` comments now say that only a deserialized exchange reaches the lazy path. Tests: a cloned portfolio (with a market added through `add_market`) reports a fill, a triggered stop and a forced cancel of a reduce-only order. All three panic without the new `Clone`.
SummaryThe PR replaces derived IssuesNone |
…oExchange Both exchanges sized their event and scratch buffers from the same input, the number of order slots (`max_num_open_orders`, or its sum over all markets), but each had its own private `BufferCapacities` with different formulas. A new event source would have needed the bound fixed in two places. `execution_rules::BufferCapacities::for_order_slots(n)` is now the only source: 4n order events, 2n forced cancels, n elected stops and n stop events. The field docs give the reason for each bound. `Exchange::new`/`clone` pass `max_num_open_orders`, `PortfolioExchange::new`/`add_market`/`clone` pass the summed slots. Portfolio sizes that change: - The `.max(16)` floor is gone. Nothing depended on it, and an empty portfolio now allocates nothing. - `elected_stops_scratch` shrinks from 2n to n. Elections are evaluated per market, so a single market's pending stops already bound it. This only changes memory use, not behavior. The push paths use `push_within_capacity`, so a bound that is too small would panic in the existing tests rather than allocate. Also drops the wrong "two forced cancellations per resting order" comment: each order is cancelled at most once. The 2n comes from bids and asks. Tests: unit test for the bounds, including 0 slots and the largest valid input, and a should_panic test for one slot more than any config allows.
SummaryThe PR centralizes the event/scratch buffer bounds in a new IssuesNone |
Exchange, its per-market stores insidePortfolioExchangeand the event buffersderive
Clone, butVec::clonekeeps only the length while the pre-allocatedcapacity is load-bearing:
PendingStopsadmission uses the capacity as the configured bound, so a clonedexchange or market rejected every stop with
StopOrderError::MaxPendingStops(0).The event and scratch buffers are pushed into with
push_within_capacity, so thefirst fill, forced cancellation or stop event after a clone panicked.
PendingStopsnow has aClonethat retains its capacity, the same patternSortedOrdersalready uses, which fixes bothExchangeand the portfolio'sper-market stores.
Exchangegets a manualClonethat restores its event andscratch buffers, sharing the capacity formulas with
newthroughBufferCapacitiesso the two cannot drift apart. Clone is cold, so the push pathsstay free of capacity checks.
Tests: a cloned exchange accepts new stops, reports a triggered stop and a fill, and a
cloned portfolio accepts new stops. All four fail without this change.
Serializing and deserializing still drops the capacity (serde has no hook to restore
it), the same pre-existing gap as
SortedOrders.