Skip to content

stop_orders, exchange: keep pre-allocated buffer capacity through Clone - #32

Merged
MathisWellmann merged 3 commits into
mainfrom
clone
Nov 25, 2026
Merged

MathisWellmann merged 3 commits into
mainfrom
clone

Conversation

@MathisWellmann

Copy link
Copy Markdown
Owner
  • 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.

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

Copy link
Copy Markdown

Summary

The PR replaces derived Clone on Exchange, PortfolioExchange and PendingStops with manual impls that restore the pre-allocated event/scratch-buffer capacities (via new shared BufferCapacities bounds and utils::reserve_to_capacity), plus regression tests. I verified the capacity arithmetic matches the constructor values exactly, the struct literals in both manual Clone impls cover every field, reserve_to_capacity's bounds and asserts are sound, and all new test helpers exist in the test tree.

Issues

None

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

Copy link
Copy Markdown

Summary

The PR centralizes the event/scratch buffer bounds in a new BufferCapacities type and replaces derived Clone on Exchange, PortfolioExchange and PendingStops with manual impls that restore the pre-allocated capacities Vec::clone drops, fixing push_within_capacity panics after cloning. I verified the manual struct literals against the actual field lists, the capacity formulas against the original new/add_market bounds, and the reserve_to_capacity/PendingStops::clone logic; both add_market and clone now size all four buffers against the summed slot total.

Issues

None

@MathisWellmann
MathisWellmann merged commit e6b3aa4 into main Nov 25, 2026
2 checks passed
@MathisWellmann
MathisWellmann deleted the clone branch November 25, 2026 20:11
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