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

0 stars 0 forks source link

Gas Optimizations #3

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Gas Savings

---------------------------------------------------------------------------

1 - Accessing array length in for loop can be optimized by caching in local variable in order to save SLOAD op per spin of the loop

https://github.com/code-423n4/2022-02-nested/blob/main/contracts/FeeSplitter.sol#L318

Tools Used

Manual review

Recommended Mitigation Steps

Consider caching length before the for loop.

uint256 shareholdersLength = shareholders.length
for (uint256 i; i < shareholdersLength; ++i) {
    ...
}

---------------------------------------------------------------------------

2 - Using pre-increment (++i) instead of post-increment (i++) can save gas

6 occurences in FeeSplitter.sol

9 occurences in NestedFactory.sol

1 occurences in NestedRecords.sol

3 occurences in OperatorResolver.sol

2 occurences in MixinOperatorResolver.sol

Tools Used

Manual review

Recommended Mitigation Steps

Change i++ to ++i

---------------------------------------------------------------------------

3 - Assigning default values costs unnecessary gas.

Especially present in for loop index assignment.

6 occurences in FeeSplitter.sol

9 occurences in NestedFactory.sol

1 occurences in NestedRecords.sol

3 occurences in OperatorResolver.sol

2 occurences in MixinOperatorResolver.sol

Tools Used

Manual review

Recommended Mitigation Steps

Remove default value assignments.

---------------------------------------------------------------------------

harleythedogC4 commented 2 years ago

1 and 2 already surfaced in the previous audit, they were disputed on #32 of this audit.

harleythedogC4 commented 2 years ago

My personal judgments:

  1. "Accessing array length in for loop can be optimized". Invalid.
  2. "Prefix vs postfix". Invalid.
  3. "Assigning default values costs unnecessary gas". This issue was already raised in some findings in the previous contest. Invalid.