Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #77

Open
code423n4 opened this issue Jun 18, 2022 · 1 comment
Open

Gas Optimizations #77

code423n4 opened this issue Jun 18, 2022 · 1 comment
Assignees
Labels

Comments

@code423n4
Copy link
Contributor

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

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 18, 2022
code423n4 added a commit that referenced this issue Jun 18, 2022
@obatirou obatirou self-assigned this Jun 21, 2022
@Yashiru
Copy link
Collaborator

Yashiru commented Jun 24, 2022

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

@Yashiru Yashiru added sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue duplicate This issue or pull request already exists and removed sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue labels Jun 24, 2022
@JeeberC4 JeeberC4 removed the duplicate This issue or pull request already exists label Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants