foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(`snapshots`): track internal gas usage

Open PaulRBerg opened this issue 3 years ago • 4 comments

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 avatar Nov 25 '22 14:11 PaulRBerg

@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

rkrasiuk avatar Nov 28 '22 08:11 rkrasiuk

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 https://github.com/foundry-rs/foundry/issues/3723.

PaulRBerg avatar Nov 28 '22 09:11 PaulRBerg

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

rkrasiuk avatar Nov 28 '22 09:11 rkrasiuk

Hi @PaulRBerg we've added gas snapshots over sections here: https://github.com/foundry-rs/foundry/pull/8952, would be great to get your feedback. It handles both external and internal gas usage.

zerosnacks avatar Oct 17 '24 11:10 zerosnacks

Marking as effectively resolved by https://github.com/foundry-rs/foundry/pull/8952

zerosnacks avatar Oct 21 '24 12:10 zerosnacks