Skip to content
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 loop optimization in MerkleProof.sol #5036

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

CarsonCase
Copy link

@CarsonCase CarsonCase commented May 9, 2024

Closes #5028 by caching the length of the proof to prevent an unnecessary DUP call (3 gas per iteration)

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

Copy link

changeset-bot bot commented May 9, 2024

⚠️ No Changeset found

Latest commit: 27654f7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@cairoeth cairoeth changed the title Fix gas loop optimization in MerkleProof.sol (#5028) Gas loop optimization in MerkleProof.sol (#5028) May 31, 2024
@cairoeth cairoeth marked this pull request as ready for review May 31, 2024 21:34
@cairoeth
Copy link
Contributor

cairoeth commented May 31, 2024

@CarsonCase reverted back the unchecked block because it's done automatically by Solidity 0.8.22 onwards, and also made the i variable explicit (per the style guide)

@cairoeth
Copy link
Contributor

Gas difference:

Normal Optimizer (1000 runs) Via IR
testProcessProof (unoptimized) μ: 41703, ~: 41638 μ: 41699, ~: 41638 μ: 34632, ~: 34667
testProcessProof (optimized) μ: 41335, ~: 41274 μ: 41331, ~: 41274 μ: 37574, ~: 37602
diff μ: -368, ~: -364 μ: -368, ~: -364 μ: +2,942, ~: +2,935
testprocessProofCalldata (unoptimized) μ: 31410, ~: 31343 μ: 31362, ~: 31299 μ: 24572, ~: 24567
testprocessProofCalldata (optimized) μ: 31418, ~: 31351 μ: 31370, ~: 31307 μ: 24572, ~: 24567
diff μ: +8, ~: +8 μ: +8, ~: +8 μ: 0, ~: 0

@cairoeth cairoeth changed the title Gas loop optimization in MerkleProof.sol (#5028) Gas loop optimization in MerkleProof.sol May 31, 2024
@cairoeth cairoeth requested a review from ernestognw May 31, 2024 21:50
Copy link
Member

@ernestognw ernestognw left a comment

Choose a reason for hiding this comment

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

It does look good to me but I am not sure if the tradeoff makes sense.

From the gas benchmarks we can see --via-ir is even worse in the case of memory. For the calldata one I see no difference, which is consistent with how --via-ir works as it can assume that calldata never changes, thus it can be taken out of the loop.

At the moment, seems like we're largely affecting projects using --via-ir but improving performance for those who don't, and I don't have an opinion yet 🤔

Not opposed to closing this yet, but I'd like to hear @Amxx take, perhaps he has more clarity on why via IR is getting worse results

@cairoeth cairoeth requested a review from Amxx May 31, 2024 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loop gas optimisation
3 participants