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

Enable writing gas reports for specific contracts to .snapshot #2065

Open
wilsoncusack opened this issue Jun 22, 2022 · 7 comments · May be fixed by #8952
Open

Enable writing gas reports for specific contracts to .snapshot #2065

wilsoncusack opened this issue Jun 22, 2022 · 7 comments · May be fixed by #8952
Assignees
Labels
A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge P-high Priority: high T-feature Type: feature
Milestone

Comments

@wilsoncusack
Copy link
Contributor

Component

Forge

Describe the feature you would like

Problem:
Currently, changes in .snapshot do not necessarily reflect gas changes a developer cares about. Snapshots directly reflect the gas use of the test functions, and only indirectly reflect the gas used by the contract being tested.

If a developer changes a test and the contract being tested in the same diff, they lose signal in the .snapshot as to the gas effect of their changes.

We see many developers solve this by creating super minimal tests with names like testXGas so that they always have some indication of exclusively the changing gas use in the contract being tested.

In fact, for me personally, I generally only care about the snapshot of these functions. The gas snapshot of all the other functions is mostly just noise.

Proposed solution
A very useful tool is forge test --gas-report it shows the gas use of each function in each contract! This is great, and could be super powered by (1) enabling writing this gas report to the snapshot (2) enabling passing arguments as to which contracts we exclusively want gas reports for.

The command might look something like

forge snapshot --gas-report --contracts MyContract

This command could run existing snapshot functionality and additionally write the gas report of the specified contracts to the end of the snapshot. Though, for me personally, if writing the gas-report to snapshot existed, I could do without the existing snapshot functionality entirely, I think. Possibly still nice for quickly checking gas of something, though.

Additional context

No response

@wilsoncusack wilsoncusack added the T-feature Type: feature label Jun 22, 2022
@wilsoncusack
Copy link
Contributor Author

Another idea would be to just have a new .gas-report file. Possibly in foundry.toml one could specify which contracts to include.

@onbjerg
Copy link
Member

onbjerg commented Jun 22, 2022

As mentioned in another snapshot/gas report issue/request recently we used to talk about just merging forge snapshot and the gas report feature because they both have strengths and shortcomings: snapshots have --check, --diff and disk saving (which gas reports lack) and gas reports have more info and less noise in the readings

@onbjerg onbjerg added C-forge Command: forge A-gas-snapshots Area: gas snapshotting/reporting labels Jun 22, 2022
@wilsoncusack
Copy link
Contributor Author

As mentioned in another snapshot/gas report issue/request recently we used to talk about just merging forge snapshot and the gas report feature because they both have strengths and shortcomings: snapshots have --check, --diff and disk saving (which gas reports lack) and gas reports have more info and less noise in the readings

Ok got it, thanks. Feel free to close if this is a duplicate.

@wilsoncusack
Copy link
Contributor Author

@gakonst I'm happy to work on this if we can get consensus on what to implement :)

@mds1
Copy link
Collaborator

mds1 commented Jul 2, 2023

Maybe some optimizoors disagree (and I'd be curious to hear their thoughts first), but my suggestion would be:

  • deprecate the existing forge snapshot behavior for the reasons mentioned above (it's sensitive to test file changes)
  • replace it with forge snapshot <subcommand> as spec'd out in feat: expand snapshot functionality #2056, where forge snapshot gas uses the gas-report behavior and supports --diff and --check options

If we go this route I'd split things into a few PRs:

  • First PR: Adds forge snapshot gas with support for the diff/check flags, keep forge snapshot behavior but log a warning that "it's deprecated and will be removed, use forge snapshot gas instead
  • Future PRs add additional subcommands to forge snapshot

@wilsoncusack
Copy link
Contributor Author

Sorry @mds1 just saw your reply. That sounds reasonable to me! I'd propose default new snapshot is just to write gas-report.

@zerosnacks
Copy link
Member

zerosnacks commented Jun 25, 2024

Relevant: https://github.com/marktoda/forge-gas-snapshot/
Being used in the Uniswap V4 repo: https://github.com/Uniswap/v4-core/tree/main/.forge-snapshots

Would be great to upstream this into Foundry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge P-high Priority: high T-feature Type: feature
Projects
Status: Ready For Review
4 participants