-
Notifications
You must be signed in to change notification settings - Fork 4
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 #83
Labels
bug
Something isn't working
G (Gas Optimization)
resolved
Finding has been patched by sponsor (sponsor pls link to PR containing fix)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Comments
1./2. Fixed in previous update |
Re prefix increments:We internally decided to avoid prefix increments for now. |
Re unnecessarily payable:If the token to swap is native, the message value is used in |
Re: uint8Fixed in previous commit. |
Re Gas Optimization On The 2^256-1Duplicate of #197 |
Re Caching array length can save gasDuplicate of #44 |
H3xept
added
resolved
Finding has been patched by sponsor (sponsor pls link to PR containing fix)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
labels
Apr 8, 2022
Re external functionsDuplicate of #197 |
Re Unused variable MAX_INTDuplicate of #100 |
Re Redundant literal boolean comparisonsDuplicate of #39 |
Re Some internal functions can be made privateDuplicate of #122 |
Re uintx to uint256Duplicate of #196 |
Re prefix incrementsWe internally decided to avoid previx increments for now. |
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)
resolved
Finding has been patched by sponsor (sponsor pls link to PR containing fix)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
The following functions could be set external to save gas and improve code quality.
External call cost is less expensive than of public functions.
Title: Unnecessary equals boolean
Severity: GAS
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
Title: Consider inline the following functions to save gas
Severity: GAS
Title: Unnecessary functions
Severity: GAS
Title: Unnecessary array boundaries check when loading an array element twice
Severity: GAS
Title: Prefix increments are cheaper than postfix increments
Severity: GAS
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the
++i
infor (uint256 i = 0; i < numIterations; ++i)
).But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (
++x
) or not using the unchecked keyword:Title: Inline one time use functions
Severity: GAS
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
Title: Unnecessary cast
Severity: Gas
Title: Unused state variables
Severity: GAS
Unused state variables are gas consuming at deployment (since they are located in storage) and are
a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.
Title: Caching array length can save gas
Severity: GAS
Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory.
We recommend to change from:
to:
Title: Gas Optimization On The 2^256-1
Severity: GAS
Some projects (e.g. Uniswap - https://github.com/Uniswap/interface/blob/main/src/hooks/useApproveCallback.ts#L88)
set the default value of the user's allowance to 2^256 - 1. Since the value 2^256 - 1 can also be represented in
hex as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know
that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative
choice could be now 0x8000000000000000000000000000000000000000000000000000000000000000 or 2^255 to represent
"infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter
costs 45'888 gas. If you accept that infinity can also be represented via 2^255 (instead of 2^256-1), which almost
all projects can - you can already save about 4% gas leveraging this optimisation trick on those calculations.
Title: Internal functions to private
Severity: GAS
The following functions could be set private to save gas and improve code quality:
Title: Unnecessary payable
Severity: GAS
The following functions are payable but msg.value isn't used - therefore the function payable state modifier isn't necessary.
Payable functions are more gas expensive than others, and it's danger the users if they send ETH by mistake.
Title: uint8 index
Severity: GAS
Due to how the EVM natively works on 256 numbers, using a 8 bit number here introduces additional costs as the EVM has to properly enforce the limits of this smaller type.
See the warning at this link: https://docs.soliditylang.org/en/v0.8.0/internals/layout_in_storage.html#layout-of-state-variables-in-storage
We recommend to use uint256 for the index in every for loop instead using uint8:
The text was updated successfully, but these errors were encountered: