Adapter Caching
One of the most effective optimizations is adapter caching in batch operations. When processing multiple operations with the same adapter, the Router caches the adapter address to avoid repeated storage reads.How It Works
In RouterLogic.sol:188-280, the optimized routing function implements:Gas Savings
- Cold SLOAD: ~2,100 gas per adapter lookup
- Cache hit: Skips SLOAD entirely
- Typical savings: 20-40% for batches with repeated adapters
Special Selectors
Special selectors bypass the adapter lookup mechanism entirely for common operations, providing even greater gas savings.Available Special Selectors
singleCall
multiCall
Fee Collection
In-router fee collection functions process fees without delegatecall:How Special Selectors Work
From DirectRoutes.sol:146-179:Batch Operations
Optimized Encoding
Theoptimized_routeFill921336808 function uses encoded calldata to reduce costs:
- Reduces calldata costs by avoiding array decoding overhead
- Saves ~200-500 gas per element
- Assembly-based decoding operates directly on calldata pointers
Assembly Decoding
From RouterLogic.sol:206-221:Solver Context Management
Skip Relayer Context
Adapters can declare they don’t need relayer context using adapter tags:- Skips array access and bounds checking
- Prevents unnecessary context consumption tracking
- Example: IntentExecutorAdapter doesn’t consume relayer context
Context Consumption Tracking
From RouterLogic.sol:261-280:Packed Calldata Encoding
For approval operations, DirectRoutes uses packed encoding to save calldata costs.Single Approval
From DirectRoutes.sol:332-368:Batch Approvals
From DirectRoutes.sol:386-415:Performance Benchmarks
Based on DirectGasComparison.t.sol test results:Adapter Caching
20-30% savings for 3+ identical adapter calls
Special Selectors
~2,600 gas saved per direct route
Encoded Calldata
200-500 gas saved per batch element
Packed Encoding
~1,200 gas saved per approval
Best Practices
1
Group by Adapter
Batch operations that use the same adapter together to maximize cache hits
2
Use Special Selectors
For simple calls and fee collection, use special selectors instead of custom adapters
3
Skip Unnecessary Context
If your adapter doesn’t need relayer context, use
setSkipRelayerContext() in your adapter tag4
Use Packed Encoding
For approvals and simple data, prefer packed encoding over ABI encoding