Skip to content

Commit

Permalink
reduce overhead of re-allocating Dispatch::none
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Jun 22, 2022
1 parent fa45696 commit 930a844
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tracing-core/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@
//!

use crate::{
callsite, span,
callsite,
lazy::Lazy,
span,
subscriber::{self, NoSubscriber, Subscriber},
Event, LevelFilter, Metadata,
};
Expand Down Expand Up @@ -170,6 +172,8 @@ const INITIALIZED: usize = 2;

static mut GLOBAL_DISPATCH: Option<Dispatch> = None;

static NO_DISPATCH: Lazy<Dispatch> = Lazy::new(|| Dispatch::new(NoSubscriber::default()));

/// The dispatch state of a thread.
#[cfg(feature = "std")]
struct State {
Expand Down Expand Up @@ -327,9 +331,9 @@ where
return entered.with_current(|current| f(current));
}

f(&Dispatch::none())
f(&NO_DISPATCH)
})
.unwrap_or_else(|_| f(&Dispatch::none()))
.unwrap_or_else(|_| f(&NO_DISPATCH))
}

/// Executes a closure with a reference to this thread's current [dispatcher].
Expand Down Expand Up @@ -372,7 +376,7 @@ where
if let Some(d) = get_global() {
f(d)
} else {
f(&Dispatch::none())
f(&NO_DISPATCH)
}
}

Expand All @@ -396,9 +400,7 @@ impl Dispatch {
/// Returns a new `Dispatch` that discards events and spans.
#[inline]
pub fn none() -> Self {
Dispatch {
subscriber: Arc::new(NoSubscriber::default()),
}
NO_DISPATCH.clone()
}

/// Returns a `Dispatch` that forwards to the given [`Subscriber`].
Expand Down Expand Up @@ -710,7 +712,7 @@ impl<'a> Entered<'a> {
*default = Some(global.clone());
f(global)
}
None => f(&Dispatch::none()),
None => f(&NO_DISPATCH),
},
}
}
Expand Down

0 comments on commit 930a844

Please sign in to comment.