Loading
perf(querier): cut API swap quote simulation cost
🔎 Visual review of454c9cec: explainer, review flags and prompts to ask an AI about the change
Summary
This makes API swap quotes (/thorchain/quote/swap and the gRPC QuoteSwap served to external clients) cheaper. It removes repeated store work inside simulate(). Each commit is its own change with its own A/B in its message:
| Commit | Change |
|---|---|
894df300f8 |
test: quote benchmark cases with a mainnet-sized (134-item) swap queue |
1975dad1af |
the vault-security sort walks the txout window once per sort, not once per vault (API quotes only) |
fb850b19ed |
the quote TxOutCache fills 1,000 heights per range read instead of one point read per height |
3cf0103658 |
a quote's unsent txout window is reused across vault sorts while the cache is live |
aa24da3663 |
a simulation finds the pools it touched by comparing stored bytes, without decoding |
0fbe2ae7f9 |
API simulations drop the stale market swap index once, instead of removing each orphaned entry by rewriting the whole list (quadratic in the queue) |
b7e68248fc, 222ac34543 |
tests: assert the first vault sort's result on a live cache; unit-test TxOutCache's nil, disabled and fork paths |
Risk: high (keeper code: keeper_vault.go, keeper_txout.go, txout_cache.go, querier.go).
Consensus and gas
- Every new path runs only for API quotes, which carry a
TxOutCacheand run outside the state machine. - Contract quotes (
CtxWASMQuery, a wasmQuoteSwapinside a tx, where store reads are gas) keep develop's code. So do tx and block paths, which have no cache. - Wasm quote gas/op is unchanged on every benchmark case.
- Quote responses are byte-identical to develop's. This was checked with a scratch dump of 25 request shapes × plain/queued fixtures × API/contract, under the mainnet and mocknet tags.
Evidence
Cumulative A/B of the branch against develop (scripts/perf/ab.sh, 10 interleaved rounds, independent confirmation pass):
| Benchmark | sec/op | allocs/op |
|---|---|---|
| PerfQuoteSwap/l1-to-l1 | −35.6% (p=0.000) | −37.8% |
| PerfQuoteSwap/rune-to-l1 | −19.1% (p=0.004) | −32.0% |
| PerfQuoteSwap/l1-to-rune | −19.1% (p=0.000) | −37.3% |
| PerfQuoteSwap/streaming | −31.4% (p=0.007) | −36.6% |
| PerfQuoteSwap/wasm/* | ~ | ~ |
| PerfEndBlock/* | ~ | ~ |
- Wasm and tx gas/op are unchanged. API gas/op drops (−4% to −10%); that's informational, since API queries carry no
gas_used. - The queued cases are new in this branch, so they have no develop baseline here. Their per-commit wins are in
fb850b19edand0fbe2ae7f9, for example queued/streaming −15.5%. - Per commit, each message records a significant QuoteSwap win from its own confirmation pass. The smallest is
3cf0103658at −4% to −5%. I didn't rerun those individually; the cumulative pass above confirms the total. scripts/perf/guard.shvs develop: PASS. 2,446 of 2,464 regression output files were identical in the parallel run. The other 18 are one suite,rune-pool/rune-pool-pol, which failed once in the parallel candidate run and matched the base byte for byte when the guard reran it in isolation. It makes no quote; the same flake is noted in0fbe2ae7f9.
Tests and coverage
Changed-line coverage: x/thorchain 42/42, keeper 72/73, keeper/v1 74/83, so 188/198 (95%) in total.
Edited by Huginn Ai