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 #382

Open
code423n4 opened this issue Sep 23, 2022 · 0 comments
Open

Gas Optimizations #382

code423n4 opened this issue Sep 23, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

code423n4 commented Sep 23, 2022

Array Optimization

https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/VTVLVesting.sol#L353-L355
i++ can become ++i to save approximately 5 gas per iteration
The part uint i = 0 can become uint i as just letting the default be applied is cheaper than setting i to the default value of 0.
The ++i part can also become unchecked, unchecked{ ++i; } as the createClaimsBatch function will certainly run out of gas before ++i overflows from getting to uint256(-1).

Therefore the new array will be:

for (uint256 i; i < length; ) { 
    _createClaimUnchecked(_recipients[i], _startTimestamps[i], _endTimestamps[i], _cliffReleaseTimestamps[i], _releaseIntervalsSecs[i], 
    _linearVestAmounts[i], _cliffAmounts[i]); 
    unchecked{ ++i; }
}

Using bools for storage incurs unnecessary overhead

Using uint256(1)and uint256(2) for true/false avoids a 100 gas Gwarmaccess, and avoids a 20000 gas Gsset when changing from false to true after having been a true value in the past.

Instances are:
https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/VTVLVesting.sol#L45
https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/AccessProtected.sol#L12

Letting the default of zero be applied to uint variables saves gas

Setting a uint variable to zero costs more than allowing the default value of zero be applied, therefore the = 0 can be removed in these instances:
https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/VTVLVesting.sol#L27
https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/VTVLVesting.sol#L148
(I already mentioned in the array optimization section) https://github.com/code-423n4/2022-09-vtvl/blob/main/contracts/VTVLVesting.sol#L353

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Sep 23, 2022
code423n4 added a commit that referenced this issue Sep 23, 2022
code423n4 added a commit that referenced this issue Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant