Open code423n4 opened 2 years ago
3 * 8 = 24
Would save 20 gas
Saves 5 gas per 2 = 10
Only for requires, only if solidity < 0.8.13 with optimizer on, saves 3 gas
9 * 3 = 27
Cannot quantify unless you sent POC with before and after
Saves 3 gas
Saves 6 gas (MSTORE = 3 + MLOAD = 3) per instance 5 * 6 = 30
Agree, will save 2.1k per first SLOAD, in lack of more details I'll give it 1 SLOAD each 2100 * 2 = 4200
No longer true
Total Gas Saved 4314
Gas Optimizations Report
For-Loops: Cache array length outside of loops
Reading an array length at each iteration of the loop takes 6 gas (3 for
mload
and 3 to placememory_offset
) in the stack.Caching the array length in the stack saves around 3 gas per iteration.
For example:
can be changed to:
Consider making the following change to these lines:
For-Loops: Index increments can be left unchecked
From Solidity v0.8 onwards, all arithmetic operations come with implicit overflow and underflow checks.
In for-loops, as it is impossible for the index to overflow, it can be left unchecked to save gas every iteration.
For example, the code below:
can be changed to:
Consider making the following change to these lines:
Arithmetics:
++i
costs less gas compared toi++
ori += 1
++i
costs less gas compared toi++
ori += 1
for unsigned integers, as pre-increment is cheaper (about 5 gas per iteration). This statement is true even with the optimizer enabled.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
, thus it costs more gas.The same logic applies for
--i
andi--
.Consider using
++i
instead ofi++
ori += 1
in the following instances:Arithmetics: Use
!= 0
instead of> 0
for unsigned integersuint
will never go below 0. Thus,> 0
is gas inefficient in comparisons as checking if!= 0
is sufficient and costs less gas.Consider changing
> 0
to!= 0
in these lines:Errors: Use custom errors instead of revert strings
Since Solidity v0.8.4, custom errors should be used instead of revert strings due to:
Taken from Custom Errors in Solidity:
Custom errors can be defined using of the
error
statement, both inside or outside of contracts.Instances where custom errors can be used instead:
Unnecessary initialization of variables with default values
Uninitialized variables are assigned with a default value depending on its type:
uint
:0
bool
:false
address
:address(0)
Thus, explicitly initializing a variable with its default value costs unnecesary gas. For example, the following code:
can be changed to:
Consider declaring the following lines without explicitly setting a value:
Unnecessary definition of variables
Some variables are defined even though they are only used once in their respective functions. Not defining these variables can help to reduce gas cost and contract size.
Instances include:
Storage variables should be declared
immutable
when possibleIf a storage variable is assigned only in the constructor, it should be declared as
immutable
. This would help to reduce gas costs as calls toimmutable
variables are much cheaper than regular state variables, as seen from the Solidity Docs:Consider declaring these variables as
immutable
:Variables declared as
constant
are expressions, not constantsDue to how
constant
variables are implemented (replacements at compile-time), an expression assigned to aconstant
variable is recomputed each time that the variable is used, which wastes some gas.If the variable was
immutable
instead: the calculation would only be done once at deploy time (in the constructor), and then the result would be saved and read directly at runtime rather than being recalculated.See: ethereum/solidity#9232:
Change these expressions from
constant
toimmutable
and implement the calculation in the constructor. Alternatively, hardcode these values in the constants and add a comment to say how the value was calculated.