You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following variables or operations can be wrapped by unchecked to reduce gas cost.
The increment of i in the for loop
Following codebase which contains for loop can wrap i++ by unchecked since the end condition of the for loop uses uint256 variable.
Here is an example.
for (uint256 i = 0; i < operatorsLength; i++) {
operatorsLength is uint256, and i++ will not overflow in the for loop since it has the end condition i < operatorsLength. The above part can be written like this by using unchecked which reduces the gas cost.
for (uint256 i = 0; i < operatorsLength; ) {
// .... omitted
unchecked {
i++
}
}
uint256 operatorsLength = operatorsCache.length;
for (uint256 i = 0; i < operatorsLength; i++) {
if (operatorsCache[i] == operator) {
operatorsCache[i] = operators[operatorsLength - 1];
Since operatorsLength is uint256, operatorsLength - 1 will not be underflown. Therefore, this part can be written like this:
uint256 operatorsLength = operatorsCache.length;
for (uint256 i = 0; i < operatorsLength; i++) {
if (operatorsCache[i] == operator) {
unchecked {
operatorsCache[i] = operators[operatorsLength - 1];
}
[Gas-2] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
Yashiru
added
sponsor disputed
Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
duplicate
This issue or pull request already exists
and removed
sponsor disputed
Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
labels
Jun 24, 2022
[Gas-1] Potential usage of unchecked
Following variables or operations can be wrapped by unchecked to reduce gas cost.
i
in the for loopFollowing codebase which contains for loop can wrap
i++
by unchecked since the end condition of the for loop uses uint256 variable.Here is an example.
operatorsLength
is uint256, and i++ will not overflow in the for loop since it has the end conditioni < operatorsLength
. The above part can be written like this by using unchecked which reduces the gas cost.https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L136
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L196
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L256
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L315
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L333
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L369
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L412
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/OperatorResolver.sol#L40
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/OperatorResolver.sol#L60
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/operators/Beefy/BeefyVaultOperator.sol#L18
operators[operatorsLength - 1]
can be wrapped by uncheckedhttps://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L138
Since
operatorsLength
is uint256,operatorsLength - 1
will not be underflown. Therefore, this part can be written like this:[Gas-2] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L124
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L136
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L196
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L315
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L333
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L369
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L412
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L651
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/OperatorResolver.sol#L40
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/OperatorResolver.sol#L60
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/abstracts/MixinOperatorResolver.sol#L37
https://github.com/code-423n4/2022-06-nested/blob/main/contracts/abstracts/MixinOperatorResolver.sol#L56
The text was updated successfully, but these errors were encountered: