All the gas optimizations are determined based opcodes and sample tests.
Count | Issues | Instances | Gas Saved |
---|---|---|---|
[G-1] | Reduce gas usage by moving to Solidity 0.8.19 or later | 2 | - |
[G-2] | Modifiers are redundant if used only once or not used at all | 3 | 120 |
[G-3] | abi.encode is more efficient than abi.encodePacked | 2 | 180 |
[G-4] | Use nested if and, avoid multiple check combinations | 1 | 15 |
[G-5] | Constructors can be marked payable | 1 | 105 |
[G-6] | The compiler uses opcodes GT and ISZERO for solidity code that uses > , but only requires LT for >= |
1 | 3 |
[G-7] | State variables should be cached in stack variables rather than re-reading them from storage | 2 | 700 |
[G-8] | Use assembly to emit an event | 4 | 152 |
[G-9] | Use assembly to check for address(0) | 4 | 24 |
There are 2 instances of this issue:
Gas save: 120 Gas
https://github.com/Cyfrin/2023-07-escrow/blob/main/src/Escrow.sol
https://github.com/Cyfrin/2023-07-escrow/blob/main/src/Escrow.sol
https://github.com/Cyfrin/2023-07-escrow/blob/main/src/Escrow.sol
abi.encode
is more efficient than abi.encodePacked
(Save 180 )abi.encode
uses less gas than abi.encodePacked
: the gas saved depends on the number of arguments, with an average of ~90 per argument.
Gas save: 180 gas
There are 2 instances of this issue.
Test :
https://github.com/Cyfrin/2023-07-escrow/blob/main/src/EscrowFactory.sol#L76
https://github.com/Cyfrin/2023-07-escrow/blob/main/src/EscrowFactory.sol#L71C24-L71C42
Using nested is cheaper than using && multiple check combinations. There are more advantages, such as easier to read code and better coverage reports.
Gas save: 15 gas
payable
Payable functions cost less gas to execute, since the compiler does not have to add extra checks to ensure that a payment wasn't provided. A constructor can safely be marked as payable, since only the deployer would be able to pass funds, and the project itself would not pass any funds.
Gas save: 105 gas
GT
and ISZERO
for solidity code that uses >
, but only requires LT
for >=
,Gas saves: 3 gas
The instances below point to the second+ access of a state variable within a function. Caching of a state variable replaces each Gwarmaccess (100 gas) with a much cheaper stack read. Other less obvious fixes/optimizations include having local memory caches of state variable structs, or having local caches of state variable contracts/addresses
Gas save: 700 gas
event
To efficiently emit events, it's possible to utilize assembly by making use of scratch space and the free memory pointer. This approach has the advantage of potentially avoiding the costs associated with memory expansion.
However, it's important to note that in order to safely optimize this process, it is preferable to cache and restore the free memory pointer.
A good example of such practice can be seen in Solady's codebase.
Gas save: 152 Gas
address(0)
A simple zero address check can be written in assembly to save some gas.
Gas save: 24 gas
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.