This repository was archived by the owner on Apr 12, 2021. It is now read-only.
Optimize loop iterator in Lib_MerkleTree.sol.#354
Merged
maurelian merged 1 commit intoethereum-optimism:masterfrom Mar 31, 2021
Merged
Optimize loop iterator in Lib_MerkleTree.sol.#354maurelian merged 1 commit intoethereum-optimism:masterfrom
Lib_MerkleTree.sol.#354maurelian merged 1 commit intoethereum-optimism:masterfrom
Conversation
maurelian
approved these changes
Mar 31, 2021
Collaborator
maurelian
left a comment
There was a problem hiding this comment.
Hey @cleanunicorn thanks for the thorough details in the submission.
This could save us quite a bit of gas in the long haul!
LGTM, but would appreciate one more quick sanity check from either @smartcontracts or @ben-chain.
smartcontracts
approved these changes
Mar 31, 2021
Collaborator
smartcontracts
left a comment
There was a problem hiding this comment.
LGTM! Great catch. Thank you!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
While reviewing the code from a project that uses the same
Lib_MerkleTreelibrary you're using, I noticed a sub-optimal loop iterator usinguint8instead ofuint256.Because of this, Solidity will generate more operations to make sure the iterator stays within the
uint8constraints, hence it will cost more gas to execute the loop.I created a Solidity contract and ran a few tests which might help you decide if you want to include the change or not.
The following Solidity contract example was used to measure the difference between using
uint8anduint256with a few example inputs.Changing the line from
uint8touint256Gives the following different gas costs, while returning the same, identical results.
Solidity version 0.8.3+commit.8d00100c and 200 optimization runs were used in Remix IDE for tests.
0xffffffff)0xffffffffff)0xffffffffffffffff)Additional context
The Solidity example repository includes this method as an example for other developers to learn how to use the language. Because it does not strive to be the optimal implementation, its purpose is to show different features of the language itself.
https://github.com/ethereum/solidity-examples/blob/f44fe3b3b4cca94afe9c2a2d5b7840ff0fafb72e/src/bits/Bits.sol#L87-L99