-
Notifications
You must be signed in to change notification settings - Fork 701
⚡️ Optimize gas consumption for mulDivUp and mulDivDown
#306
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
⚡️ Optimize gas consumption for mulDivUp and mulDivDown
#306
Conversation
mulDivUp and mulDivDownmulDivUp and mulDivDown
MathisGD
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last remark, I think that it would be great to add a little comment about how the div is rounded up :
aaaffe3 to
880ad0f
Compare
|
Some useful proofs for this can be found in morpho-utils. |
| // We allow z - 1 to underflow if z is 0, because we multiply the | ||
| // end result by 0 if z is zero, ensuring we return 0 if z is zero. | ||
| z := mul(iszero(iszero(z)), add(div(sub(z, 1), denominator), 1)) | ||
| z := add(gt(mod(mul(x, y), denominator), 0), div(mul(x, y), denominator)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add an english translation comment here like the one you removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
|
|
||
| // Divide z by the denominator. | ||
| z := div(z, denominator) | ||
| z := div(mul(x, y), denominator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add an english translation comment here like the one you removed?
| SIMPLIFIED FIXED POINT OPERATIONS | ||
| //////////////////////////////////////////////////////////////*/ | ||
|
|
||
| uint256 internal constant MAX_UINT256 = 2**256 - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use type(uint256).max here instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it is impossible in assembly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes not possible, unfortunately. We can use not(0) to replace it though. If the optimizer is high enough it should be the same gas cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just meant writing
uint256 internal constant MAX_UINT256 = type(uint256).max;
haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh thats... stupid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
welp alright
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah clearly
this is sick |
|
and thank you guys for actually following the PR title format... no one else seems to do that lol |
|
nice, this is looking really good! |
|
Awesome! |
|
can this be ported to v7? |


Description
Apply gas optimizations based on the work done in morpho-utils repository by the MEP team and the Morpho Labs's team.
Results
Before
After
Checklist
Ensure you completed all of the steps below before submitting your pull request:
forge snapshot?npm run lint?forge test?Pull requests with an incomplete checklist will be thrown out.