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

0 stars 1 forks source link

Gas Optimizations #77

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

To optimize the for loop and make it consume less gas, i suggest to:

  1. If a variable is not set/initialized, it is assumed to have the default value (0 for uint, false for bool, address(0) for address…). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

  2. Use ++i instead of i++, which is a cheaper operation (in this case there is no difference between i++ and ++i because we dont use the return value of this expression, which is the only difference between these two expression).

As an example:

for (uint256 i = 0; i < numIterations; ++i) { 

should be replaced with

for (uint256 i; i < numIterations; ++i) {

i suggest to change the code below: 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 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/OperatorResolver.sol#L75 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#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/NestedFactory.sol#L651

Yashiru commented 2 years ago

1. If a variable is not set/initialized, it is assumed to have the default value (Duplicated)

Duplicated of #2 at For loop optimizaion

2. Use ++i instead of i++ (Duplicated)

Duplicated of #2 at For loop optimizaion