Skip to content

Commit

Permalink
Use const thread_locals when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
SUPERCILEX committed Dec 25, 2023
1 parent bac2508 commit 920b80b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/examples/sloggish/sloggish_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct CurrentSpanPerThread {
impl CurrentSpanPerThread {
pub fn new() -> Self {
thread_local! {
static CURRENT: RefCell<Vec<Id>> = RefCell::new(vec![]);
static CURRENT: RefCell<Vec<Id>> = const { RefCell::new(Vec::new()) };
};
Self { current: &CURRENT }
}
Expand Down
8 changes: 5 additions & 3 deletions tracing-core/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ enum Kind<T> {

#[cfg(feature = "std")]
thread_local! {
static CURRENT_STATE: State = State {
default: RefCell::new(None),
can_enter: Cell::new(true),
static CURRENT_STATE: State = const {
State {
default: RefCell::new(None),
can_enter: Cell::new(true),
}
};
}

Expand Down
24 changes: 20 additions & 4 deletions tracing-subscriber/src/filter/subscriber_filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,17 @@ pub struct FilterId(u64);
///
/// [`Registry`]: crate::Registry
/// [`Filter`]: crate::subscribe::Filter
#[derive(Default, Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) struct FilterMap {
bits: u64,
}

impl FilterMap {
pub(crate) const fn default() -> Self {
Self { bits: 0 }
}
}

/// The current state of `enabled` calls to per-subscriber filters on this
/// thread.
///
Expand Down Expand Up @@ -145,7 +151,7 @@ pub(crate) struct FilterState {

/// Extra counters added to `FilterState` used only to make debug assertions.
#[cfg(debug_assertions)]
#[derive(Debug, Default)]
#[derive(Debug)]
struct DebugCounters {
/// How many per-subscriber filters have participated in the current `enabled`
/// call?
Expand All @@ -156,8 +162,18 @@ struct DebugCounters {
in_interest_pass: Cell<usize>,
}

#[cfg(debug_assertions)]
impl DebugCounters {
const fn default() -> Self {
Self {
in_filter_pass: Cell::new(0),
in_interest_pass: Cell::new(0),
}
}
}

thread_local! {
pub(crate) static FILTERING: FilterState = FilterState::new();
pub(crate) static FILTERING: FilterState = const { FilterState::new() };
}

/// Extension trait adding [combinators] for combining [`Filter`].
Expand Down Expand Up @@ -1072,7 +1088,7 @@ impl fmt::Binary for FilterMap {
// === impl FilterState ===

impl FilterState {
fn new() -> Self {
const fn new() -> Self {
Self {
enabled: Cell::new(FilterMap::default()),
interest: RefCell::new(None),
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ where

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
thread_local! {
static BUF: RefCell<String> = RefCell::new(String::new());
static BUF: RefCell<String> = const { RefCell::new(String::new()) };
}

BUF.with(|buf| {
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/registry/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ thread_local! {
/// track how many subscribers have processed the close.
/// For additional details, see [`CloseGuard`].
///
static CLOSE_COUNT: Cell<usize> = Cell::new(0);
static CLOSE_COUNT: Cell<usize> = const { Cell::new(0) };
}

impl Collect for Registry {
Expand Down

0 comments on commit 920b80b

Please sign in to comment.