code-423n4 / 2022-06-canto-findings

0 stars 0 forks source link

Gas Optimizations #273

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Report

\<VAR>++ OR \<VAR> += 1 TO ++\<VAR> ALSO USE UNCHECKED FOR OPERATIONS NOT EXPECTED TO OVERFLOW

When the value of the post-loop increment/decrement is not stored or used in any calculations, the prefix increment/decrement operators (++i/--i) cost less gas PER LOOP than the postfix increment/decrement operators (i++/i--)

Use ++i rather than i++ to save gas

Instances include

i++ to ++i:

i++ to ++i and add unchecked:

CACHING LENGTH ON FOR

Cache the lenth in the for loops

zeroswap/contracts/uniswapv2/UniswapV2Oracle.sol: L83 cache the granularity

zeroswap/contracts/uniswapv2/UniswapV2Router02.sol: L214, L323 cache the path.lenght-1

zeroswap/contracts/mocks/ComplexRewarderTime.sol: L157 zeroswap/contracts/uniswapv2/UniswapV2Oracle.sol: L83

State variables can be constant;

MasterChef.sol

MasterChef.sol#L59 MasterChef.sol#L63 MasterChef.sol#L65 MasterChef.sol#L77

Save gas avoiding initialize variables with 0

MasterChef.sol#L75 This will save you gas

uint256 public totalAllocPoint;

:

GalloDaSballo commented 2 years ago

Run time gas saved is less than 500