Gas Optimizations #44
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
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
Code instances:
State variables that could be set immutable
In the following files there are state variables that could be set immutable to save gas.
Code instances:
Unused state variables
Unused state variables are gas consuming at deployment (since they are located in storage) and are
a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.
Code instances:
Unused declared local variables
Unused local variables are gas consuming, since the initial value assignment costs gas. And are
a bad code practice. Removing those variables will decrease the gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.
Code instances:
Caching array length can save gas
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:
to:
Code instances:
Prefix increments are cheaper than postfix increments
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the
++i
infor (uint256 i = 0; i < numIterations; ++i)
).But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (
++x
) or not using the unchecked keyword:Code instances:
Unnecessary index init
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas.
It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
Code instances:
Storage double reading. Could save SLOAD
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the
overall gas uses. The following is a list of functions and the storage variables that you read twice:
Code instances:
Unnecessary default assignment
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
Code instances:
Rearrange state variables
You can change the order of the storage variables to decrease memory uses.
Code instances:
In CrvDepositor.sol,rearranging the storage fields can optimize to: 12 slots from: 13 slots.
The new order of types (you choose the actual variables):
1. uint256
2. uint256
3. uint256
4. uint256
5. uint256
6. uint256
7. address
8. bool
9. address
10. address
11. address
12. address
13. address
In BoosterOwner.sol,rearranging the storage fields can optimize to: 8 slots from: 9 slots.
The new order of types (you choose the actual variables):
1. uint256
2. uint256
3. address
4. bool
5. bool
6. address
7. address
8. address
9. address
10. address
In AuraVestedEscrow.sol,rearranging the storage fields can optimize to: 6 slots from: 7 slots.
The new order of types (you choose the actual variables):
1. IERC20
2. IAuraLocker
3. uint256
4. uint256
5. uint256
6. address
7. bool
In AuraLocker.sol,rearranging the storage fields can optimize to: 13 slots from: 15 slots.
The new order of types (you choose the actual variables):
1. uint256
2. uint256
3. uint256
4. uint256
5. uint256
6. IERC20
7. uint256
8. uint256
9. uint256
10. string
11. string
12. address
13. uint8
14. bool
15. address
Short the following require messages
The following require messages are of length more than 32 and we think are short enough to short
them into exactly 32 characters such that it will be placed in one slot of memory and the require
function will cost less gas.
The list:
Code instance:
Use != 0 instead of > 0
Using != 0 is slightly cheaper than > 0. (see code-423n4/2021-12-maple-findings#75 for similar issue)
Code instances:
Unnecessary cast
Code instances:
Use unchecked to save gas for certain additive calculations that cannot overflow
You can use unchecked in the following calculations since there is no risk to overflow:
Code instances:
Consider inline the following functions to save gas
Code instances
Inline one time use functions
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
Code instances:
Upgrade pragma to at least 0.8.4
Using newer compiler versions and the optimizer gives gas optimizations
and additional safety checks are available for free.
The advantages of versions 0.8.* over <0.8.0 are:
Code instances:
Do not cache msg.sender
We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.
Code instances:
The text was updated successfully, but these errors were encountered: