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

feat(snapshots): track internal gas usage #3766

Closed
PaulRBerg opened this issue Nov 25, 2022 · 5 comments
Closed

feat(snapshots): track internal gas usage #3766

PaulRBerg opened this issue Nov 25, 2022 · 5 comments
Assignees
Labels
A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge Cmd-forge-snapshot Command: forge snapshot T-feature Type: feature

Comments

@PaulRBerg
Copy link
Contributor

PaulRBerg commented Nov 25, 2022

Component

Forge

Describe the feature you would like

I would like to have gas reports for free functions, e.g.

function min(uint x, uint y) pure returns (uint) {
    return x < y ? x : y;
}

Passing --gas-report to Forge doesn't generate a report. The only solution as of now is to wrap my free functions in an intermediary mock contract used specifically for testing.

@PaulRBerg PaulRBerg added the T-feature Type: feature label Nov 25, 2022
@rkrasiuk rkrasiuk added C-forge Command: forge A-gas-snapshots Area: gas snapshotting/reporting labels Nov 28, 2022
@rkrasiuk
Copy link
Collaborator

rkrasiuk commented Nov 28, 2022

@PaulRBerg as described in the book, gas reporting traces the external contract calls. since free functions act as internal functions, we cannot correctly identify calls to them.

theoretically, we could record the jumps within each call, but 1) we wouldn't be able to decode them since free/internal functions are not a part of the artifacts 2) encountering a jump doesn't necessarily mean we've entered the internal function 3) that'd produce a lot of data which imo would dilute the value of gas reports

@mattsse @onbjerg @draganrakita @mds1 curious to hear your thoughts

@PaulRBerg
Copy link
Contributor Author

Thanks for explaining the difficulties of implementing this, @rkrasiuk.

I wonder if the user could somehow help Forge identify the functions that should be metered? e.g. what if there was a field in the Foundry config like meter_free_functions or gas_reports_free_functions (or something along those lines)? Would that help in any way?

Btw, this is related to #3723.

@rkrasiuk
Copy link
Collaborator

the config would still not solve the instruction to code reverse lookup, only the collected functions.

some thought dump:

  • debugger already performs the reverse lookup. we could attempt to match jumps to the code by using source maps? this would be a cursed piece of code 😆
  • roll a custom ASTInspector? the idea is to traverse the AST alongside the execution bytecode

@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@zerosnacks zerosnacks changed the title Gas reports for free functions feat(snapshots): track internal gas usage Sep 6, 2024
@zerosnacks zerosnacks added the Cmd-forge-snapshot Command: forge snapshot label Sep 6, 2024
@zerosnacks
Copy link
Member

Hi @PaulRBerg we've added gas snapshots over sections here: #8952, would be great to get your feedback. It handles both external and internal gas usage.

@zerosnacks zerosnacks removed this from the v1.0.0 milestone Oct 17, 2024
@zerosnacks zerosnacks self-assigned this Oct 17, 2024
@github-project-automation github-project-automation bot moved this to Todo in Foundry Oct 17, 2024
@zerosnacks
Copy link
Member

Marking as effectively resolved by #8952

@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Oct 21, 2024
@grandizzy grandizzy moved this from Done to Completed in Foundry Oct 21, 2024
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 Cmd-forge-snapshot Command: forge snapshot T-feature Type: feature
Projects
Status: Completed
Development

No branches or pull requests

3 participants