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

Caching array length can save gas #22

Closed
code423n4 opened this issue Dec 14, 2021 · 2 comments
Closed

Caching array length can save gas #22

code423n4 opened this issue Dec 14, 2021 · 2 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

robee

Vulnerability details

Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory
We recommend to change from:
for (uint256 i=0; i<array.length; i++) { ... }
to:
uint len = array.length
for (uint256 i=0; i<len; i++) { ... }
These functions use not using prefix increments (++x) or not using the unchecked keyword:

    RebalanceManager.sol, _swapsV2, 218
    RebalanceManager.sol, _swapsV3, 234
    RebalanceManagerV2.sol, _swapsV2, 155
    RebalanceManagerV3.sol, _swapsV2, 166
    BasketFacet.sol, tokens.bs, 50
    BasketFacet.sol, tokens.bs, 160
    BasketFacet.sol, tokens.bs, 202
    BasketFacet.sol, tokens, 321
    BasketFacet.sol, tokens.bs, 348
    BasketFacet.sol, tokens.bs, 381
    CallFacet.sol, callers.callStorage, 55
    CallFacet.sol, _targets, 82
    CallFacet.sol, _targets, 95
    PieFactoryContract.sol, _tokens, 88
    SingleNativeTokenExit.sol, tokens, 69
    SingleNativeTokenExitV2.sol, trades._exitTokenStruct, 74
    SingleTokenJoin.sol, tokens, 108
    SingleTokenJoinV2.sol, trades._joinTokenStruct, 86
    SingleTokenJoinV2.sol, tokens, 117
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 14, 2021
code423n4 added a commit that referenced this issue Dec 14, 2021
@ghost ghost added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 30, 2021
@loki-sama loki-sama added the duplicate This issue or pull request already exists label Jan 11, 2022
@loki-sama
Copy link
Collaborator

duplicate #288

@0xleastwood
Copy link
Collaborator

Duplicate of #249

@0xleastwood 0xleastwood marked this as a duplicate of #249 Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants