Skip to content

Collapse settlement metrics into single-pass summarize()#4388

Open
ashleychandy wants to merge 6 commits intocowprotocol:mainfrom
ashleychandy:perf/settlement-single-pass-summarize
Open

Collapse settlement metrics into single-pass summarize()#4388
ashleychandy wants to merge 6 commits intocowprotocol:mainfrom
ashleychandy:perf/settlement-single-pass-summarize

Conversation

@ashleychandy
Copy link
Copy Markdown
Contributor

Description

Previously, computing settlement metrics required four separate iterations
over the trades collection — one each for surplus_in_ether(),
fee_in_ether(), fee_breakdown(), and jit_orders():

let gas = settlement.gas();
let gas_price = settlement.gas_price();
let surplus = settlement.surplus_in_ether();
let fee = settlement.fee_in_ether();
let fee_breakdown = settlement.fee_breakdown();
let jit_orders = settlement.jit_orders();

This PR introduces a summarize() method on Settlement that performs a
single pass over self.trades, accumulating all four values simultaneously
and returning them as a SettlementMetrics<'_> struct. The call site in
infra/persistence is updated to destructure directly from the returned struct:

let domain::settlement::SettlementMetrics {
    gas,
    gas_price,
    surplus,
    fee,
    fee_breakdown,
    jit_orders,
} = settlement.summarize();

The returned struct is defined as:

#[derive(Debug)]
pub struct SettlementMetrics<'a> {
    pub gas: eth::Gas,
    pub gas_price: eth::EffectiveGasPrice,
    pub surplus: eth::Ether,
    pub fee: eth::Ether,
    pub fee_breakdown: HashMap<domain::OrderUid, trade::FeeBreakdown>,
    pub jit_orders: Vec<&'a trade::Jit>,
}

Changes

  • domain/settlement/mod.rs

    • Added SettlementMetrics<'a> struct
    • Added Settlement::summarize() — single-pass computation replacing
      the four individual iteration methods
    • Preserves existing tracing::warn! fallback behaviour on per-trade errors
  • infra/persistence/mod.rs

    • Replaced six individual settlement.*() calls with a single
      settlement.summarize(), destructuring all fields directly from
      SettlementMetrics via pattern matching

Fixes #4346

@ashleychandy ashleychandy requested a review from a team as a code owner May 8, 2026 06:47
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much easier to review, thanks for the effort!

Some notes though

Comment thread crates/autopilot/src/infra/persistence/mod.rs
Comment thread crates/autopilot/src/domain/settlement/mod.rs Outdated
Comment thread crates/autopilot/src/domain/settlement/mod.rs Outdated
Comment thread crates/autopilot/src/domain/settlement/mod.rs Outdated
@ashleychandy
Copy link
Copy Markdown
Contributor Author

@jmg-duarte thanks for the detailed review, I’ve addressed the comments:

removed the now-unused individual metric functions
simplified the summarize docs
restricted SettlementMetrics fields to pub(crate)
removed the redundant inline comments in the loop

Copy link
Copy Markdown
Contributor

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice this one, but again, not needed

Comment thread crates/autopilot/src/domain/settlement/mod.rs Outdated
Comment thread crates/autopilot/src/domain/settlement/mod.rs
Copy link
Copy Markdown
Contributor

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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.

perf: compute settlement surplus, fee, and fee breakdown in a single pass

2 participants