From c945ac07b3c7b6fa2865dd6741f2e74042ff7b64 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Sep 2021 16:37:01 +0000 Subject: [PATCH] Explain the lazy variable initialization --- tracing-attributes/src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index e2cacdd1c3..9f7d3f6c9f 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -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) {