Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
Solidity does not recognize null as a value, so uint variables are initialized to zero. Setting a uint variable to zero is redundant and can waste gas.
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
Gas Optimazations
[G-01] Use prefix not postfix in loops
Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
There are 5 occurrences
Recommended Mitigation Steps
Use prefix not postfix to increment in a loop
[G-02] Redundant zero initialization
Solidity does not recognize null as a value, so uint variables are initialized to zero. Setting a uint variable to zero is redundant and can waste gas.
There are 6 occurrences
Recommended Mitigation Steps
Remove the redundant zero initialization
[G-03] Use != 0 instead of > 0
Using > 0 uses slightly more gas than using != 0. Use != 0 when comparing uint variables to zero, which cannot hold values below zero.
There are 9 occurrences
Recommended Mitigation Steps
Replace
> 0
with!= 0
to save gas[G-04] Adding unchecked directive can save gas
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
There are 11 occurrences
Recommended Mitigation Steps
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
[G-05] Use Custom Errors instead of revert()/require()
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)
Revert function names are used as strings instead of the error functions.
There are 15 occurrences
Recommended Mitigation Steps
Recommended to replace revert strings with custom errors.