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

Open
code423n4 opened this issue Apr 19, 2022 · 1 comment
Open

Gas Optimizations #7

code423n4 opened this issue Apr 19, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

1. Split up require statements instead of &&

Impact

Combining require statement conditions with && logic uses unnecessary gas. It is better to split up each part of the logical statement into separate require statements

Proof of Concept

Several instances of this issue was found
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ChainlinkPriceOracle.sol#L51
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ChainlinkPriceOracle.sol#L86
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L30-L31
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PriceOracle.sol#L46

Tools Used

Manual analysis

Recommended Mitigation Steps

Use separate require statements instead of concatenating with &&

2. Use prefix not postfix in loops

Impact

Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.

Proof of Concept

There are three examples of this
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L34
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L49

Tools Used

Manual analysis

Recommended Mitigation Steps

Use prefix not postfix to increment in a loop

3. Short require strings save gas

Impact

Strings in solidity are handled in 32 byte chunks. A require string longer than 32 bytes uses more gas. Shortening these strings will save gas.

Proof of Concept

Several cases of this gas optimization were found. These are a few examples, but more may exist

  1. https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L25
  2. https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TopNMarketCapIndex.sol#L74
  3. https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TopNMarketCapReweightingLogic.sol#L67

Tools Used

Manual analysis

Recommended Mitigation Steps

Shorten all require strings to less than 32 characters

4. Use != 0 instead of > 0

Impact

Using > 0 uses slightly more gas than using != 0. Use != 0 when comparing uint variables to zero, which cannot hold values below zero

Proof of Concept

Locations where this was found include
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L56
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L61
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L98
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L76
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L86
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L98
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L114
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L141
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L56
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L64

Tools Used

grep

Recommended Mitigation Steps

Replace > 0 with != 0 to save gas

5. Cache array length before loop

Impact

Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. This saves gas.

Proof of Concept

This optimization is already used in some places, but is not used in these places
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L39
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L60
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L125
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L38
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TopNMarketCapReweightingLogic.sol#L37
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndexReweightingLogic.sol#L37
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndexReweightingLogic.sol#L66

Tools Used

Manual analysis

Recommended Mitigation Steps

Cache the array length before the for loop

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 19, 2022
code423n4 added a commit that referenced this issue Apr 19, 2022
@jn-lp
Copy link
Collaborator

jn-lp commented May 3, 2022

Tips 2-5 were useful, thanks

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)
Projects
None yet
Development

No branches or pull requests

2 participants