G-01: pre-increment ++i/--i costs less gas than post-increment i++/i--
Saves 6 gas per loop in a for loop
Total instances of this issue: 3
contracts/libraries/JBIpfsDecoder.sol
68: for (uint256 i = 0; i < _length; i++) {
76: for (uint256 i = 0; i < _input.length; i++) {
84: for (uint256 i = 0; i < _indices.length; i++) {
G-02: ++i/i++ should be placed in unchecked blocks to save gas as it is impossible for them to overflow in for and while loops
Unchecked keyword is available in solidity version 0.8.0 or higher and can be applied to iterator variables to save gas.
Saves more than 30 gas per loop.
Total instances of this issue: 4
contracts/libraries/JBIpfsDecoder.sol
49: for (uint256 i = 0; i < _source.length; ++i) {
68: for (uint256 i = 0; i < _length; i++) {
76: for (uint256 i = 0; i < _input.length; i++) {
84: for (uint256 i = 0; i < _indices.length; i++) {
G-03: Length of the array (<array>.length) need not be looked up in every iteration of a for-loop
Reading array length at each iteration of the loop takes total 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array.length saves around 3 gas per iteration.
Total instances of this issue: 3
contracts/libraries/JBIpfsDecoder.sol
49: for (uint256 i = 0; i < _source.length; ++i) {
76: for (uint256 i = 0; i < _input.length; i++) {
84: for (uint256 i = 0; i < _indices.length; i++) {
G-04: x += y costs more gas than x = x + y for state variables
G-06: No need to initialize non-constant/non-immutable variables to zero
Since the default value is already zero, overwriting is not required.
Saves 8 gas per instance.
Total instances of this issue: 5
contracts/libraries/JBIpfsDecoder.sol
49: for (uint256 i = 0; i < _source.length; ++i) {
51: for (uint256 j = 0; j < digitlength; ++j) {
68: for (uint256 i = 0; i < _length; i++) {
76: for (uint256 i = 0; i < _input.length; i++) {
84: for (uint256 i = 0; i < _indices.length; i++) {
G-07: Using uints/ints smaller than 256 bits increases overhead
Gas usage becomes higher with uint/int smaller than 256 bits because EVM operates on 32 bytes and uses additional operations to reduce the size from 32 bytes to the target size.
Total instances of this issue: 2
contracts/libraries/JBIpfsDecoder.sol
48: uint8 digitlength = 1;
66: function _truncate(uint8[] memory _array, uint8 _length) private pure returns (uint8[] memory) {
G-08: Using custom errors rather than revert()/require() strings will save deployment gas
Custom errors are available from solidity version 0.8.4.
Gas Optimizations
G-01: pre-increment
++i/--i
costs less gas than post-incrementi++/i--
Saves 6 gas per loop in a for loop
Total instances of this issue: 3
G-02:
++i/i++
should be placed in unchecked blocks to save gas as it is impossible for them to overflow in for and while loopsUnchecked keyword is available in solidity version
0.8.0
or higher and can be applied to iterator variables to save gas. Saves more than30 gas
per loop.Total instances of this issue: 4
G-03: Length of the array (
<array>.length
) need not be looked up in every iteration of a for-loopReading array length at each iteration of the loop takes total 6 gas (3 for mload and 3 to place memory_offset) in the stack. Caching the
array.length
saves around3 gas
per iteration.Total instances of this issue: 3
G-04:
x += y
costs more gas thanx = x + y
for state variablesTotal instances of this issue: 7
G-05: Adding
payable
to functions which are only meant to be called by specific actors likeonlyOwner
will save gas costMarking functions payable removes additional checks for whether a payment was provided, hence reducing gas cost
Total instances of this issue: 7
G-06: No need to initialize non-constant/non-immutable variables to zero
Since the default value is already zero, overwriting is not required. Saves
8 gas
per instance.Total instances of this issue: 5
G-07: Using uints/ints smaller than 256 bits increases overhead
Gas usage becomes higher with uint/int smaller than 256 bits because EVM operates on 32 bytes and uses additional operations to reduce the size from 32 bytes to the target size.
Total instances of this issue: 2
G-08: Using custom errors rather than revert()/require() strings will save deployment gas
Custom errors are available from solidity version 0.8.4.
Total instances of this issue: 2