Gas Optimizations #60
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")
Using memory copies of storage variables when repeated access is required uses less gas
credit
should be copiedhttps://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/MarginAccount.sol#L574-L576
Functions not called by the contract itself should be
external
rather thanpublic
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L387
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L148
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L153
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L116
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L509
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L599
require()
strings longer than 32 bytes cost extra gashttps://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L487
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L511
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L174
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L354
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L453
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/VUSD.sol#L55
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L84
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L101
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/ERC20Mintable.sol#L32
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/ERC20Mintable.sol#L60
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/TestOracle.sol#L20
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/TestOracle.sol#L33
Using
> 0
costs more gas than!= 0
when used in on uints in arequire()
statementhttps://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L150
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/Oracle.sol#L153
Splitting
require()
statements that use&&
saves gasSee this issue for an example
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol
It costs more gas to initialize variables to zero than to let the default of zero be applied
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L31
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L331
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L521
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L552
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccountHelper.sol#L13
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#L251
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
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L52
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L15
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L52
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L78
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L95
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L120
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L137
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L9
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L17
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L18
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L21
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L27
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L32
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L43
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L45
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Executor.sol#L19
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L25
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L31
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L37
<array>.length
should not be looked up in every loop of a for-loopEven memory arrays incur the overhead of bit tests and bit shifts to calculate the array length
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L331
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L373
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L521
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L552
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#L251
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
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L78
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L367
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L9
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L18
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L21
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L27
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L32
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Executor.sol#L19
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L25
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L31
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L37
++i
/i++
should beunchecked{++i}
/unchecked{++i}
when it is not possible for them to overflow, as is the case when used in for- and while-loopshttps://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L331
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L373
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L521
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L552
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#L251
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
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L52
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L78
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L95
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L120
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L137
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L367
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L9
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L18
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L21
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L27
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L32
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L43
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L45
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Executor.sol#L19
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L25
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L31
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L37
++i
costs less gas than++i
, especially when it's used in for-loops (--i
/i--
too)https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L331
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L373
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L521
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L552
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#L251
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
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L52
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L78
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L95
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L120
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L137
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/HubbleViewer.sol#L367
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L9
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L18
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L21
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L27
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Disperse.sol#L32
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L43
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Leaderboard.sol#L45
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/Executor.sol#L19
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L25
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L31
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/tests/BatchLiquidator.sol#L37
The text was updated successfully, but these errors were encountered: