src/utils/GobblerReserve.sol
37: for (uint256 i = 0; i < ids.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.
src/Pages.sol
251: for (uint256 i = 0; i < numPages; i++) _mint(community, ++lastMintedPageId);
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.
src/utils/GobblerReserve.sol
37: for (uint256 i = 0; i < ids.length; i++) {
G-08: 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.
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: 2
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L251
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/GobblerReserve.sol#L37
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: 2
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L186
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L251
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: 2
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC1155B.sol#L114
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/GobblerReserve.sol#L37
G-04:
x += y
costs more gas thanx = x + y
for state variablesTotal instances of this issue: 9
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L439
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L662
instance #3 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L844
instance #4 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L912
instance #5 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L913
instance #6 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/lib/solmate/src/utils/SignedWadMath.sol#L203
instance #7 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/lib/solmate/src/utils/SignedWadMath.sol#L205
instance #8 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L184
instance #9 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L244
G-05: Multiplication or Division by two should use bit shifting
<x> * 2
is the same as<x> >> 1
and<x> / 2
is the same as<x> >> 1
. MUL and DIV opcodes cost2 gas
more than SHL and SHR.Total instances of this issue: 2
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L449
instance #2 Permalink: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L461-L463
G-06: 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: 3
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L560
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/lib/solmate/src/auth/Owned.sol#L39
instance #3 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/GobblerReserve.sol#L34
G-07: 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: 7
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L432
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L592
instance #3 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC1155B.sol#L114
instance #4 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC1155B.sol#L173
instance #5 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L186
instance #6 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L251
instance #7 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/GobblerReserve.sol#L37
G-08: 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: 18
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L159
instance #2 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L167
instance #3 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L189
instance #4 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L191
instance #5 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L204
instance #6 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L206
instance #7 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L208
instance #8 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/ArtGobblers.sol#L210
instance #9 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC1155B.sol#L48
instance #10 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC1155B.sol#L50
instance #11 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L38
instance #12 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L40
instance #13 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L49
instance #14 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L51
instance #15 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L53
instance #16 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L55
instance #17 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L107
instance #18 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/Pages.sol#L114
G-09: Using
!= 0
costs less gas than> 0
in a require() statementSaves 6 gas per instance for solidity version less than 0.8.12.
Total instances of this issue: 1
instance #1 Link: https://github.com/code-423n4/2022-09-artgobblers/blob/main/lib/solmate/src/utils/SignedWadMath.sol#L142