Gas Optimizations #108
Labels
bug
Something isn't working
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
C4-001 : Adding unchecked directive can save gas
Impact - Gas Optimization
Using the unchecked keyword to avoid redundant arithmetic underflow/overflow checks to save gas when an underflow/overflow cannot happen. E.g. 'unchecked' can be applied in the following lines of code since there are require statements before to ensure the arithmetic operations would not cause an integer underflow or overflow. 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.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Consider applying unchecked arithmetic where overflow/underflow is not possible.
C4-002 :
> 0 can be replaced with != 0 for gas optimization
Impact - Gas Optimization
!= 0
is a cheaper operation compared to> 0
, when dealing with uint.Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Use "!=0" instead of ">0" for the gas optimization.
C4-003 :
++i is more gas efficient than i++ in loops forwarding
Impact - Gas Optimization
++i is more gas efficient than i++ in loops forwarding.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
It is recommend to use unchecked{++i} and change i declaration to uint256.
C4-004 :
Cache array length in for loops can save gas
Impact - Gas Optimization
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Consider to cache array length.
C4-005 : Less than 256 uints are not gas efficient
Impact - Gas Optimization
Lower than uint256 size storage instance variables are actually less gas efficient. E.g. using uint16 does not give any efficiency, actually, it is the opposite as EVM operates on default of 256-bit values so uint16 is more expensive in this case as it needs a conversion. It only gives improvements in cases where you can pack variables together, e.g. structs.
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider to review all uint types. Change them with uint256 If the integer is not necessary to present with uint16.`
C4-006 : State variables could be declared constant
Impact - Gas Optimization
State variables that never change can be declared constant. This can greatly reduce gas costs.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Add the constant keyword for state variables whose value never change.
C4-007 : Immutable Variables
Impact - Gas Optimization
'immutable' greatly reduces gas costs. There are variables that do not change so they can be marked as immutable to greatly improve the gas costs.
Proof of Concept
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/ClearingHouse.sol#L23
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/ClearingHouse.sol#L24
Tools Used
Code Review
Recommended Mitigation Steps
Mark variables as immutable.
C4-008 : There is no need to assign default values to variables
Impact - Gas Optimization
When a variable is declared solidity assigns the default value. In case the contract assigns the value again, it costs extra gas.
Example: uint x = 0 costs more gas than uint x without having any different functionality.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
uint x = 0 costs more gas than uint x without having any different functionality.
C4-009 : Cache external call results can save gas
Impact
Every call to an external contract costs a decent amount of gas. For optimization of gas usage, external call results should be cached if they are being used for more than one time.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Cache external call for the gas optimization.
C4-010 : Redundant Import
Impact - Gas Optimization
Safemath is an unnecessary import in all contracts since it is used solely for development. It can therefore be removed.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Consider to delete redundant import.
C4-011 : Gas Optimization on the Public Functions
Impact
This does not directly impact the smart contract in anyway besides cost. This is a gas optimization to reduce cost of smart contract. Calling each function, we can see that the public function uses 496 gas, while the external function uses only 261.
Proof of Concept
According to Slither Analyzer documentation (https://github.com/crytic/slither/wiki/Detector-Documentation#public-function-that-could-be-declared-external), there are functions in the contract that are never called. These functions should be declared as external in order to save gas.
Slither Detector:
external-function:
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/VUSD.sol#L69
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/InsuranceFund.sol#L104
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/InsuranceFund.sol#L108
Tools Used
Slither
Recommended Mitigation Steps
C4-012 : Avoid unnecessary SafeCast.toInt256() can save gas
Impact
Gas Improvement
Proof of Concept
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/ClearingHouse.sol#L332
Tools Used
None
Recommended Mitigation Steps
Avoid unnecessary SafeCast.toInt256() can save gas
C4-013 : Use of _msgSender()
Impact
The use of _msgSender() when there is no implementation of a meta transaction mechanism that uses it, such as EIP-2771, very slightly increases gas consumption.
Proof of Concept
_msgSender() is utilized three times where msg.sender could have been used in the following function.
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/MarginAccount.sol#L115
https://github.com/code-423n4/2022-02-hubble/blob/8c157f519bc32e552f8cc832ecc75dc381faa91e/contracts/MarginAccount.sol#L140
Tools Used
None
Recommended Mitigation Steps
Replace _msgSender() with msg.sender if there is no mechanism to support meta-transactions like EIP-2771 implemented.
The text was updated successfully, but these errors were encountered: