index & optimize advanced swap queue execution

Profiling FinalizeBlock for CosmWasm execution found computational complexity in the advanced swap queue that could increase end block processing time.

image

Under more normal operation, about 25% of the FinalizeBlock spikes above baseline come from the adv swap queue, so it is also worth optimising for normal running.

image

This MR adds indexes and cuts complexity and computation, as listed in the table below. As pool count grows, it may later be worth chunking the execution.

With these optimisations, the pair matcher's original 41-pool/300-success case took 1.81 seconds locally. The indexed implementation takes about 2.9 ms, including building the index. That is roughly a 620× reduction for this component. CPU profiling attributed 78.3% of the old runtime to strings.ToUpper under repeated Asset.Equals calls.

Path Before After
Reverse-pair discovery after successful swaps O(S·P³), potentially quartic if S∝P O(P² + S·P) (per-asset position index plus set-based dedupe; falls back to the reference scan for non-canonical assets)
Pair list construction Rebuilt O(P²) cross-product every rapid iteration Pools are still re-fetched every iteration, but the pair list, pair index and active-limit-pair set are rebuilt only on iteration 0 or when the set of available pools changes (for example, a derived-asset swap reactivating its suspended virtual pool mid-EndBlock). Otherwise only pool balances are refreshed
Initial limit-pair probing 1,722 empty iterator probes live 40 populated pairs live; adversarial scan bounded at P(P+1), then falls back to pair probing
Failed handler executions Could bypass MaxSwapsPerBlock; up to B+(R−1)C At most R·B, currently 300
Rapid-iteration direction skips Protobuf clone before the skip check Checked before cloning; skipped items are never marshalled
Halt/Mimir reads Repeated per candidate and inside handlers One read per unique key per EndBlock, using a context-scoped snapshot cache. Any Mimir write or delete during EndBlock (e.g. the MaxRuneSupply circuit breaker) turns the cache off for the rest of the block
Memo parsing Up to three parses per executed swap One parse per unique memo per EndBlock (validate, handler, settlement)
State persistence Re-scanned the shared index per update Direct item-state write (SetAdvSwapQueueItemState). On mocknet builds (including unit tests) it panics if the item is no longer indexed under its current key; on mainnet it skips that check
Settlement Duplicate index removal plus item re-fetch One removal using the loaded message (RemoveAdvSwapQueueItemMsg)
Virtual depth scaling Two big-int share calculations per score Skipped when the multiplier is 10,000 (identity)
Telemetry Pair cross-product plus queue reads One queue scan

The new benchmarks and tests cover:

  • equivalence between indexed and reference pair matching, including the non-canonical asset fallback
  • building the active-limit-pair set, and FetchQueue probing only populated limit pairs
  • the budget bound for failed executions
  • a limit swap on a derived pool that a market swap reactivates mid-EndBlock still filling in the next rapid iteration of the same block (TestRapidSwapRefreshesPairsWhenDerivedPoolReactivates)
  • snapshot and write-disable behaviour of the scoped Mimir cache, including writes inside discarded child cache contexts
  • a Mimir halt written mid-EndBlock stopping later swaps in the same block
  • SetAdvSwapQueueItemState keeping the queue index intact and panicking on mocknet when an index-key field changes, and RemoveAdvSwapQueueItemMsg removing both the item and its index entry
  • a benchmark for limit-swap TTL insertion when many orders share one expiry height (measurement only; the TTL bucket code is unchanged)

Release notes

  • Advanced swap queue: failed swap executions on rapid-swap iterations 1+ now count toward MaxSwapsPerBlock, the same as iteration 0 failures and all successes. Before, they did not count, so a block could run more handler executions than the cap. Failed swaps still do not advance streaming swap state. Per-block swap counts will change: when a rapid iteration hits failing market swaps, fewer lower-priority swaps run in that block and wait for later blocks. Covered by TestRapidSwapFailedMarketConsumesExecutionBudget and the rapid-swaps-failed-market-budget regression suite.
Edited by Huginn Ai

Merge request reports

Loading
Loading