Gas Optimization
Identifies opportunities to reduce gas consumption including storage packing, unnecessary SLOADs, and suboptimal loop patterns.
Gas Optimization
Overview
The gas optimization detector identifies patterns where gas consumption can be reduced without changing contract semantics. Findings include redundant storage reads (cache in memory), unoptimized storage layout (pack adjacent variables), and loop patterns that waste gas on repeated computations.
Detection Methodology
- Redundant SLOAD detection: Identifies multiple SLOADs from the same slot within a function without intervening SSTOREs.
- Storage packing analysis: Checks whether adjacent state variables could be packed into fewer storage slots.
- Loop optimization: Detects loop-invariant computations that could be hoisted, and array length reads in loop conditions.
- Calldata vs memory: Flags
memoryparameters that could usecalldatafor read-only access.
Examples
Sample Sigvex Output
{
"detector_id": "gas-optimization",
"severity": "low",
"confidence": 0.90,
"description": "Storage slot 0x03 is read 4 times in function process() without intervening writes. Cache the value in a local variable to save ~2,100 gas per redundant SLOAD.",
"location": { "function": "process()", "offset": 82 }
}
Related Detectors
- Dead Code — unreachable code
- Bytecode Size Limit — deployment cost
- Loop Gas Exhaustion — unbounded loops