Skip to content

Gas optimizations: loop and storage improvements#7

Open
eil-docbot wants to merge 8 commits intoeth-infinitism:masterfrom
eil-docbot:gas-optimizations
Open

Gas optimizations: loop and storage improvements#7
eil-docbot wants to merge 8 commits intoeth-infinitism:masterfrom
eil-docbot:gas-optimizations

Conversation

@eil-docbot
Copy link

Summary

  • Phase 1: Loop optimizations across core contracts (cache array lengths, unchecked increments)
  • Phase 2: Storage variable optimizations
  • Optimize getChainInfos in L1AtomicSwapStakeManager

Affects 15 files across the codebase including CrossChainPaymaster, L1AtomicSwapStakeManager, bridge connectors, and swap managers.

forshtat and others added 8 commits November 20, 2025 21:06
* Add README.md

---------

Co-authored-by: shahafn <shahaflol@gmail.com>
Applied loop optimizations to several core smart contracts:
- Cached array lengths outside loops.
- Used `unchecked { ++i }` for loop increments.
- Removed `= 0` initializations where appropriate.

Optimized files include:
- `src/CrossChainPaymaster.sol`
- `src/destination/DestinationSwapManager.sol`
- `src/destination/TokenDepositManager.sol`
- `src/origin/OriginSwapManager.sol`
Completed the remaining loop optimizations across various smart contracts, improving gas efficiency by caching array lengths and using unchecked increments.

Optimized files:
- `src/L2XlpRegistry.sol`
- `src/destination/DestinationSwapDisputeManager.sol`
- `src/L1AtomicSwapStakeManager.sol`
- `src/origin/OriginationSwapDisputeManager.sol`
- `src/bridges/arbitrum/L1ArbitrumBridgeConnector.sol`
- `src/common/utils/XlpSelectionHelper.sol`
- `src/bridges/optimism/L1OptimismBridgeConnector.sol`
- `src/bridges/ethereum/L1EthereumLocalBridge.sol`
Reordered fields in several structs to optimize storage packing and reduce SLOADs. While the goal was to reduce gas costs, some functions saw slight increases, which might be due to complex access patterns for packed variables or compiler optimizations.

Optimized files:
- `eil-contracts/src/types/AtomicSwapMetadata.sol`
- `eil-contracts/src/types/AtomicSwapVoucher.sol`
- `eil-contracts/src/types/StakeManagerStructs.sol`

Gas Benchmarks (after optimization):
lockUserDeposit_singleAsset: 131330 (Previous: 131315, Diff: +15)
lockUserDeposit_twoAssets: 134607 (Previous: 134592, Diff: +15)
issueVouchers_single: 172636 (Previous: 171977, Diff: +659)
issueVouchers_batch3: 403598 (Previous: 402653, Diff: +945)
withdrawFromUserDeposit_single: 60007 (Previous: 59995, Diff: +12)
cancelVoucherRequest: 77286 (Previous: 77296, Diff: -10)
withdrawFromVoucher_single: 107420 (Previous: 107222, Diff: +198)
depositToXlp_native: 44687 (Previous: 44711, Diff: -24)
tokenDepositToXlp: 48395 (Previous: 48463, Diff: -68)
multiTokenDepositToXlp_2: 111178 (Previous: 111246, Diff: -68)
nativeWithdrawToXlp: 62096 (Previous: 62118, Diff: -22)
tokenWithdraw: 60829 (Previous: 60917, Diff: -88)
Cached the `chainIds` array in memory before the loop in the `getChainInfos` function. This avoids repeated `SLOAD` operations for the `chainIds` array within the loop, leading to a reduction in gas costs for this function.
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.

4 participants