Gas Optimizations #16
Labels
bug
Something isn't working
G (Gas Optimization)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Impact
[1] Consider using optimized for-loop and apply the following optimizations:
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[2] Splitting && conditions into several require statements saves gas.
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[3] Using x != 0 uses 6 less gas than x > 0.
Consider changing all "greater than zero" comparisons to "not equal to zero".
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[4] Using ++x uses 5 less gas than x++ (same applies to --x).
Consider changing all "x++" operators to "++x" (same applies to --x).
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[5] As per 0.8.4 solidity version it supports new custom errors.
Custom errors are reducing 38 gas if condition is met and 22 gas otherwise.
Also reduces contract size and deployment costs.
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[6] No overflow possible here because total supply will overflow earlier.
You can use unchecked keyword here:
unchecked{ _balances[_for] = _balances[_for] + _amount; }
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[7] No overflow possible here because _balance will overflow earlier.
You can use unchecked keyword here:
unchecked{ _totalSupply = _totalSupply - amount; }
Affected code:
Proof of Concept
Tools Used
Recommended Mitigation Steps
Impact
[8] Storage keyword is recommended to be used whenever possible. It can save up-to 2k gas per each variable with memory keyword (if updating storage up-to 5-10k).
Memory keyword is supposed to be used when temporary data is being updated and calculated regularly.
Affected code:
Proof of Concept
https://geeksforgeeks.org/storage-vs-memory-in-solidity
Tools Used
Recommended Mitigation Steps
The text was updated successfully, but these errors were encountered: