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.
[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