Gas Optimizations #74
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
Caching storage variables in memory to save gas
Calldata instead of memory for RO function parameters
Comparison operators
Custom errors
Default value initialization
Prefix increments
Shifting cheaper than division
unnecessary computation
Caching storage variables in memory to save gas
IMPACT
Anytime you are reading from storage more than once, it is cheaper in gas cost to cache the variable in memory: a SLOAD cost 100gas, while MLOAD and MSTORE cost 3 gas.
PROOF OF CONCEPT
Instances include:
FlywheelCore.sol
scope:
setFlywheelRewards()
flywheelRewards
is read twice:scope:
accrueStrategy()
flywheelBooster
is read twice:scope:
accrueUser()
flywheelBooster
is read twice:TOOLS USED
Manual Analysis
MITIGATION
cache these storage variables in memory
Calldata instead of memory for RO function parameters
PROBLEM
If a reference type function parameter is read-only, it is cheaper in gas to use calldata instead of memory.
Calldata is a non-modifiable, non-persistent area where function arguments are stored, and behaves mostly like memory.
Try to use calldata as a data location because it will avoid copies and also makes sure that the data cannot be modified.
PROOF OF CONCEPT
Instances include:
FlywheelCore.sol
scope:
accrueStrategy()
scope:
accrueUser()
FlywheelGaugeRewards.sol
scope:
_queueRewards()
TOOLS USED
Manual Analysis
MITIGATION
Replace
memory
withcalldata
Comparison Operators
IMPACT
In the EVM, there is no opcode for
>=
or<=
.When using greater than or equal, two operations are performed:
>
and=
.Using strict comparison operators hence saves gas
PROOF OF CONCEPT
Instances include:
FlywheelGaugeRewards.sol
ERC20Gauges.sol
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
Replace
<=
with<
, and>=
with>
. Do not forget to increment/decrement the compared variableexample:
However, if
1
is negligible compared to the value of the variable, we can omit the increment.example:
Custom Errors
IMPACT
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met) while providing the same amount of information, as explained here
Custom errors are defined using the error statement
PROOF OF CONCEPT
Instances include:
FlywheelCore.sol
FlywheelGaugeRewards.sol
ERC20Gauges.sol
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
Replace require and revert statements with custom errors.
For instance, in
FlywheelGaugeRewards.sol
:Replace
with
and define the custom error in the contract
Default value initialization
IMPACT
If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type).
Explicitly initializing it with its default value is an anti-pattern and wastes gas.
PROOF OF CONCEPT
Instances include:
xTribe.sol
FlywheelGaugeRewards.sol
ERC20Gauges.sol
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
Remove explicit initialization for default values.
Prefix increments
IMPACT
Prefix increments are cheaper than postfix increments.
PROOF OF CONCEPT
Instances include:
xTRIBE.sol
FlywheelGaugeRewards.sol
ERC20Gauges.sol
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
change
variable++
to++variable
.Shifting cheaper than division
IMPACT
A division by 2 can be calculated by shifting one to the right. While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.
PROOF OF CONCEPT
Instances include:
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
Unnecessary computation
IMPACT
When emitting an event that includes a new and an old value, it is cheaper in gas to avoid caching the old value in memory. Instead, emit the event, then save the new value in storage.
PROOF OF CONCEPT
Instances include:
ERC20Gauges.sol
ERC20MultiVotes.sol
TOOLS USED
Manual Analysis
MITIGATION
Replace
with
The text was updated successfully, but these errors were encountered: