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

Best in class Gas Reporting #1795

Closed
w1nt3r-eth opened this issue May 31, 2022 · 3 comments
Closed

Best in class Gas Reporting #1795

w1nt3r-eth opened this issue May 31, 2022 · 3 comments
Labels
A-gas-snapshots Area: gas snapshotting/reporting C-forge Command: forge T-feature Type: feature

Comments

@w1nt3r-eth
Copy link

Component

Forge

Describe the feature you would like

Make it easy for the Foundry users to identify & optimize gas usage of their contracts.

Currently, the gas is reported as a single number next to the test cases or function names.

inline summary
image image

This doesn't help new users understand what's taking up all the gas in the transaction. The lack of insights pushes users to adopt questionable hacks (e.g. unchecked) vs optimizing the heavy operations (like SSTORE).

Here are a few ideas on how to improve gas reporting visuals:

Opcodes Histogram

Group every opcode executed in the transaction by the opcode itself (maybe SSTORE/SLOAD need flags like hot/cold) and plot the total cumulative gas consumption. Feels like this would be the easiest to implement as it only requires debug_traceTransaction and a hashmap.

The disadvantage here is that it's not obvious how to find the expensive operations in the contract without having a good idea of how Solidity & EVM work.

image

Flame Graph

This is a screenshot from Tenderly's gas profiler tool. It gives a good idea of which functions are contributing the most to gas consumption. Ideally, the lowest level should show heavy opcodes.

This probably requires an untrivial amount of work connecting gas usage to (obfuscated) function names.

image

Export Systrace

Systrace is commonly used to debug the performance of large & complicated systems. Its ecosystem has a bunch of tools for processing and visualizations, so if we could export a compatible JSON file we could get a lot of this functionality for free.

This is very advanced, but also very hard to use for a regular user.

image

Compare Before & After

A typical flow optimizing gas is to make a change and compare reports between previous and current runs. This is error-prone and fragile. We could store gas profiles in a temporary location and show if the numbers improved or got worse vs the last run.

With Opcodes Histogram or Flame Graph, we could use some color to highlight the differences in a very visual (and motivating) way.

Tenderly Integration

Tenderly has decent visualizations and tools, but getting it to work with hardhat is a lot of pain. Thinking of an ideal DX here, maybe passing a flag to forge command could send the tx (along with everything else needed, like source code) to Tenderly and print out a URL to view the trace.

Additional context

No response

@w1nt3r-eth w1nt3r-eth added the T-feature Type: feature label May 31, 2022
@onbjerg onbjerg added this to Foundry May 31, 2022
@onbjerg onbjerg moved this to Todo in Foundry May 31, 2022
@mattsse
Copy link
Member

mattsse commented Jun 1, 2022

thanks for this write up.

in order to support this, we need some kind of Metrics/Diagnostic type that keeps track of all the info. To some degree, this exists here

https://github.com/foundry-rs/foundry/blob/master/forge/src/gas_report.rs

to track this a bit more fine-grained we'd need a custom revm::Inspector I think.

visualizing this is then just additional work.

ref #1737

@mattsse mattsse added C-forge Command: forge A-gas-snapshots Area: gas snapshotting/reporting labels Jun 1, 2022
@onbjerg
Copy link
Member

onbjerg commented Jun 21, 2022

Ref #137

@zerosnacks
Copy link
Member

Closing in favor of #7761 for flamegraphs + #8222 for lifting of required information for flamegraphs

Related tickets:

@zerosnacks zerosnacks closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2024
@jenpaff jenpaff moved this from Todo to Completed in Foundry Sep 30, 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 T-feature Type: feature
Projects
Archived in project
Development

No branches or pull requests

5 participants