-
Notifications
You must be signed in to change notification settings - Fork 1
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 #63
Comments
Unnecessary array boundaries check when loading an array element twice (Confirmed)Gas optimization confirmed |
State variables that could be set immutable (disputed)Code instances out of scope Unused state variables (disputed)Code instances out of scope Unused declared local variables (disputed)Code instances out of scope Use bytes32 instead of string to save gas whenever possible (disputed)Code instances out of scope Unnecessary cast (disputed)Code instances out of scope |
Empty else statement can be removed to save gas (Disputed)There is no empty else block in StakingLPVaultHelpers._addLiquidityAndDepositETH Empty else if statement can be removed to save gas (Disputed)There is no empty else if block in StakingLPVaultHelpers._addLiquidityAndDepositETH Inline one time use functions (Disputed)Writing these functions inline would considerably reduce the readability of the code. Indeed we would save deployment gas consumption if we do so, but we prefer to keep a good readability at the cost of more expensive smart contract deployment. |
Consider inline the following functions to save gas (Disputed)
|
Use unchecked to save gas for certain additive calculations that cannot overflow (Disputed)Can overflow, since the delay can be |
Short the following require messages (duplicated) |
Rearrange state variables (Disputed)The address |
Caching array length can save gas (Duplicated)Duplicated of #2 at For loop optimizaion Prefix increments are cheaper than postfix increments (Duplicated)Duplicated of #2 at For loop optimizaion Unnecessary index init (Duplicated)Duplicated of #2 at For loop optimizaion |
State variables that could be set immutable
In the following files there are state variables that could be set immutable to save gas.
Code instance:
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:
Unnecessary array boundaries check when loading an array element twice
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:
Rearrange state variables
You can change the order of the storage variables to decrease memory uses.
Code instance:
In OwnableProxyDelegation.sol,rearranging the storage fields can optimize to: 2 slots from: 3 slots.
The new order of types (you choose the actual variables):
1. bytes32
2. address
3. bool
Use bytes32 instead of string to save gas whenever possible
Code instances:
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 instances:
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 instance:
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 instance:
Empty else statement can be removed to save gas
Code instance:
Empty else if statement can be removed to save gas
Code instance:
Consider inline the following functions to save gas
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:
The text was updated successfully, but these errors were encountered: