-
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 #100
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
Re For uint use != 0 instead of > 0:Fixed in previous commit. |
Re Keep revert strings below 32 bytesFixed in lifinance/lifi-contracts@f35ed79a266a69b363d72332b7861d15d18b98cb |
Re Unused library importRemoved in previous commit. Re ++iWe internally decided to avoid prefix increments for now. Re Cache _postBalance earlierFixed in lifinance/lifi-contracts@87a27cee2fbde337c4ab873971f37573d2240994 |
This was referenced Apr 8, 2022
This was referenced Apr 11, 2022
Closed
Closed
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
Gas optimization report
For uint use != 0 instead of > 0
For the type
uint
it is cheaper to use the logical operator!=
instead of the>
when checking the value is not0
. The value can never be below0
since it is unsigned and hence would underflow instead.The relevant code:
Keep revert strings below 32 bytes
Strings are stored in slots of 32 bytes, and hence the length of the revert string should be at max 32 bytes to fit inside 1 slot. If the string is just 33 bytes it will occupy 2 slots (64 bytes). Keeping the string size at 32 bytes or below will save gas on both deployment and when the revert condition is met.
Since the used version of Solidity is
>=0.8.4
it would also be worth considering using Custom Errors which is both more gas efficient and allows error descriptions using NatSpec.The relevant code:
Unused variable MAX_INT in LibSwap.sol
The constant variable
MAX_INT
is never used inLibSwap.sol
and can hence be removed.The relevant code:
For loop gas optimization
It is cheaper to prefix increment (
++i
) a loop counter rather than postfix increment it (i++
). Furthermore, there is no reason to use checked arithmetic on auint
/uint256
loop counter, since there is little to no risk of it overflowing. In newer versions of Solidity there is a built-in overflow check for increments, which is not necessary in this case and is hence a waste of gas. Instead it is possible to skip the overflow check by using unchecked arithmetic likeunchecked{++i}
This means that a loop looking like this
Should be rewritten into this
These optimizations will save gas for each iteration and hence the total amount of saved gas depends on the size of the element that is looped over.
The relevant code:
Unused library import
The library
LibDiamond
is imported twice inAnySwapFacet.sol
and the library is not used in the file and hence should be removed.The relevant code:
Cache _postSwapBalance earlier
In
AnySwapFacet.sol
the_postSwapBalance
is cached in line 103, however, the exact same operation is used in the line above the caching (line 101). Therefore, it is possible to save aBALANCE
instruction by swapping the two lines such that:Becomes this:
The text was updated successfully, but these errors were encountered: