Skip to content
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 #93

Open
code423n4 opened this issue Jun 3, 2022 · 1 comment
Open

Gas Optimizations #93

code423n4 opened this issue Jun 3, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

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

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 3, 2022
code423n4 added a commit that referenced this issue Jun 3, 2022
@chase-manning chase-manning added sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) labels Jun 6, 2022
@GalloDaSballo
Copy link
Collaborator

GalloDaSballo commented Jun 15, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants