Open code423n4 opened 2 years ago
Not anymore
Valid but these are deployment costs
Valid but in lack of gas golfing I can't give it points
Saves 5 gas
7 * 5 = 35
Saves 6 gas per instance 9 * 6 (2 seem to not be valid) = 54
Report is really concise, which is nice, grep seems to be missing tons of these so I recommend the warden to do more manual searches
Total Gas saved: 89
1.
!=0
is cheaper than>0
!=0
is cheaper than using>0
for uints for uints2. Initilization to default values can be omitted.
uint variables are set to zero by default, initializing them to zero (for storage variables) causes extra gas.
3. Use solidity custom errors to save gas
solidity 0.8.4 introduces custom errors which are cheaper than using revert strings in terms of gas Use the custom error patterns to reduce gas cost.
for eg.
more details can be found here
4. using prefix increment in loops can save some gas
Prefix increment operation(++i) is more gas effiecient than postfix increment(i++), while few instance of postfix increment doesn't makes much difference in overall gas, in loops these increments are called a large number of times resulting in significant gas savings.
Also, since the loop increment starts from zero and only increases, there is no chance of overflow. So the increment can also be added inside a unchecked block so save further gas
The instances of prefix increment can be found as follows
5. array length in loops can be cached instead of calculating in every iteration
The loop bounds are calculated with
array.length
which are calculated in every loop iterations which can result in high gas. The array length can be cached instead of calculating in every loop iteration to save gas.The instances where this pattern can be applied is found as follows