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 #24

Open
code423n4 opened this issue May 31, 2022 · 1 comment
Open

Gas Optimizations #24

code423n4 opened this issue May 31, 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

Catching The Array Length Prior To Loop

Context: RoleManager.sol#L75-L88, RewardHandler.sol#L35-L55, StakerVault.sol#L256-L263, FeeBurner.sol#L43-L88, InflationManager.sol#L110-L125 (For L116), VestedEscrow.sol#L89-L111, PoolMigrationZap.sol#L20-L29, PoolMigrationZap.sol#L38-L45

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.

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.

In require(), Use != 0 Instead of > 0 With Uint Values

Context: BkdLocker.sol#L90-L100 (For both), BkdLocker.sol#L133-L155 (For L137), AmmGauge.sol#L103-L116 (For L104), AmmGauge.sol#L124-L138 (For L125), KeeperGauge.sol#L125-L144 (For L140), VestedEscrow.sol#L80-L87 (For L84)

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.

Recommendation:
Use != 0 instead of > 0 with uint values but only in require() statements.

Setting The Constructor To Payable

Context: All Contracts

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.

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.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 31, 2022
code423n4 added a commit that referenced this issue May 31, 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

Catching The Array Length Prior To Loop

Would save 3 gas (offset check)

3 * 8 = 24

In require(), Use != 0 Instead of > 0 With Uint Values

Valid for solidity below 0.8.13, saves 3 gas

3 * 7 = 21

Setting The Constructor To Payable

Appreciate the finding but because it's a one-off gas saving will not include in scoring

Function Ordering via Method ID

I really like this finding, had the warden spent the extra time to submit each optimized function they would have made a killing

Total Gas Saved:
45

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