Description:
One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.
NOTES: _updatedAssets Can be catched before the requrie statement since it is also read there.
Recommendation:
Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.
Description:
Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.
Recommendation:
Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.
Use ++index instead of index++ to increment a loop counter
Description:
You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.
Description:
In a require, when checking a uint, using != 0 instead of > 0 saves 6 gas. This will jump over or avoid an extra ISZERO opcode. But this only works in requrie and not in other situations hence it is cheaper to use > 0 instead of !=0 with uint values in an if().
Recommendation:
Use != 0 instead of > 0 with uint values but only in require() statements and use > 0 instead of != 0 in other situations.
In
require()
, Use!= 0
Instead of> 0
With Uint ValuesContext:
NAV.sol#L36-L51
,NAV.sol#L56-L61
,IndexLogic.sol#L31-L92
,IndexLogic.sol#L96-L147
Description: In a require, when checking a uint, using
!= 0
instead of> 0
saves 6 gas. This will jump over or avoid an extraISZERO
opcode.Recommendation: Use
!= 0
instead of> 0
with uint values but only inrequire()
statements.Catching The Array Length Prior To Loop
Context:
BaseIndex.sol#L75-L81
,IndexLogic.sol#L31-L92 (For both loops)
,IndexLogic.sol#L96-L147 (For loop at L125-L144)
,ManagedIndex.sol#L27-L39
,ManagedIndexReweightingLogic.sol#L28-L105 (For all 3 loops read additonal notes)
,TopNMarketCapIndex.sol#L37-L65
,TopNMarketCapReweightingLogic.sol#L30-L113 (For all 3 loops)
,TrackedIndex.sol#L25-L54
,TrackedIndexReweightingLogic.sol#L28-L80 (For both loops)
,UniswapV2PathPriceOracle.sol#L32-L44
,UniswapV2PathPriceOracle.sol#L47-L59
Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables. NOTES:
_updatedAssets
Can be catched before the requrie statement since it is also read there.Recommendation: Simply do something like so before the for loop:
uint length = variable.length
. Then addlength
in place ofvariable.length
in the for loop.Function Ordering via Method ID
Context:
All Contracts
Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use
This tool
to help find alternative function names with lower Method IDs while keeping the original name intact.Recommendation: Find a lower method ID name for the most called functions for example
mostCalled()
vs.mostCalled_41q()
is cheaper by 44 gas.Use
++index
instead ofindex++
to increment a loop counterContext:
BaseIndex.sol#L75-L81
,UniswapV2PathPriceOracle.sol#L32-L44
,UniswapV2PathPriceOracle.sol#L47-L59
Description: Due to reduced stack operations, using
++index
saves 5 gas per iteration.Recommendation: Use
++index
to increment a loop counter.Setting The Constructor To Payable
Context:
BaseIndex.sol#L33-L40
,ChainlinkPriceOracle.sol#L46-L57
,UniswapV2PathPriceOracle.sol#L23-L29
Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of
msg.value == 0
and saves 21 gas on deployment with no security risks.Recommendation: Set the constructor to payable.
In
if()
, Use> 0
Instead of!=0
With Uint ValuesContext:
NAV.sol#L36-L51
,NAV.sol#L77-L88
,NAV.sol#L94-L101
,IndexLogic.sol#L31-L92
Description: In a require, when checking a uint, using
!= 0
instead of> 0
saves 6 gas. This will jump over or avoid an extraISZERO
opcode. But this only works in requrie and not in other situations hence it is cheaper to use> 0
instead of!=0
with uint values in anif()
.Recommendation: Use
!= 0
instead of> 0
with uint values but only inrequire()
statements and use> 0
instead of!= 0
in other situations.uint256
Is Cheaper Thanuint8
Context:
ChainlinkPriceOracle.sol#L38
,ChainlinkPriceOracle.sol#L41
,IndexLayout.sol#L27
,PhuturePriceOracle.sol#L33
,TopNMarketCapIndex.sol#L21
Description: The EVM reads in 32 byte words if your data is smaller, further operations are needed to downscale from 256 bits to 8 bit.
Recommendation: use
uint256
instead ofuint8
.