Gas Optimizations #79
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")
Hubble contest Gas Optimization
1 use initial value to save gas for uint.
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L52
uint shares;
2 use cache for array length to save gas.
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L122
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L130
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L170
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L194
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L263
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L277
3 Delete unused variable. It seems that the following variable will be not used in this construct and other contracts. If it is not used, you can delete it to save gas.
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L22
4 Avoid extra mstore.
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L121
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L141
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L614
isNewPosition and isLongPosition are used only one time in
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L123
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L143
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L616
You can use position.size == 0 and position.size > 0 directly there to save gas.
if (position.size == 0 || (position.size > 0 ? Side.LONG : Side.SHORT) == side) {
if (position.size > 0) {
5 Use msg.sender instead of _msgSender() to save gas.
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L69
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L98
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L113
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L214
6 Delete unused import statements in AMM.sol
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L5
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L9
The text was updated successfully, but these errors were encountered: