code-423n4 / 2022-06-nested-findings

0 stars 1 forks source link

Gas Optimizations #99

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[Gas-1] Potential usage of unchecked

Following variables or operations can be wrapped by unchecked to reduce gas cost.

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++
    }
}

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

https://github.com/code-423n4/2022-06-nested/blob/main/contracts/NestedFactory.sol#L138

        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.

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

Yashiru commented 2 years ago

[Gas-1] Potential usage of unchecked (Duplicated)

Duplicated of #2 at For loop optimizaion

[Gas-2] No need to set 0 on uint variables (Duplicated)

Duplicated of #2 at For loop optimizaion