Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tracing: move macro callsite impls out of macro expansion (#869)
## Motivation Currently, every `tracing` macro generates a _new implementation_ of the `Callsite` trait for a zero-sized struct created for that particular callsite. This callsite accesses several statics defined in the macro expansion. This means that each tracing macro expands to a _lot_ of code — check out the `cargo expand` output: ``` eliza on butterfly in tracing/examples on master [$?] is v0.0.0 via ⚙️ v1.44.0 :; cargo expand --example all-levels | wc -l Checking tracing-examples v0.0.0 (/home/eliza/code/tracing/examples) Finished check [unoptimized + debuginfo] target(s) in 0.20s 463 ``` More code in the macro expansion means more code in the function *invoking* the macro, which may make that function harder for `rustc` to optimize. This effects the performance of *other* code in the function, not the `tracing` code, so this isn't necessarily visible in `tracing`'s microbenchmarks, which only contain `tracing` code. In `rustc` itself, there is a small but noticeable performance impact from switching from `log` to `tracing` even after making changes that should make the filtering overhead equivalent: rust-lang/rust#74726 (comment). This appears to be due to more complex generated code impacting optimizer behavior. ## Solution This branch moves the callsite generated by each macro out of the macro expansion and into a single `MacroCallsite` private API type in the `__macro_support` module. Instead of creating a zero-sized `Callsite` static and multiple statics for the `Metadata`, the `Once` cell for registration, and the `Interest` atomic, these are all now fields on the `Callsite` struct. This shouldn't result in any real change, but makes the implementation simpler. All the hot filtering functions on `MacroCallsite` are `#[inline(always)]`, so we shouldn't be adding stack frames to code that was previously generated in the macro expansion. After making this change, the expanded output is about half as long as it was before: ``` eliza on butterfly in tracing/examples on eliza/smaller-macros [$?] is v0.0.0 via ⚙️ v1.44.0 :; cargo expand --example all-levels | wc -l Checking tracing-examples v0.0.0 (/home/eliza/code/tracing/examples) Finished check [unoptimized + debuginfo] target(s) in 0.75s 233 ``` This change appears to fix most of the remaining `rustc` performance regressions: rust-lang/rust#74726 (comment) Additionally, it has some other side benefits. I imagine it probably improves compile times a bit for crates using `tracing` (although I haven't tested this), since the compiler is generating fewer callsite implementations. Reducing the number of branches in the macro expansion probably helps make the pesky `cognitive_complexity` Clippy lint show up less often, and improves maintainability for the macros as well. Signed-off-by: Eliza Weisman <[email protected]> Co-authored-by: David Barsky <[email protected]>
- Loading branch information