Skip to content

Commit

Permalink
Use more efficient thread_local! { … = const { … } }
Browse files Browse the repository at this point in the history
Probably can't be included any time soon, as this requires Rust 1.59.

rust-lang/rust#91355 (comment)
  • Loading branch information
hrxi committed Mar 18, 2022
1 parent 4adc0a3 commit cdc7e1f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 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![]) };
};
Self { current: &CURRENT }
}
Expand Down
4 changes: 2 additions & 2 deletions tracing-core/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ enum Kind<T> {

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

static EXISTS: AtomicBool = AtomicBool::new(false);
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/filter/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct EnvFilter {
}

thread_local! {
static SCOPE: RefCell<Vec<LevelFilter>> = RefCell::new(Vec::new());
static SCOPE: RefCell<Vec<LevelFilter>> = const { RefCell::new(Vec::new()) };
}

type FieldMap<T> = HashMap<Field, T>;
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 @@ -727,7 +727,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 @@ -198,7 +198,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 cdc7e1f

Please sign in to comment.