Skip to content

perf: skip nonstandard_macro_braces work when the lint is disabled - #17125

Closed
xmakro wants to merge 1 commit into
rust-lang:masterfrom
xmakro:perf/nonstandard-macro-braces-guard
Closed

perf: skip nonstandard_macro_braces work when the lint is disabled#17125
xmakro wants to merge 1 commit into
rust-lang:masterfrom
xmakro:perf/nonstandard-macro-braces-guard

Conversation

@xmakro

@xmakro xmakro commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

NONSTANDARD_MACRO_BRACES is a nursery (allow-by-default) lint, but is_offending_macro ran on every item, stmt, expr and ty regardless of whether the lint was enabled. For each node it walked the full macro backtrace (which clones ExpnData and re-enters the hygiene thread-local once per level) and hashed macro names against the brace map. When the lint is disabled none of that can produce a warning, so it is all wasted.

The fix adds two early returns at the top of is_offending_macro:

  1. If the span is not from a macro, return right away. This matches what the original loop already did, but it happens before the lint-level lookup so ordinary non-macro nodes stay as cheap as before.
  2. If the span is from a macro but the lint is allowed at that node, skip the walk.

When the lint is enabled the second guard is never taken, so behaviour is unchanged.

Benchmarked with callgrind so the numbers are deterministic and machine-independent. Release cargo-clippy run under valgrind --tool=callgrind --trace-children=yes, with dependencies pre-warmed so the measured run only re-lints the workspace crate. The figure is the summed clippy-driver instruction count (Ir). Two drivers built against identical libs, differing only in this change:

crate Ir before Ir after change
wasmi 12,471,491,702 11,362,130,905 -8.90%
serde 3,361,612,726 3,347,918,880 -0.41%
ripgrep 1,319,816,618 1,318,269,988 -0.12%

The saving is concentrated on macro-heavy code such as wasmi. On crates that use few macros it is small, which is what the guard is meant to do.

changelog: none

…sabled

`NONSTANDARD_MACRO_BRACES` is a nursery (allow-by-default) lint, but
`is_offending_macro` ran on every item/stmt/expr/ty unconditionally:
walking the full macro backtrace (`outer_expn_data` clone + hygiene
thread-local re-entry) and hashing macro names against the brace map,
even though nothing could be emitted when the lint is disabled.

Bail out early on two cheap checks. First, if the span isn't from a
macro there is nothing to do (same as the original loop, but before any
lint-level lookup so the common non-macro node stays free). Then, once we
know the span came from a macro, skip everything when the lint is allowed
at this node. Behaviour is unchanged when the lint is enabled.

Same-binary A/B (instructions, N=12 medians) on macro-heavy code shows
wasmi -8.3%; macro-light crates (tokio/syn/regex/ripgrep/serde) stay
within noise. Diagnostics are byte-identical and the UI test is green.
@xmakro
xmakro force-pushed the perf/nonstandard-macro-braces-guard branch from f4e0c41 to 460f017 Compare June 1, 2026 15:42
@xmakro
xmakro marked this pull request as ready for review June 1, 2026 15:58
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 1, 2026
@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dswij (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@samueltardieu samueltardieu added the G-performance-project Goal: For issues and PRs related to the Clippy Performance Project label Jun 1, 2026
@blyxyas

blyxyas commented Jun 3, 2026

Copy link
Copy Markdown
Member

Thanks for looking into it! ❤️ But this is thankfully Not necessary when we land #16808, and could cause more performance issues that it fixes.

@xmakro

xmakro commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #16808

@xmakro xmakro closed this Jun 3, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

G-performance-project Goal: For issues and PRs related to the Clippy Performance Project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants