Collapse settlement metrics into single-pass summarize()#4388
Collapse settlement metrics into single-pass summarize()#4388ashleychandy wants to merge 6 commits intocowprotocol:mainfrom
summarize()#4388Conversation
There was a problem hiding this comment.
Code Review
This pull request optimizes settlement metric calculations by introducing a SettlementMetrics struct and a summarize method that performs a single pass over trades. This replaces four separate iterations previously used to compute surplus, fees, and JIT orders. The persistence layer was updated to use this more efficient approach. No critical issues found.
jmg-duarte
left a comment
There was a problem hiding this comment.
Much easier to review, thanks for the effort!
Some notes though
|
@jmg-duarte thanks for the detailed review, I’ve addressed the comments: removed the now-unused individual metric functions |
jmg-duarte
left a comment
There was a problem hiding this comment.
I expect this to be the final list of notes, will run CI as well
| let fee = settlement.fee_in_ether(); | ||
| let fee_breakdown = settlement.fee_breakdown(); | ||
| let jit_orders = settlement.jit_orders(); | ||
| // Use optimized single-pass calculation |
There was a problem hiding this comment.
I didn't notice this one, but again, not needed
Description
Previously, computing settlement metrics required four separate iterations
over the trades collection — one each for
surplus_in_ether(),fee_in_ether(),fee_breakdown(), andjit_orders():This PR introduces a
summarize()method onSettlementthat performs asingle pass over
self.trades, accumulating all four values simultaneouslyand returning them as a
SettlementMetrics<'_>struct. The call site ininfra/persistenceis updated to destructure directly from the returned struct:The returned struct is defined as:
Changes
domain/settlement/mod.rsSettlementMetrics<'a>structSettlement::summarize()— single-pass computation replacingthe four individual iteration methods
tracing::warn!fallback behaviour on per-trade errorsinfra/persistence/mod.rssettlement.*()calls with a singlesettlement.summarize(), destructuring all fields directly fromSettlementMetricsvia pattern matchingFixes #4346