How much code does that proc macro generate?

Jun 26, 2025

AI summary AI-generated, may contain mistakes

This post introduces -Zmacro-stats, a new unstable Rust compiler flag that quantifies how much code proc macros and declarative macros generate, a compile-time cost the author believes is underappreciated. Examples show that macros like serde::Deserialize or tracing::instrument can generate hundreds of lines of code even for tiny structs, and in one large project where macro expansion tripled the code size, removing or replacing heavyweight macros cut compile times by about 20%. The tool is most useful for large codebases, and where a macro dominates generated code, options include avoiding it entirely or switching to a cheaper alternative.

Rust’s proc macros are powerful but tricky, and have some compile-time costs. This post is about a new tool to help quantify and reduce some of those costs.

Proc macro costs

There are several ways proc macros affect compile times.

Keep reading

Out of respect for the author only the opening is shown here. Read the rest on the original site, or in HorizonReader with highlights, notes and AI help.

More from Nicholas Nethercote

How to speed up the Rust compiler in November 2026

AI summaryThe article reviews a month of Rust compiler performance work, highlighting a new span compression scheme that cut interning to ~1% for incremental builds and reduced instruction counts by up to 3%+, along with a dozen micro-optimizations that lowered the new trait solver's overhead on Diesel from 1.49x to 1.16x. Other changes included removing the AttrTokenStream types, avoiding type variables in large vec literals, and sparse metadata encoding that shrank metadata by up to 37% and peak memory by up to 7%. Overall, the period saw a 2.85% mean wall-time improvement and a 4.46% mean max-RSS reduction, with 560 of 561 instruction-count measurements since the start of the year showing improvements.

How to speed up the Rust compiler in September 2026

AI summaryThe article reports that the Rust compiler achieved a mean wall-time reduction of 4.57% between late July and late September 2026, with 555 of 629 benchmarks improving. Key contributors included enabling PGO for Clippy, upgrading to LLVM 23, optimizations for the new Polonius Alpha borrow checker and new trait solver, and numerous improvements from contributors like xmakro and the author, including a ~30% speedup for cranelift-codegen via a new CFG traversal algorithm. The author also notes they will soon start working at Hexcat on compiler performance optimization.

How to speed up the Rust compiler in July 2026

AI summaryThis July 2026 update on Rust compiler performance reports a 5.59% mean wall-time reduction since December 2025, driven largely by a 37.92% improvement in rustdoc (from optimized impl processing, PGO training, and faster impl sorting) plus major Clippy speed-ups from eliminating no-op virtual dispatch calls. Additional gains came from incremental compilation tweaks, AST node size reductions, and dramatic new trait solver progress, including one benchmark dropping from 27 seconds to under one second, with notable contributions from newcomers xmakro and arya dradjica. The author also thanks the weekly performance triage crew and shares personal news of leaving their job at VectorWare to pursue paid work on the Rust compiler.

How to speed up the Rust compiler in December 2025

AI summaryThis article reviews recent Rust compiler performance improvements as of December 2025, including optimizations like a fast path for lowering trivial consts (speeding up the libc crate by 5-15%), a more space-efficient reimplementation of format_args!(), an LLVM 21 upgrade, and reduced allocations in rustdoc-json. It also describes efforts to streamline code generated by the #[derive(Reflect)] proc macro in Bevy, cutting generated code size by 39% and compile times by 16%. Overall, mean wall-times improved by about 2.65-3.19% since May, with other metrics like memory usage and binary size showing mixed results.

My new job

AI summaryThe author announces they have joined VectorWare, a startup aiming to improve GPU programming with Rust, in a role that involves open source and some Rust compiler work while they remain a Rust compiler team member and maintainer. They clarify that speculation about their delayed announcement was unfounded—the wait was due to paperwork—and that they received interest from many employers, demonstrating Rust's adoption across a remarkably wide range of industries. They close by thanking everyone who helped during their job search.