Skip to content

Aggregate deployment gas statistics in --gas-stats output#8038

Merged
schaable merged 3 commits intomainfrom
deployment-gas-stats
Mar 18, 2026
Merged

Aggregate deployment gas statistics in --gas-stats output#8038
schaable merged 3 commits intomainfrom
deployment-gas-stats

Conversation

@schaable
Copy link
Copy Markdown
Member

@schaable schaable commented Mar 6, 2026

Closes #8037.

Docs: NomicFoundation/hardhat-website#232

Output example:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
║                                  Gas Usage Statistics                                 ║
╚═══════════════════════════════════════════════════════════════════════════════════════╝
╔═══════════════════════════════════════════════════════════════════════════════════════╗
║ contracts/Calculator.sol:Calculator                                                   ║
╟───────────────────────────────────┬────────┬─────────┬────────┬────────┬──────────────╢
║ Function name                     │ Min    │ Average │ Median │ Max    │ #calls       ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ divide                            │ 44316  │ 44316   │ 44316  │ 44316  │ 1            ║
║ multiply(uint256,uint256)         │ 44254  │ 44254   │ 44254  │ 44254  │ 2            ║
║ multiply(uint256,uint256,uint256) │ 44875  │ 44875   │ 44875  │ 44875  │ 1            ║
║ reset                             │ 21485  │ 21485   │ 21485  │ 21485  │ 1            ║
║ result                            │ 23510  │ 23510   │ 23510  │ 23510  │ 6            ║
║ subtract                          │ 44213  │ 44213   │ 44213  │ 44213  │ 1            ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ Deployment                        │ Min    │ Average │ Median │ Max    │ #deployments ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║                                   │ 288115 │ 288115  │ 288115 │ 288115 │ 1            ║
╚═══════════════════════════════════╧════════╧═════════╧════════╧════════╧══════════════╝
╔═══════════════════════════════════════════════════════════════════════════════════════╗
║ contracts/Counter.sol:Counter                                                         ║
╟───────────────────────────────────┬────────┬─────────┬────────┬────────┬──────────────╢
║ Function name                     │ Min    │ Average │ Median │ Max    │ #calls       ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ add(uint256)                      │ 43915  │ 43915   │ 43915  │ 43915  │ 1            ║
║ add(uint256,bool)                 │ 44284  │ 44419   │ 44419  │ 44554  │ 2            ║
║ inc                               │ 43482  │ 43482   │ 43482  │ 43482  │ 1            ║
║ x                                 │ 23466  │ 23466   │ 23466  │ 23466  │ 5            ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ Deployment                        │ Min    │ Average │ Median │ Max    │ #deployments ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║                                   │ 234940 │ 234940  │ 234940 │ 234940 │ 1            ║
╚═══════════════════════════════════╧════════╧═════════╧════════╧════════╧══════════════╝

@schaable schaable requested a review from Copilot March 6, 2026 23:41
@schaable schaable self-assigned this Mar 6, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 6, 2026

🦋 Changeset detected

Latest commit: 870c01c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates Hardhat’s --gas-stats reporting to aggregate deployment gas measurements per contract (instead of keeping only the latest deployment), aligning deployment reporting with the existing function-call statistics approach.

Changes:

  • Store per-contract deployment gas measurements as a list and compute aggregated stats (min/avg/median/max/count).
  • Update the gas stats report to display aggregated deployment stats and remove the deployment size column.
  • Update tests and add a changeset documenting the behavior change.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Aggregates deployment gas measurements and updates report output to show deployment stats (min/avg/median/max/#deployments).
v-next/hardhat/test/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Updates aggregation/stat/report tests to validate the new deployment stats behavior and formatting.
.changeset/evil-beers-pump.md Documents the patch change: deployment gas stats are now aggregated instead of showing only last deployment cost/size.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +292 to +299
type: "row",
cells: [
`${contractGasStats.deployment.gas}`,
`${contractGasStats.deployment.size}`,
"",
`${contractGasStats.deployment.min}`,
`${contractGasStats.deployment.avg}`,
`${contractGasStats.deployment.median}`,
`${contractGasStats.deployment.max}`,
`${contractGasStats.deployment.calls}`,
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The deployment stats data row uses an empty string for the first cell, so the column labeled "Deployment" has no value in the only row. Consider putting a label (e.g. "Deployment") in the row’s first cell (or making the first column header blank) to avoid a confusing blank column in the rendered output.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we use "Deployment" as the first cell in the row, we could remove the subheaders and keep only the top headers. We could even sort the table to place the deployment row first. Still, the first column header ("Function Name") might be a bit misleading. Wdyt @alcuadrado?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we do this, we'd also need to update the last column header.

Copy link
Copy Markdown
Member Author

@schaable schaable Mar 7, 2026

Choose a reason for hiding this comment

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

Some examples:

Initial suggestion:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
║ contracts/Calculator.sol:Calculator                                                   ║
╟───────────────────────────────────┬────────┬─────────┬────────┬────────┬──────────────╢
║ Function name                     │ Min    │ Average │ Median │ Max    │ #calls       ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ divide                            │ 44316  │ 44316   │ 44316  │ 44316  │ 1            ║
║ multiply(uint256,uint256)         │ 44254  │ 44254   │ 44254  │ 44254  │ 2            ║
║ multiply(uint256,uint256,uint256) │ 44875  │ 44875   │ 44875  │ 44875  │ 1            ║
║ reset                             │ 21485  │ 21485   │ 21485  │ 21485  │ 1            ║
║ result                            │ 23510  │ 23510   │ 23510  │ 23510  │ 6            ║
║ subtract                          │ 44213  │ 44213   │ 44213  │ 44213  │ 1            ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║                                   │ Min    │ Average │ Median │ Max    │ #deployments ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ Deployment                        │ 288115 │ 288115  │ 288115 │ 288115 │ 1            ║
╚═══════════════════════════════════╧════════╧═════════╧════════╧════════╧══════════════╝

Alternative 1:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
║ contracts/Calculator.sol:Calculator                                                   ║
╟───────────────────────────────────┬────────┬─────────┬────────┬────────┬──────────────╢
║                                   │ Min    │ Average │ Median │ Max    │ Count        ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ Deployment                        │ 288115 │ 288115  │ 288115 │ 288115 │ 1            ║
║ divide                            │ 44316  │ 44316   │ 44316  │ 44316  │ 1            ║
║ multiply(uint256,uint256)         │ 44254  │ 44254   │ 44254  │ 44254  │ 2            ║
║ multiply(uint256,uint256,uint256) │ 44875  │ 44875   │ 44875  │ 44875  │ 1            ║
║ reset                             │ 21485  │ 21485   │ 21485  │ 21485  │ 1            ║
║ result                            │ 23510  │ 23510   │ 23510  │ 23510  │ 6            ║
║ subtract                          │ 44213  │ 44213   │ 44213  │ 44213  │ 1            ║
╚═══════════════════════════════════╧════════╧═════════╧════════╧════════╧══════════════╝

Deployment could be colorized to distinguish it from functions.

Alternative 2:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
║ contracts/Calculator.sol:Calculator                                                   ║
╟───────────────────────────────────┬────────┬─────────┬────────┬────────┬──────────────╢
║ Function name                     │ Min    │ Average │ Median │ Max    │ #calls       ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ divide                            │ 44316  │ 44316   │ 44316  │ 44316  │ 1            ║
║ multiply(uint256,uint256)         │ 44254  │ 44254   │ 44254  │ 44254  │ 2            ║
║ multiply(uint256,uint256,uint256) │ 44875  │ 44875   │ 44875  │ 44875  │ 1            ║
║ reset                             │ 21485  │ 21485   │ 21485  │ 21485  │ 1            ║
║ result                            │ 23510  │ 23510   │ 23510  │ 23510  │ 6            ║
║ subtract                          │ 44213  │ 44213   │ 44213  │ 44213  │ 1            ║
╟───────────────────────────────────┼────────┼─────────┼────────┼────────┼──────────────╢
║ Deployment                        │ 288115 │ 288115  │ 288115 │ 288115 │ 1            ║
╚═══════════════════════════════════╧════════╧═════════╧════════╧════════╧══════════════╝

@schaable schaable requested review from alcuadrado and kanej March 7, 2026 00:21
@kanej kanej linked an issue Mar 9, 2026 that may be closed by this pull request
@schaable schaable changed the title Deployment gas stats Aggregate deployment gas statistics in --gas-stats output Mar 9, 2026
@kanej kanej removed the request for review from alcuadrado March 9, 2026 15:37
@schaable schaable force-pushed the aggregate-gas-stats-output branch from eec7cb6 to e62d33c Compare March 12, 2026 13:05
@kanej kanej removed their request for review March 12, 2026 15:17
@schaable schaable force-pushed the aggregate-gas-stats-output branch from 8ab302f to 126259a Compare March 12, 2026 16:51
@schaable
Copy link
Copy Markdown
Member Author

Added an issue to edr to get the runtime size from the deployments, so we can show it in the --gas-stats table.

Base automatically changed from aggregate-gas-stats-output to main March 12, 2026 17:08
@schaable
Copy link
Copy Markdown
Member Author

Added #8059

Copilot AI review requested due to automatic review settings March 12, 2026 17:28
@schaable schaable force-pushed the deployment-gas-stats branch from 343f139 to 3a35c9e Compare March 12, 2026 17:28
@schaable schaable force-pushed the deployment-gas-stats branch from 3a35c9e to 870c01c Compare March 12, 2026 17:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@schaable schaable requested a review from alcuadrado March 12, 2026 18:27
@schaable schaable added this pull request to the merge queue Mar 18, 2026
Merged via the queue into main with commit f1b501e Mar 18, 2026
213 checks passed
@schaable schaable deleted the deployment-gas-stats branch March 18, 2026 14:17
@github-actions github-actions Bot mentioned this pull request Mar 18, 2026
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.

Aggregate deployment gas statistics in --gas-stats output

3 participants