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 #99

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

Gas Optimizations #99

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

Comments

@code423n4
Copy link
Contributor

[Gas-1] Potential usage of unchecked

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

  • The increment of i in the for loop

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

  • operators[operatorsLength - 1] can be wrapped by unchecked

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

@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 20, 2022
@Yashiru
Copy link
Collaborator

Yashiru commented Jun 24, 2022

[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

@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