-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14
Comments
More efficient Struct packing of Market in the contract ComptrollerStorage.solYes but in lack of POC of gas saved for end users I cannot give it any points Unnecessary equals booleanThe list provided doesn't match the statement State variables that could be set immutableFinding is valid, in lack of any further details will give each variable a saved SLOAD Unused state variablesValid for the errors Unused declared local variablesThey are used in the value Caching array length can save gasValid 3 gas per instance Prefix increments are cheaper than postfix increments5 gas Unnecessary default assignmentValid for 2 assignments, that said these are deployment costs Rearrange state variablesIn lack of POC I cannot give it points ## Use bytes32 instead of string to save gas whenever possible Short the following require messagesSaves 6 gas Use != 0 instead of > 0Valid for 4 instances, 3 gas each |
Use unchecked to save gas for certain additive calculations that cannot overflow20 per instance * 5 Consider inline the following functions to save gasDisagree as the optimizer takes cases Inline one time use functionsAgreed for Would save 2 jumps (8 gas each) Cache powers of 10 used several timesDisagree as they are calculated once and at the time of need, with variable inputs Change if -> revert pattern to requireAgree but in lack of math I can't quantify Do not cache msg.senderSaves 1 gas Total Gas Saved |
In reviewing, this is the only report that found 4 storage -> immutable gas improvements which ultimately save the most gas. My advice for most wardens is not to spam submission but just focus on finding those big gas savings, which despite all the unactionable findings, ultimately this report was the only one that did |
@GalloDaSballo LpToken.sol and VaultReserve.sol are not in scope, so three out of the four storage->immutable are invalid, and therefore #108 saves more gas |
Thank you @IllIllI000 looking into it |
I've confirmed that LpToken and VaultReserve are outside of scope, will have to recalculate gas savings for this report |
Removing |
My math was off, as one of the 2303 gas saved |
More efficient Struct packing of Market in the contract ComptrollerStorage.sol
In ComptrollerStorage.sol, Market is optimized to: 3 slots from: 4 slots.
The new order of types (you choose the actual variables):
1. uint256
2. mapping
3. bool
4. bool
Unnecessary equals boolean
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 instance:
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 VestedEscrow.sol,rearranging the storage fields can optimize to: 8 slots from: 9 slots.
The new order of types (you choose the actual variables):
1. IERC20
2. uint256
3. uint256
4. uint256
5. uint256
6. uint256
7. address
8. bool
9. address
In KeeperGauge.sol,rearranging the storage fields can optimize to: 3 slots from: 4 slots.
The new order of types (you choose the actual variables):
1. IController
2. uint256
3. address
4. uint48
5. 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 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:
Cache powers of 10 used several times
You calculate the power of 10 every time you use it instead of caching it once as a constant variable and using it instead.
Fix the following code lines:
Code instances:
DecimalScale.sol, 22 : You should cache the used power of 10 as constant state variable since it's used several times (4): return value / 10**(_DECIMALS - decimals);
DecimalScale.sol, 12 : You should cache the used power of 10 as constant state variable since it's used several times (4): return value * 10**(_DECIMALS - decimals);
DecimalScale.sol, 20 : You should cache the used power of 10 as constant state variable since it's used several times (4): return value * 10**(decimals - _DECIMALS);
DecimalScale.sol, 10 : You should cache the used power of 10 as constant state variable since it's used several times (4): return value / 10**(decimals - _DECIMALS);
Change if -> revert pattern to require
Change if -> revert pattern to 'require' to save gas and improve code quality,
if (some_condition) {
revert(revert_message)
}
to: require(!some_condition, revert_message)
In the following locations:
Code instance:
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 instance:
The text was updated successfully, but these errors were encountered: