Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #282

Open
code423n4 opened this issue Sep 27, 2022 · 2 comments
Open

Gas Optimizations #282

code423n4 opened this issue Sep 27, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

code423n4 commented Sep 27, 2022

Gas Optimization Report

Note On Methodology

The gas snapshot file didn't seem to be up to date with the original tests so forge snapshot was run before any optimization was tested to ensure the snapshot was up-to-date.

Each of the following optimizations have been implemented and tested individually. The gas savings represent the total "overall gas change" displayed after
running forge snapshot --diff .gas-snapshot.

Optimizations

[G-1] Bit Shifting Rather Than Base-2 Division / Multiplication

Total Gas Improvement: 128
Files: src/ArtGobblers.sol
Optimization: Replace / 2 with >> 1 and * 2 with << 1 respectively.

[G-2] Use Local Value Rather Than Storage Re-Read

Total Gas Improvement: 3224
Files: src/ArtGobblers.sol
Optimization: Replace numMintedFromGoo with mintedFromGoo on L493. This will ensure that the existing local copy of the value is used instead of re-reading from storage.

[G-3] Improve Swap Indices Retrieval

Total Gas Improvement: 87023
Files: src/ArtGobblers.sol L615,L623,
Optimization: Replace lines L615-625 with the following code avoiding the repeated storage load and make the ternary branchless:

// Get the index of the swap id.
uint64 storedSwapIndex = getGobblerData[swapId].idx;
uint64 swapIndex;
assembly {
	swapIndex := or(storedSwapIndex, mul(iszero(storedSwapIndex), swapId))
}

// Get the owner of the current id.
address currentIdOwner = getGobblerData[currentId].owner;

// Get the index of the current id.
uint64 storedCurrentIndex = getGobblerData[currentId].idx;
uint64 currentIndex;
assembly {
	currentIndex := or(storedCurrentIndex, mul(iszero(storedCurrentIndex), currentId))
}

[G-4] Remove Unused Method Parameter

Total Gas Improvement: 780967 (Note that some tests seem to fair slightly worse with this optimization)
Files: src/ArtGobblers.sol, src/utils/rand/ChainlinkV1RandProvider.sol,
Optimization: Since the acceptRandomSeed method does nothing with bytes32 requestId it does not have to receive it. It can therefore be removed as the random provider is a custom adapter anyway.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Sep 27, 2022
code423n4 added a commit that referenced this issue Sep 27, 2022
code423n4 added a commit that referenced this issue Sep 27, 2022
@GalloDaSballo
Copy link
Collaborator

[G-2] Use Local Value Rather Than Storage Re-Read

100

[G-3] Improve Swap Indices Retrieval

Seems to save 200 gas per instance, will give you 500 gas as it's well researched

Rest is unclear and the shift is disputed per chat with sponsor

Best report so far, would recommend trying to add a few more basic findings to get some extra free savings and make a have a stronger report

700

@GalloDaSballo
Copy link
Collaborator

Ultimately this report missed the immutables else it would have been a strong contender

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants