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

Open
code423n4 opened this issue May 30, 2022 · 2 comments
Open

Gas Optimizations #131

code423n4 opened this issue May 30, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

1. Using Prefix (++i) rather than postfix (i++) in increment/decrement operators in for-loops

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/RewardsDistributor.sol#L195

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L143

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L147

impact

using the prefix increment/decrement operators (++i/--i) cost less gas PER LOOP than the postfix increment/decrement operators (i++/i--)

2. Setting uint variables to 0 is redundant

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Pair.sol#L20

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Pair.sol#L61

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Pair.sol#L62

impact

Setting uint variables to 0 is redundant as they default to 0.

3. For-Loops: Increments can be unchecked

POC

Examples of this issue in the codebase:
https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/RewardsDistributor.sol#L75

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/RewardsDistributor.sol#L148

https://github.com/code-423n4/2022-05-aura/blob/a8758161373bc9c9ad2aec363b511afa3ed0613f/contracts/AuraClaimZap.sol#L151

impact

In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.

4. Avoiding initialization of loop index can save a little gas

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Gauge.sol#L353

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Minter.sol#L57

impact

The local variable used for the loop index need not be initialized to 0 because the default value is 0. Avoiding this anti-pattern can save a few opcodes and therefore a tiny bit of gas.

5. Using Prefix (++i) rather than postfix (i++) in increment/decrement operators in for-loops

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/RewardsDistributor.sol#L195

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L143

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L147

impact

using the prefix increment/decrement operators (++i/--i) cost less gas PER LOOP than the postfix increment/decrement operators (i++/i--)

6. Use Custom Errors instead of Revert Strings to save Gas

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Bribe.sol#L44

https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Bribe.sol#L69

impact

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met).

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

7. Storage

POC

Examples of this issue in the codebase:

here, isReward[token] can be cached as a memory to save gas
https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Bribe.sol#L43
https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Bribe.sol#L53

impact

The code can be optimized by minimizing the number of SLOADs. SLOADs are expensive (100 gas) compared to MLOADs/MSTOREs (3 gas).

8. Use calldata instead of memory

POC

Examples of this issue in the codebase:
_tokens can be changed to calldata
https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L74

_weights can be changed to calldata
https://github.com/code-423n4/2022-05-velodrome/blob/731a7c438c4f93efc8310586d217006930be63fd/contracts/contracts/Voter.sol#L135

impact

when the parameter of a function is not going to be changed, it's cheaper to use calldata than memory

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 30, 2022
code423n4 added a commit that referenced this issue May 30, 2022
@pooltypes
Copy link
Collaborator

These are high quality but we decided to forgo many as gas optimization is not a huge advantage on Optimism

@pooltypes pooltypes added the sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons label Jun 10, 2022
@GalloDaSballo
Copy link
Collaborator

Would save between 500 and 2k gas

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 acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

3 participants