code-423n4 / 2022-05-backd-findings

0 stars 0 forks source link

Gas Optimizations #93

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas optimizations

Use unchecked lib to increment safe variables

PoolMigrationZap.sol

You can add the unchecked math lib like you do on the others contracts to safely increment the i var, saving gas and make contract consistent with the others by enforcing the same style.

Recommendation, add using UncheckedMath for uint256 And on line PoolMigrationZap.sol#L22 change;

for (uint256 i; i < newPools_.length; ++i) {

To

for (uint256 i; i < newPools_.length; i = i.uncheckedInc()) {

TopUpKeeperHelper.sol

On line TopUpKeeperHelper.sol#L52 you coul use unchecked math lib to increment the variable. Change:

topupsAdded++;

To:

topupsAdded = topupsAdded.uncheckedInc();

KeeperGauge.sol

On lines #L98 and L59 you could use unchecked math lib, consider change;

epoch++;

To;

epoch = epoch.uncheckedInc();

Use unchecked for decrement i

On BkdLocker.sol#L140 you can do a unchecked decrement (or add a function to the Unchecked math to do it) change;

i = i - 1;

to;

unchecked { --i; }

Cache .length

You could cache lenght of arrays to save gas; RewardHandler.sol#L41-L42 StakerVault.sol#L259 VestedEscrow.sol#L94

Use require(foo != 0) instead of require(foo > 0)

>0 is less gas efficient than !0 if you enable the optimizer at 10k AND you’re in a require statement.

BkdLocker.sol#L91 BkdLocker.sol#L92 BkdLocker.sol#L137 VestedEscrow.sol#L84 KeeperGauge.sol#L140 AmmGauge.sol#L104 AmmGauge.sol#L125

GalloDaSballo commented 2 years ago

Use unchecked lib to increment safe variables Agree, this should save about 17 gas (20 gas minus the cost of the intermediary result, 3 gas) 3 * 17 = 51

Use unchecked for decrement i Agree, this should save around 25 gas (20 for unchecked + 5 for pre-decrement)

Cache .length Saves the cost of computing the offset, 3 gas 3*3 = 9

Use require(foo != 0) instead of require(foo > 0) Valid until 0.8.13, saves 3 gas per instance only with optimizer 7 * 3 = 21

Total Gas Saved 83