Gas Optimizations #16
Labels
bug
Something isn't working
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Unnecessary equals boolean
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
Code instances:
Change transferFrom to transfer
'transferFrom(address(this), , **)' could be replaced by the following more gas efficient 'transfer(, **)'
This replacement is more gas efficient and improves the code quality.
Code instance:
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 instance:
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 instance:
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 instance:
Rearrange state variables
You can change the order of the storage variables to decrease memory uses.
Code instance:
In Operator.sol,rearranging the storage fields can optimize to: 11 slots from: 13 slots.
The new order of types (you choose the actual variables):
1. Status
2. Config
3. OlympusPrice
4. OlympusRange
5. OlympusTreasury
6. OlympusMinter
7. IBondAuctioneer
8. IBondCallback
9. ERC20
10. ERC20
11. uint32
12. uint8
13. uint8
14. bool
15. bool
Use bytes32 instead of string to save gas whenever possible
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 instance:
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:
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:
Operator.sol, 374 : You should cache the used power of 10 as constant state variable since it's used several times (2): uint256 oracleScale = 10**uint8(int8(PRICE.decimals()) - priceDecimals);
Operator.sol, 419 : You should cache the used power of 10 as constant state variable since it's used several times (2): uint256 invWallPrice = 10**(oracleDecimals * 2) / range.wall.low.price;
Operator.sol, 783 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10ohmDecimals * 10PRICE.decimals(),
Operator.sol, 784 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10**reserveDecimals * RANGE.price(true, true)
Operator.sol, 752 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10**reserveDecimals * RANGE.price(true, false),
Operator.sol, 764 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10**reserveDecimals * RANGE.price(true, true)
Operator.sol, 763 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10ohmDecimals * 10PRICE.decimals(),
Operator.sol, 753 : You should cache the used power of 10 as constant state variable since it's used several times (3): 10ohmDecimals * 10PRICE.decimals()
Operator.sol, 429 : You should cache the used power of 10 as constant state variable since it's used several times (2): uint256 oracleScale = 10**uint8(int8(oracleDecimals) - priceDecimals);
Operator.sol, 418 : You should cache the used power of 10 as constant state variable since it's used several times (2): uint256 invCushionPrice = 10**(oracleDecimals * 2) / range.cushion.low.price;
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 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: