Skip to content

Introduce -Zmacro-stats #142069

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

Merged
merged 3 commits into from
Jun 13, 2025
Merged

Introduce -Zmacro-stats #142069

merged 3 commits into from
Jun 13, 2025

Conversation

nnethercote
Copy link
Contributor

Introduce -Zmacro-stats.

It collects data about macro expansions and prints them in a table after expansion finishes. It's very useful for detecting macro bloat, especially for proc macros.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2025
@nnethercote
Copy link
Contributor Author

I used this recently on a non-open-source codebase with ~100,000 lines of Rust code. cargo expand produced ~325,000 lines, due to heavy proc macro usage. Avoiding some unnecessary proc macro usage got this down to ~205,000 lines, reducing compile times by ~20%.

@lqd
Copy link
Member

lqd commented Jun 5, 2025

-Zself-profile has the expand_proc_macro activity, didn't that work?

@Kobzol
Copy link
Contributor

Kobzol commented Jun 5, 2025

This sounds awesome!

@lqd recently let me know about the fact that -Zself-profile can actually be used to record how many times was a specific proc macro invoked (#95739), and I wanted to use that in a cargo command for profiling builds, but the thing that was missing was how many lines of code did the macro generate. With -Zmacro-stats that information would be available, which is awesome (although for programmatic usage, I'd probably want a JSON based output, but that can be added later if needed).

@nnethercote
Copy link
Contributor Author

-Zself-profile has the expand_proc_macro activity, didn't that work?

I hadn't heard about that...

@Kobzol
Copy link
Contributor

Kobzol commented Jun 5, 2025

FWIW I tried to use it, and it gives me the durations of proc macro executions for individual macros (e.g. #[derive(Serialize, Deserialize)] would give me time for expanding Serialize and time for expanding Deserialize. However, it only seemed to work for actual proc macros, not things like #[derive(Hash)], and it doesn't contain information about how much code was generated. It also requires using -Zself-profile, which can make builds way slower than they normally are.

@lqd
Copy link
Member

lqd commented Jun 5, 2025

builtin derives are not proc-macros, but you're more than welcome to add their expansion to the self-profiler :)

code-size is an interesting metric but it's not always correlated with compile times which you're interested in optimizing: you still want expansion times (example: cranelift-codegen has two huge functions of the same size, one of which takes 8ms in dataflow, the other 2.5s)

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Kobzol
Copy link
Contributor

Kobzol commented Jun 5, 2025

builtin derives are not proc-macros, but you're more than welcome to add their expansion to the self-profiler :)

Right, that was my first question on this PR, if we can just add it to -Zself-profile without a new flag :) But I think that the richness of the potentially useful metadata that we could produce here might not be a good fit for the quite limited data model of the self-profile.

code-size is an interesting metric but it's not always correlated with compile times which you're interested in optimizing: you still want expansion times (example: cranelift-codegen has two huge functions of the same size, one of which takes 8ms in dataflow, the other 2.5s)

It's a very rough approximation, of course. Ideally, I would like to see "this macro generated code that took the compiler 2.5s to typecheck", but I don't suppose that's really feasible easily today, so I'll take what I can get :)

@lqd
Copy link
Member

lqd commented Jun 5, 2025

AFAIK there's nothing inherent to the data model that would prevent adding information to correlate between events of different queries, if that's what was needed for nick's project or your query (e.g. if compilation time was not in expansion but elsewhere). But this is getting offtopic so I'll stop here.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 5, 2025
@nnethercote
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 10, 2025
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 10, 2025
They will be used in a subsequent commit.
It currently only inserts separators into `usize`s, because that's all
that has been needed so far. `-Zmacro-stats` will need `isize` and `f64`
handling, so this commit adds that.
@nnethercote
Copy link
Contributor Author

I have addressed the comments. I moved the code to the stats module in a new commit. We can merge it with the previous commit before merging.

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 12, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 12, 2025
@petrochenkov
Copy link
Contributor

r=me after the commit squash #142069 (comment)
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 12, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

It collects data about macro expansions and prints them in a table after
expansion finishes. It's very useful for detecting macro bloat,
especially for proc macros.

Details:
- It measures code snippets by pretty-printing them and then measuring
  lines and bytes. This required a bunch of additional pretty-printing
  plumbing, in `rustc_ast_pretty` and `rustc_expand`.
- The measurement is done in `MacroExpander::expand_invoc`.
- The measurements are stored in `ExtCtxt::macro_stats`.
@nnethercote
Copy link
Contributor Author

I squashed.

@bors r=petrochenkov

@bors
Copy link
Collaborator

bors commented Jun 12, 2025

📌 Commit 376cbc3 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 12, 2025
bors added a commit that referenced this pull request Jun 13, 2025
Rollup of 10 pull requests

Successful merges:

 - #134847 (Implement asymmetrical precedence for closures and jumps)
 - #141491 (Delegate `<CStr as Debug>` to `ByteStr`)
 - #141770 (Merge `Cfg::render_long_html` and `Cfg::render_long_plain` methods common code)
 - #142069 (Introduce `-Zmacro-stats`)
 - #142158 (Tracking the old name of renamed unstable library features)
 - #142221 ([AIX] strip underlying xcoff object)
 - #142340 (miri: we can use apfloat's mul_add now)
 - #142379 (Add bootstrap option to compile a tool with features)
 - #142410 (intrinsics: rename min_align_of to align_of)
 - #142413 (rustc-dev-guide subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9639a7c into rust-lang:master Jun 13, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 13, 2025
rust-timer added a commit that referenced this pull request Jun 13, 2025
Rollup merge of #142069 - nnethercote:Zmacro-stats, r=petrochenkov

Introduce `-Zmacro-stats`

Introduce `-Zmacro-stats`.

It collects data about macro expansions and prints them in a table after expansion finishes. It's very useful for detecting macro bloat, especially for proc macros.

r? `@petrochenkov`
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 14, 2025
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#134847 (Implement asymmetrical precedence for closures and jumps)
 - rust-lang/rust#141491 (Delegate `<CStr as Debug>` to `ByteStr`)
 - rust-lang/rust#141770 (Merge `Cfg::render_long_html` and `Cfg::render_long_plain` methods common code)
 - rust-lang/rust#142069 (Introduce `-Zmacro-stats`)
 - rust-lang/rust#142158 (Tracking the old name of renamed unstable library features)
 - rust-lang/rust#142221 ([AIX] strip underlying xcoff object)
 - rust-lang/rust#142340 (miri: we can use apfloat's mul_add now)
 - rust-lang/rust#142379 (Add bootstrap option to compile a tool with features)
 - rust-lang/rust#142410 (intrinsics: rename min_align_of to align_of)
 - rust-lang/rust#142413 (rustc-dev-guide subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
@nnethercote nnethercote deleted the Zmacro-stats branch June 14, 2025 09:33
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jun 16, 2025
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#134847 (Implement asymmetrical precedence for closures and jumps)
 - rust-lang/rust#141491 (Delegate `<CStr as Debug>` to `ByteStr`)
 - rust-lang/rust#141770 (Merge `Cfg::render_long_html` and `Cfg::render_long_plain` methods common code)
 - rust-lang/rust#142069 (Introduce `-Zmacro-stats`)
 - rust-lang/rust#142158 (Tracking the old name of renamed unstable library features)
 - rust-lang/rust#142221 ([AIX] strip underlying xcoff object)
 - rust-lang/rust#142340 (miri: we can use apfloat's mul_add now)
 - rust-lang/rust#142379 (Add bootstrap option to compile a tool with features)
 - rust-lang/rust#142410 (intrinsics: rename min_align_of to align_of)
 - rust-lang/rust#142413 (rustc-dev-guide subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
@Kobzol
Copy link
Contributor

Kobzol commented Jun 17, 2025

@rust-timer build d601de6

(For #142442)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d601de6): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.1% [-1.2%, -1.0%] 4
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.1%] 3
All ❌✅ (primary) -1.1% [-1.2%, -1.0%] 4

Max RSS (memory usage)

Results (primary 0.8%, secondary 0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.8% [0.8%, 0.8%] 1
Regressions ❌
(secondary)
2.7% [2.7%, 2.7%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) 0.8% [0.8%, 0.8%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 754.993s -> 692.243s (-8.31%)
Artifact size: 372.27 MiB -> 372.32 MiB (0.02%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants