Skip to content

Comments

refactor: optimize gas usage across contracts#119

Open
ScriptedBro wants to merge 2 commits intoRemitwise-Org:mainfrom
ScriptedBro:refactor/optimize-gas-usage-all-contracts
Open

refactor: optimize gas usage across contracts#119
ScriptedBro wants to merge 2 commits intoRemitwise-Org:mainfrom
ScriptedBro:refactor/optimize-gas-usage-all-contracts

Conversation

@ScriptedBro
Copy link

Closes #12
Issue: #12

Summary

This PR implements a gas/cost-efficiency pass across the RemitWise Soroban contracts, focusing on benchmarked hot paths and storage-heavy read operations.

Key improvements

  • remittance_split

    • Removed duplicate split arithmetic
    • Added internal split calculation path for distribute_usdc to avoid unnecessary event/allocation overhead
    • Reduced redundant nonce helper cloning
  • savings_goals

    • Added owner->goal index cache (OWN_GOAL)
    • Optimized get_all_goals(owner) with a hybrid strategy (index path for subsets, full-scan fast path when owner owns all goals)
  • bill_payments

    • Added cached per-owner unpaid totals (UNPD_TOT)
    • Optimized get_total_unpaid(owner) to use cached totals (fallback scan preserved)
  • insurance

    • Added cached per-owner active premium totals (PRM_TOT)
    • Optimized get_total_monthly_premium(owner) to use cached totals (fallback scan preserved)
  • family_wallet

    • Removed redundant signers.clone() in configure_multisig
  • Code cleanup / size-related

    • Removed unused dead file bill_payments/src/schedule.rs
    • Removed stale commented-out schedule module scaffolding

Acceptance Criteria

  • Gas usage analyzed
  • Optimizations implemented
  • Gas benchmarks
  • Documentation updated

Gas Benchmark Results (Before vs After)

Aggregate CPU improvement across benchmarked methods: -3.87%

Validation Performed

cargo check -p remittance_split -p savings_goals -p bill_payments -p insurance -p family_wallet

cargo test -p remittance_split -p savings_goals -p bill_payments -p insurance -p family_wallet

cargo test -p remittance_split --test gas_bench -- --nocapture
cargo test -p savings_goals --test gas_bench -- --nocapture
cargo test -p bill_payments --test gas_bench -- --nocapture
cargo test -p insurance --test gas_bench -- --nocapture
cargo test -p family_wallet --test gas_bench -- --nocapture

Documentation

  • Added docs/gas-optimization.md with:
    • benchmark methodology/results
    • compiler optimization settings verification
    • current WASM artifact sizes
    • validation commands and tradeoffs

@Baskarayelu
Copy link
Contributor

@ScriptedBro resolve the conflicts & fix the pipeline

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.

Gas Optimization

2 participants