Gas Optimizations #102
Labels
bug
Something isn't working
duplicate
This issue or pull request already exists
G (Gas Optimization)
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
[S] Duplicated
assert
statementshttps://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/curve-v2/Swap.vy#L666-L671
L669 and L671 are duplicated.
[S] Outdated versions of OpenZeppelin library
Outdated versions of OpenZeppelin library are used.
It's a best practice to use the latest version of libraries.
New versions of OpenZeppelin libraries can be more gas effeicant.
For exmaple:
ERC20Upgradeable.sol
in @openzeppelin/contracts-upgradeable@4.3.2:https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.3.2/contracts/token/ERC20/ERC20Upgradeable.sol#L155-L169
A gas optimization upgrade has been added to @openzeppelin/contracts@4.5.0:
https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.5.0/contracts/token/ERC20/ERC20Upgradeable.sol#L163-L172
https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.5.0/contracts/token/ERC20/ERC20Upgradeable.sol#L335-L347
transferFrom
when allowance istype(uint256).max
. (#3085)[S] Avoid unnecessary storage reads in for loops can save gas
For the storage variables that will be accessed multiple times, especially in loops, cache and read from the stack can save ~100 gas from each extra read (
SLOAD
after Berlin).For example:
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/VUSD.sol#L53-L67
start
andmaxWithdrawalProcesses
can be cached.[M] Adding unchecked directive can save gas
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
For example:
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/MarginAccount.sol#L572-L579
credit -= toBurn
will never overflow.https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/MarginAccount.sol#L581-L593
uint _credit = amount - bal
will never overflow.[S] Using immutable variable can save gas
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/MarginAccountHelper.sol#L15-L26
Considering that
marginAccount
,vusd
andreserveToken
will never change, changing them to immutable variables instead of storages variable can save gas.The text was updated successfully, but these errors were encountered: