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

0 stars 1 forks source link

Gas Optimizations #85

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Avoiding initialization of loop index can save a little gas

    POC

    Examples of this issue in the codebase:

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

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

impact

The local variable used for the loop index need not be initialized to 0 because the default value is 0. Avoiding this anti-pattern can save a few opcodes and therefore a tiny bit of gas.

  1. Using Prefix (++i) rather than postfix (i++) in increment/decrement operators in for-loops

POC

Examples of this issue in the codebase:

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

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

impact

using the prefix increment/decrement operators (++i/--i) cost less gas PER LOOP than the postfix increment/decrement operators (i++/i--)

  1. For-Loops: Increments can be unchecked

    POC

    Examples of this issue in the codebase:

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

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

impact

In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.

  1. Use Custom Errors instead of Revert Strings to save Gas

    POC

    Examples of this issue in the codebase:

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

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

impact

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met).

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

maximebrugel commented 2 years ago

4. Use Custom Errors instead of Revert Strings to save Gas (Duplicated)

6 (see comment)

Yashiru commented 2 years ago

1. Avoiding initialization of loop index can save a little gas (Duplicated)

Duplicated of #2 at For loop optimizaion

2. Using Prefix (++i) rather than postfix (i++) in increment/decrement operators in for-loops (Duplicated)

Duplicated of #2 at For loop optimizaion

3. For-Loops: Increments can be unchecked (Duplicated)

Duplicated of #2 at For loop optimizaion