3.9 KiB
3.9 KiB
Performance Tuning Guide
Overview
This guide covers optimization strategies for improving gas efficiency and execution speed.
Gas Optimization
1. Batch Size Optimization
Problem: Large batches consume more gas
Solution:
- Optimize batch sizes (typically 5-10 calls)
- Split very large strategies into multiple transactions
- Use flash loans to reduce intermediate steps
2. Call Optimization
Problem: Redundant or inefficient calls
Solution:
- Combine similar operations
- Remove unnecessary calls
- Use protocol-specific batch functions (e.g., Balancer batchSwap)
3. Storage Optimization
Problem: Excessive storage operations
Solution:
- Minimize state changes
- Use events instead of storage where possible
- Cache values when appropriate
RPC Optimization
1. Connection Pooling
Problem: Slow RPC responses
Solution:
- Use multiple RPC providers
- Implement connection pooling
- Cache non-critical data
2. Batch RPC Calls
Problem: Multiple sequential RPC calls
Solution:
- Use
eth_callbatch requests - Parallelize independent calls
- Cache results with TTL
3. Provider Selection
Problem: Single point of failure
Solution:
- Use multiple providers
- Implement failover logic
- Monitor provider health
Caching Strategy
1. Price Data Caching
// Cache prices with 60s TTL
const priceCache = new Map<string, { price: bigint; timestamp: number }>();
async function getCachedPrice(token: string): Promise<bigint> {
const cached = priceCache.get(token);
if (cached && Date.now() - cached.timestamp < 60000) {
return cached.price;
}
const price = await fetchPrice(token);
priceCache.set(token, { price, timestamp: Date.now() });
return price;
}
2. Address Caching
// Cache protocol addresses (rarely change)
const addressCache = new Map<string, string>();
3. Gas Estimate Caching
// Cache gas estimates with short TTL (10s)
const gasCache = new Map<string, { estimate: bigint; timestamp: number }>();
Strategy Optimization
1. Reduce Steps
Problem: Too many steps increase gas
Solution:
- Combine operations where possible
- Use protocol batch functions
- Eliminate unnecessary steps
2. Optimize Flash Loans
Problem: Flash loan overhead
Solution:
- Only use flash loans when necessary
- Minimize operations in callback
- Optimize repayment logic
3. Guard Optimization
Problem: Expensive guard evaluations
Solution:
- Cache guard results when possible
- Use cheaper guards first
- Skip guards for trusted strategies
Monitoring Performance
Metrics to Track
- Gas Usage: Average gas per execution
- Execution Time: Time to complete strategy
- RPC Latency: Response times
- Cache Hit Rate: Caching effectiveness
- Success Rate: Execution success percentage
Tools
- Gas tracker dashboard
- RPC latency monitoring
- Cache hit rate tracking
- Performance profiling
Best Practices
- Profile First: Measure before optimizing
- Optimize Hot Paths: Focus on frequently used code
- Test Changes: Verify optimizations don't break functionality
- Monitor Impact: Track improvements
- Document Changes: Keep optimization notes
Common Optimizations
Before
// Multiple sequential calls
const price1 = await oracle.getPrice(token1);
const price2 = await oracle.getPrice(token2);
const price3 = await oracle.getPrice(token3);
After
// Parallel calls
const [price1, price2, price3] = await Promise.all([
oracle.getPrice(token1),
oracle.getPrice(token2),
oracle.getPrice(token3),
]);
Performance Checklist
- Optimize batch sizes
- Implement caching
- Use connection pooling
- Parallelize independent calls
- Monitor gas usage
- Profile execution time
- Optimize hot paths
- Document optimizations