Update October 2023: starting in Solidity 0.8.22, the G011
("Unnecessary checked arithmetic in for loop") gas optimization is performed automatically by the solc
compiler! So the unchecked
block can be dropped:
+ for (uint256 i; i < len; ++i) {
- for (uint256 i; i < len; ) {
- unchecked { ++i; }
}
Other gas optimizations remain the same however.
A simple smart contract to test the effectiveness of common code4rena gas optimizations.
Specifically:
G001
- Don't Initialize Variables with Default ValueG002
- Cache Array Length Outside of LoopG011
- Unnecessary checked arithmetic in for loopG012
- Use Prefix Increment instead of Postfix Increment if possible
Use Echidna to run the test:
echidna GasTest.sol --config echidna.yaml