Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@MerlinEgalite
Copy link
Contributor

@MerlinEgalite MerlinEgalite commented Aug 12, 2022

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

testMulDivDown() (gas: 1861)
testMulDivUp() (gas: 2273)

After

testMulDivDown() (gas: 1791)
testMulDivUp() (gas: 2073)

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge snapshot?
  • Ran npm run lint?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

@MerlinEgalite MerlinEgalite marked this pull request as ready for review August 12, 2022 12:42
@MerlinEgalite MerlinEgalite changed the title Optimize gas consumption for mulDivUp and mulDivDown ⚡️ Optimize gas consumption for mulDivUp and mulDivDown Aug 12, 2022
Copy link

@MathisGD MathisGD left a 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 :

@MerlinEgalite MerlinEgalite force-pushed the perf/fixed-point-math-lib branch from aaaffe3 to 880ad0f Compare October 15, 2022 13:34
@MathisGD
Copy link

Some useful proofs for this can be found in morpho-utils.

@MerlinEgalite MerlinEgalite mentioned this pull request Oct 15, 2022
3 tasks
// 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))
Copy link
Owner

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?

Copy link
Contributor Author

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)
Copy link
Owner

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;
Copy link
Owner

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

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

Copy link
Contributor Author

@MerlinEgalite MerlinEgalite Oct 16, 2022

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: the gas seems to increase a bit (see below). I left the config at 1M optimizer runs.

Screenshot 2022-10-16 at 14 15 55

Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah lol sorry, not possible either 🥲

Screenshot 2022-10-17 at 13 15 36

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thats... stupid

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

welp alright

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah clearly

@transmissions11
Copy link
Owner

Some useful proofs for this can be found in morpho-utils.

this is sick

@transmissions11
Copy link
Owner

and thank you guys for actually following the PR title format... no one else seems to do that lol

@transmissions11
Copy link
Owner

nice, this is looking really good!

@transmissions11 transmissions11 merged commit c2594bf into transmissions11:main Oct 17, 2022
@MerlinEgalite
Copy link
Contributor Author

Awesome!

@cruzdanilo
Copy link

can this be ported to v7?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants