Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #491

Open
code423n4 opened this issue Jul 14, 2022 · 0 comments
Open

Gas Optimizations #491

code423n4 opened this issue Jul 14, 2022 · 0 comments
Labels
bug Warden finding G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

i++ CAN BE UNCHECK AND REPLACED WITH ++i

++i costs less gas compared to i++ or i += 1 for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration). This statement is true even with the optimizer enabled.

Besides, In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline. The risk of overflow is inexistant for a uint256 here.

Original code:
https://github.com/code-423n4/2022-07-fractional/blob/e2c5a962a94106f9495eb96769d7f60f7d5b14c9/src/Vault.sol#L104
https://github.com/code-423n4/2022-07-fractional/blob/e2c5a962a94106f9495eb96769d7f60f7d5b14c9/src/Vault.sol#L78

So the gas optimized code would be

for (uint256 i = 0; i < length;) {    
     //...
    unchecked { ++i; }  
} 
code423n4 added a commit that referenced this issue Jul 14, 2022
@mehtaculous mehtaculous added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

2 participants