Gas Optimizations #135
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")
Gas Report
Table of Contents:
calldata
instead ofmemory
forstring _name
positions[trader]
in memorypositions[trader]
in memorypositions[maker]
in memorymakers[maker]
in memorypositions[trader]
in memorymakers[trader]
in memorypositions[trader]
in memorymakers[trader]
in memorypositions[trader]
in memorypositions[trader]
in memorypositions[trader]
in memoryreserveSnapshots[snapshotIndex]
in memoryreserveSnapshots.length
in memorysupportedCollateral[idx]
in memorystart
in memory> 0
is less efficient than!= 0
for unsigned integers (with proof)++i
costs less gas compared toi++
Foreword
@audit
tagsSummary
File: AMM.sol
function initialize()
Use
calldata
instead ofmemory
forstring _name
An external function passing a readonly variable should mark it as
calldata
and notmemory
function openPosition()
Do not cache
positions[trader]
in memoryAs a copy in memory of a struct makes as many SLOADs as there are slots, here a copy costs 3 SLOADs:
However, only the
size
field is read twice. Therefore, only this field should get cached:int256 _size = positions[trader].size;
function liquidatePosition()
Do not cache
positions[trader]
in memorySimilar to Do not cache
positions[trader]
in memory.However, only the
size
field is read 3 times. Therefore, only this field should get cached:int256 _size = positions[trader].size;
function removeLiquidity()
Do not cache
positions[maker]
in memorySimilar to Do not cache
positions[trader]
in memory.However, here, even the fields shouldn't get cached, as they are read only once:
Therefore, use
220: Position storage _taker = positions[maker];
Do not cache
makers[maker]
in memorySimilarly, a copy in memory for
Maker
costs 7 SLOADs:Here, caching the first 5 fields in memory is enough.
function getNotionalPositionAndUnrealizedPnl()
Do not cache
positions[trader]
in memoryHere, we need
Position storage _taker = positions[trader];
Do not cache
makers[trader]
in memoryHere, we need
Maker storage _maker = makers[trader];
function getPendingFundingPayment()
Do not cache
positions[trader]
in memoryHere, we need
Position storage _taker = positions[trader];
Do not cache
makers[trader]
in memoryHere, we need
Maker storage _maker = makers[trader];
function getTakerNotionalPositionAndUnrealizedPnl()
Do not cache
positions[trader]
in memoryHere, we need to cache these fields:
size
andopenNotional
function _emitPositionChanged()
Do not cache
positions[trader]
in memoryHere, we need
Position storage _taker = positions[trader];
function _openReversePosition()
Do not cache
positions[trader]
in memoryHere, we need to cache the
size
fieldUnchecked block L597
This line can't underflow due to the condition L596. Therefore, it should be wrapped in an
unchecked
blockfunction _calcTwap()
Do not cache
reserveSnapshots[snapshotIndex]
in memoryHere, we need to cache the
timestamp
field. Copying the struct in memory costs 3 SLOADs.Cache
reserveSnapshots.length
in memoryThis would save 1 SLOAD
Unchecked block L684
This line can't underflow due to the condition L680-L682. Therefore, it should be wrapped in an
unchecked
blockUse the cache for calculation
As we already have
currentSnapshot = reserveSnapshots[snapshotIndex];
: use it here:currentPrice = currentSnapshot.lastPrice;
File: ClearingHouse.sol
function _disperseLiquidationFee()
Unchecked block L214
This line can't underflow due to the condition L212. Therefore, it should be wrapped in an
unchecked
blockFile: InsuranceFund.sol
function pricePerShare()
Unchecked block L97
This line can't underflow for obvious mathematical reasons (
_balance
substracting at most itself). Therefore, it should be wrapped in anunchecked
blockFile: Oracle.sol
function getUnderlyingTwapPrice()
Unchecked block L81
This line can't underflow due to L76-L79. Therefore, it should be wrapped in an
unchecked
blockFile: Interfaces.sol
struct Collateral
Tight packing structs to save slots
While this file is out of scope, it deeply impacts MarginAccount.sol.
I suggest going from:
to
To save 1 slot per array element in MarginAccount.sol's storage
File: MarginAccount.sol
function _getLiquidationInfo()
Do not cache
supportedCollateral[idx]
in memoryHere, we need
Collateral storage coll = supportedCollateral[idx];
. Copying the struct in memory costs 3 SLOADs.function _transferOutVusd()
Unchecked block L588
This line can't underflow due to L583. Therefore, it should be wrapped in an
unchecked
blockFile: VUSD.sol
function processWithdrawals()
Unchecked block L57-L65
The whole while-loop can't underflow. Therefore, it should be wrapped in an
unchecked
blockCache
start
in memoryCache
start
in memory asinitialStart
and use it L55 + L57 (comparei
to it in the while-loop)General recommendations
Variables
No need to explicitly initialize variables with default values
If a variable is not set/initialized, it is assumed to have the default value (
0
foruint
,false
forbool
,address(0)
for address...). Explicitly initializing it with its default value is an anti-pattern and wastes gas.As an example:
for (uint256 i = 0; i < numIterations; ++i) {
should be replaced withfor (uint256 i; i < numIterations; ++i) {
Instances include:
I suggest removing explicit initializations for default values.
Pre-increments cost less gas compared to post-increments
Comparisons
> 0
is less efficient than!= 0
for unsigned integers (with proof)!= 0
costs less gas compared to> 0
for unsigned integers inrequire
statements with the optimizer enabled (6 gas)Proof: While it may seem that
> 0
is cheaper than!=
, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND you're in arequire
statement, this will save gas. You can see this tweet for more proofs: https://twitter.com/gzeon/status/1485428085885640706> 0
in require statements are used in the following location(s):I suggest you change
> 0
with!= 0
in require statements. Also, enable the Optimizer.For-Loops
An array's length should be cached to save gas in for-loops
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.
Here, I suggest storing the array's length in a variable before the for-loop, and use it instead:
++i
costs less gas compared toi++
++i
costs less gas compared toi++
for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration)i++
incrementsi
and returns the initial value ofi
. Which means:But
++i
returns the actual incremented value:In the first case, the compiler has to create a temporary variable (when used) for returning
1
instead of2
Instances include:
I suggest using
++i
instead ofi++
to increment the value of an uint variable.Increments can be unchecked
In Solidity 0.8+, there's a default overflow check on unsigned integers. It's possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.
ethereum/solidity#10695
Instances include:
The code would go from:
to:
The risk of overflow is inexistant for a
uint256
here.Arithmetics
Shift Right instead of Dividing by 2
A division by 2 can be calculated by shifting one to the right.
While the
DIV
opcode uses 5 gas, theSHR
opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.I suggest replacing
/ 2
with>> 1
here:Errors
Reduce the size of error messages (Long revert Strings)
Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met.
Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.
Revert strings > 32 bytes are here:
I suggest shortening the revert strings to fit in 32 bytes, or that using custom errors as described next.
Use Custom Errors instead of Revert Strings to save Gas
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)
Source: https://blog.soliditylang.org/2021/04/21/custom-errors/:
Custom errors are defined using the
error
statement, which can be used inside and outside of contracts (including interfaces and libraries).Instances include:
I suggest replacing revert strings with custom errors.
The text was updated successfully, but these errors were encountered: