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

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

Gas Optimizations #82

code423n4 opened this issue May 2, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Use ++index instead of index++ to increment a loop counter

Context: DexAddressProvider.sol#L13-L19, AdminInceptionVault.sol#L107-L114,

Description:
Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation:
Use ++index to increment a loop counter.

Catching The Array Length Prior To Loop

Context: DexAddressProvider.sol#L13-L19, AdminInceptionVault.sol#L107-L114,

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: InceptionVaultsCore.sol#L121-L126, ChainlinkInceptionPriceFeed.sol#L73-L85 (For L75 & L79), GenericMinerV2.sol#L54-L62 (For L58), GenericMinerV2.sol#L69-L74, GenericMinerV2.sol#L174-L186, GenericMinerV2.sol#L194-L207 (For L195), PARMinerV2.sol#L45-L63 (For L52), PARMinerV2.sol#L69-L74, PARMinerV2.sol#L70-L75, PARMinerV2.sol#L253-L275, PARMinerV2.sol#L284-L298 (For L284), GUniLPOracle.sol#L91-L125 (For L112),

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.

Use calldata Instead of memory For Function Parameters

Context: DemandMinerV2.sol#L56-L59,

Description:
The dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient.

Recommendation:
Use calldata instead of memory for function parameters to avoid using memory with array values whena function is getting called externally.

uint256 Is Cheaper Than uint8

Context: AdminInceptionVault.sol#L26, InceptionVaultFactory.sol#L31, BalancerV2LPOracle.sol#L19, GUniLPOracle.sol#L17,

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 of uint8.

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.

Upgrade To At Least 0.8.4

Context: All Contracts

Description:
Using newer compiler versions and the optimizer gives gas
optimizations and additional safety checks for free!

The advantages of versions =0.8.*= over =<0.8.0= are:

  • Safemath by default from =0.8.0= (can be more gas efficient than /some/
    library based safemath).
  • Low level inliner from =0.8.2=, leads to cheaper runtime gas.
    Especially relevant when the contract has small functions. For
    example, OpenZeppelin libraries typically have a lot of small
    helper functions and if they are not inlined, they cost an
    additional 20 to 40 gas because of 2 extra =jump= instructions and
    additional stack operations needed for function calls.
  • Optimizer improvements in packed structs: Before =0.8.3=, storing
    packed structs, in some cases used an additional storage read
    operation. After EIP-2929, if the slot was already cold, this
    means unnecessary stack operations and extra deploy time costs.
    However, if the slot was already warm, this means additional cost
    of =100= gas alongside the same unnecessary stack operations and
    extra deploy time costs.
  • Custom errors from =0.8.4=, leads to cheaper deploy time cost and
    run time cost. Note: the run time cost is only relevant when the
    revert condition is met. In short, replace revert strings by
    custom errors.

Recommendation:
Upgrade to at least 0.8.4 for the additional benefits.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 2, 2022
code423n4 added a commit that referenced this issue May 2, 2022
@m19
Copy link
Collaborator

m19 commented May 8, 2022

We found this issue really helpful for gas optimizations

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)
Projects
None yet
Development

No branches or pull requests

2 participants