Skip to content

Commit c945ac0

Browse files
committed
Explain the lazy variable initialization
1 parent ccd7e11 commit c945ac0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tracing-attributes/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,16 @@ fn gen_block(
553553
)
554554
} else {
555555
quote_spanned!(block.span()=>
556+
// These variables are left uninitialized and initialized only
557+
// if the tracing level is statically enabled at this point.
558+
// While the tracing level is also checked at span creation
559+
// time, that will still create a dummy span, and a dummy guard
560+
// and drop the dummy guard later. By lazily initializing these
561+
// variables, Rust will generate a drop flag for them and thus
562+
// only drop the guard if it was created. This creates code that
563+
// is very straightforward for LLVM to optimize out if the tracing
564+
// level is statically disabled, while not causing any performance
565+
// regression in case the level is enabled.
556566
let __tracing_attr_span;
557567
let __tracing_attr_guard;
558568
if tracing::level_enabled!(#level) {

0 commit comments

Comments
 (0)