perf(thorchain): swap queue EndBlock reads the TOR anchors once

🔎 Visual review of 0db990d3: explainer, review flags and prompts to ask an AI about the change

Summary

The advanced swap queue's EndBlock reads the TOR anchor list (GetAnchors(TOR)) several times for every swap: in isStableToStable (scoring and Swap) and in DollarsPerRune (fees). Every call scans and decodes the whole pool store and reads one TorAnchor-* Mimir per pool. The list depends only on which pool keys exist and on those Mimir values, and a swap changes neither.

This adds keeper.AnchorsCache, a read-through snapshot scoped to the context, the same pattern as MimirCache and ActiveValidatorsCache. SwapQueueAdv.EndBlock attaches it, and only outside simulations.

Risk: high (keeper and EndBlock). Behavior and state are unchanged; see Evidence.

What changes

  • x/thorchain/keeper/anchors_cache.go (new): the cache. It disables itself for good on any Mimir write, any pool removal, a write to a pool key the snapshot did not see, and any pool write before the first fill. The last rule exists because a fill inside a cache context that is later discarded could otherwise snapshot an uncommitted pool. Get and Set copy the anchor list, so callers can't change the snapshot. Set takes ownership of the pool-key map; its only caller, GetAnchors, builds a fresh map on every call.
  • keeper/v1/keeper_anchors.go: GetAnchors(TOR) serves from and fills the cache when the context has one. It doesn't fill when a Mimir read errors. Non-TOR assets never use it.
  • keeper/v1/keeper_pool.go, keeper_mimir.go: SetPool, RemovePool, SetMimir and DeleteMimir notify the cache.
  • manager_adv_swap_queue_current.go: attach the cache when !isSimulation, and fill it before the first swap (see Update below).

Consensus and gas

  • The cache is attached only in the real EndBlock, where store reads are not gas.
  • simulate() (API quotes, and the wasm QuoteSwap that runs inside a tx, where store reads are gas) never gets one.
  • The only callers of SwapQueueAdv.EndBlock are module.go and querier.simulate, which sets CtxSimulationMode.
  • The only pool and Mimir writers reached from the queue go through the four hooked keeper methods.

Evidence

A/B against develop with scripts/perf/ab.sh, 10 interleaved rounds, confirmed by an independent pass:

Benchmark sec/op allocs/op
PerfEndBlock/streaming-12 −19.3% (p=0.000), confirm −18.2% −20.8%
PerfEndBlock/idle ~ ~
PerfQuoteSwap/* (incl. wasm) ~ ~
PerfDepositStreamingSwap ~ ~
  • Tx gas/op and wasm quote gas/op are unchanged on every case.
  • scripts/perf/guard.sh: PASS. All 2,481 regression output files match develop, including state, events and gas_used.

Tests and coverage

  • keeper/v1/keeper_anchors_cache_test.go:
    • snapshot hit and copy semantics
    • an existing-pool write keeps the cache
    • a new pool, an anchor Mimir, a Mimir delete or a pool removal disables it
    • a pool write before fill disables it
    • a new pool in a discarded cache context doesn't leak into the parent
  • Mutation-checked: removing the pre-fill rule, the pool-write rule or either copy makes a test fail.
  • Changed-line coverage: keeper 33/33, keeper/v1 25/28. The two manager_adv_swap_queue_current.go lines run under the x/thorchain suites.
  • There's no new regression suite: this is a pure speedup, and the guard shows every existing suite's output is byte-identical.

Update: fill the cache before the first swap (051b2370)

A mainnet replay showed the cache serving no reads. A pool write before the first fill disables it, which is the rule that protects against discarded cache contexts. And only double swaps read the anchors before writing their pools, because scoring and Swap both check isDoubleSwap && isStableToStable. In a queue of single swaps, the first read is the first swap's DollarsPerRune, which runs after swapOne's SetPool, so the cache was off for the whole block. On develop that happens in every block whose queue holds no double swap. In the replay lab, whose isStableToStable never reads the list, it happened in all 151 swap blocks checked.

  • SwapQueueAdv.EndBlock now reads the anchors once before scoring, when the queue is not empty (idle blocks don't scan).
  • It uses a cache the caller already attached, which the new test needs; nothing in production attaches one.
  • Test TestEndBlockAnchorsCacheSurvivesSingleSwaps: one RUNE→ETH market swap. The swap runs and writes its pool, and the cache is still filled afterwards. It fails with the warm-up line removed.
  • scripts/perf/guard.sh: PASS, all 2,481 regression output files identical (state, events, gas_used).

Mainnet replay (1,999 blocks 28124001..28125999, app hashes checked against mainnet; v3.20.3-based lab stack with the cache, ± this commit; CPU profiles summed over 3 interleaved rounds):

without warm-up with warm-up Δ
GetAnchors 69.4 s 20.1 s −71%
SwapQueueAdv.EndBlock 228.7 s 173.0 s −24%
AppModule.EndBlock 401.7 s 341.5 s −15%
FinalizeBlock 710.9 s 645.9 s −9.1%

Wall time: 349.9 → 347.6, 360.2 → 349.9, 337.1 → 279.7 s; median −0.7%. The shared host ran at a 1-minute load of 30–65, and the load moved between sides in round 3, so the wall median can't resolve a change this size. The CPU profile is the evidence. The table is an upper bound for develop: on develop, blocks with a double swap queued already filled the cache through scoring.

Relation to other work

This replaces RSI commit b645117d32 (adv swap queue scoring reads the TOR anchors once per call), which isn't on develop. This cache covers that scoring call plus the Swap and DollarsPerRune reads. Only one of the two should land.

🤖 Generated with Claude Code

Edited by Huginn Ai

Merge request reports

Loading
Loading