Skip to content

Commit

Permalink
Explain the lazy variable initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 29, 2021
1 parent ccd7e11 commit c945ac0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ fn gen_block(
)
} else {
quote_spanned!(block.span()=>
// These variables are left uninitialized and initialized only
// if the tracing level is statically enabled at this point.
// While the tracing level is also checked at span creation
// time, that will still create a dummy span, and a dummy guard
// and drop the dummy guard later. By lazily initializing these
// variables, Rust will generate a drop flag for them and thus
// only drop the guard if it was created. This creates code that
// is very straightforward for LLVM to optimize out if the tracing
// level is statically disabled, while not causing any performance
// regression in case the level is enabled.
let __tracing_attr_span;
let __tracing_attr_guard;
if tracing::level_enabled!(#level) {
Expand Down

0 comments on commit c945ac0

Please sign in to comment.