Use x = x + y instead of x += y.
Use x = x - y instead of x -= y.
#
G-6. Using unchecked blocks saves gas
Description
In Solidity 0.8+, there’s a default overflow and underflow check on unsigned integers. When an overflow or underflow isn’t possible, some gas can be saved by using unchecked blocks.
Gas Optimizations Report for Blur Exchange contest
Overview
During the audit, 7 gas issues were found.
Gas Optimizations Findings (7)
G-1. Postfix increment
Description
Prefix increment costs less gas than postfix.
Instances
Recommendation
Consider using prefix increment where it is relevant.
#
G-2. <>.length in loops
Description
Reading the length of an array at each iteration of the loop consumes extra gas.
Instances
Recommendation
Store the length of an array in a variable before the loop, and use it.
#
G-3. Initializing variables with default value
Description
It costs gas to initialize integer variables with 0 or bool variables with false but it is not necessary.
Instances
Recommendation
Remove initialization for default values.
For example:
for (uint256 i; i < array.length; ++i) {
#
G-4. Some variables can be immutable
Description
Using immutables is cheaper than storage-writing operations.
Instances
address public weth;
Recommendation
Use immutables where possible.
Change to
address public immutable weth;
#
G-5.
x += y
is more expensive thanx = x + y
Instances
Recommendation
Use
x = x + y
instead ofx += y
. Usex = x - y
instead ofx -= y
.#
G-6. Using unchecked blocks saves gas
Description
In Solidity 0.8+, there’s a default overflow and underflow check on unsigned integers. When an overflow or underflow isn’t possible, some gas can be saved by using unchecked blocks.
Instances
Recommendation
Change:
to:
#
G-7. Custom errors may be used
Description
Custom errors from Solidity 0.8.4 are cheaper than revert strings.
Instances
Recommendation
For example, change:
to: