Reading an 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.
For example:
for (uint256 i; i < arr.length; ++i) {}
can be changed to:
uint256 len = arr.length;
for (uint256 i; i < len; ++i) {}
Consider making the following change to these lines:
contracts/lib/OrderCombiner.sol:
247: for (uint256 j = 0; j < offer.length; ++j) {
291: for (uint256 j = 0; j < consideration.length; ++j) {
598: for (uint256 j = 0; j < consideration.length; ++j) {
621: for (uint256 i = 0; i < executions.length; ) {
contracts/lib/OrderFulfiller.sol:
217: for (uint256 i = 0; i < orderParameters.offer.length; ) {
306: for (uint256 i = 0; i < orderParameters.consideration.length; ) {
Arithmetics: ++i costs less gas compared to i++ or i += 1
++i costs less gas compared to i++ or i += 1 for unsigned integers, as pre-increment is cheaper (about 5 gas per iteration). This statement is true even with the optimizer enabled.
i++ increments i and returns the initial value of i. Which means:
uint i = 1;
i++; // == 1 but i == 2
But ++i returns the actual incremented value:
uint i = 1;
++i; // == 2 and i == 2 too, so no need for a temporary variable
In the first case, the compiler has to create a temporary variable (when used) for returning 1 instead of 2, thus it costs more gas.
The same logic applies for --i and i--.
Consider using ++i instead of i++ or i += 1 in the following instances:
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.
GAS 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:
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: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: