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

Open
code423n4 opened this issue Jun 18, 2022 · 5 comments
Open

Gas Optimizations #68

code423n4 opened this issue Jun 18, 2022 · 5 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") valid

Comments

@code423n4
Copy link
Contributor

  1. The require statements could be put at the beginning part of a block of statements if it doesn’t affect the logic to save gas.
    a. require(_orders.length != 0, "NF: INVALID_ORDERS");

  2. Initializing i to 0 inside a for loop is redundant. As its initialized to 0 by default.
    Also i++ or ++I in the for loops can be put inside an unchecked block to save gas.

a. for (uint256 i = 0; i < operatorsCache.length; i++)
b. for (uint256 i = 0; i < operatorsLength; i++)
c. for (uint256 i = 0; i < batchedOrdersLength; i++)
d. for (uint256 i = 0; i < tokensLength; i++)
e. for (uint256 i = 0; i < batchedOrdersLength; i++)
f. for (uint256 i = 0; i < batchedOrdersLength; i++)
g. for (uint256 i = 0; i < batchLength; i++)
h. for (uint256 i = 0; i < batchLength; i++)
i. for (uint256 i = 0; i < _batchedOrders.length; i++)
j. for (uint256 i = 0; i < namesLength; i++)
k. for (uint256 i = 0; i < names.length; i++)
l. for (uint256 i = 0; i < destinations.length; i++)
m. for (uint256 i = 0; i < requiredOperators.length; i++)
n. for (uint256 i = 0; i < requiredOperators.length; i++)
o. for (uint256 i = 0; i < targets.length; ++i)
p. for (uint256 i = 0; i < targets.length; ++i)

  1. Unless it was introduced for readability saving memory to memory is redundant.
    address token = tokens[i];

  2. These arithmetic operations can be unchecked.
    a. uint256 halfInvestment = investmentA / 2;
    b. uint256 halfInvestment = investmentA / 2;

  3. This line could be pre-computed and defined as a constant to save gas.
    a. bytes4(keccak256(bytes("remove_liquidity_one_coin(uint256,int128,uint256)")))
    b. bytes4(keccak256(bytes("remove_liquidity_one_coin(uint256,uint256,uint256)")))

@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 22, 2022

1. The require statements could be put at the beginning part of a block [...] (Confirmed)

Gas optimization confirmed

@Yashiru
Copy link
Collaborator

Yashiru commented Jun 22, 2022

3. Unless it was introduced for readability saving memory to memory is redundant (Confirmed)

Gas optimization confirmed

@maximebrugel
Copy link
Collaborator

maximebrugel commented Jun 24, 2022

4. These arithmetic operations can be unchecked (Confirmed)

Gas optimization confirmed

@Yashiru
Copy link
Collaborator

Yashiru commented Jun 24, 2022

2. Initializing i to 0 inside a for loop is redundant. As its initialized to 0 by default (Duplicated)

Duplicated of #2 at For loop optimizaion

@obatirou
Copy link
Collaborator

5. This line could be pre-computed and defined as a constant to save gas. (confirmed)

Gas optimization confirmed

@obatirou obatirou added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jun 27, 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 G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") valid
Projects
None yet
Development

No branches or pull requests

5 participants