Skip to content

Commit 2020bba

Browse files
committed
Use more efficient thread_local! { … = const { … } }
These don't require an initialization check on every access, which should at least be useful for the `CURRENT_STATE` `thread_local!` in `tracing-core`. Probably can't be included any time soon, as this requires Rust 1.59: rust-lang/rust#91355 (comment).
1 parent 4adc0a3 commit 2020bba

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

examples/examples/sloggish/sloggish_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct CurrentSpanPerThread {
2727
impl CurrentSpanPerThread {
2828
pub fn new() -> Self {
2929
thread_local! {
30-
static CURRENT: RefCell<Vec<Id>> = RefCell::new(vec![]);
30+
static CURRENT: RefCell<Vec<Id>> = const { RefCell::new(vec![]) };
3131
};
3232
Self { current: &CURRENT }
3333
}

tracing-core/src/dispatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ enum Kind<T> {
178178

179179
#[cfg(feature = "std")]
180180
thread_local! {
181-
static CURRENT_STATE: State = State {
181+
static CURRENT_STATE: State = const { State {
182182
default: RefCell::new(None),
183183
can_enter: Cell::new(true),
184-
};
184+
} };
185185
}
186186

187187
static EXISTS: AtomicBool = AtomicBool::new(false);

tracing-subscriber/src/filter/env/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub struct EnvFilter {
108108
}
109109

110110
thread_local! {
111-
static SCOPE: RefCell<Vec<LevelFilter>> = RefCell::new(Vec::new());
111+
static SCOPE: RefCell<Vec<LevelFilter>> = const { RefCell::new(Vec::new()) };
112112
}
113113

114114
type FieldMap<T> = HashMap<Field, T>;

tracing-subscriber/src/fmt/fmt_subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ where
727727

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

733733
BUF.with(|buf| {

tracing-subscriber/src/registry/sharded.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ thread_local! {
198198
/// track how many subscribers have processed the close.
199199
/// For additional details, see [`CloseGuard`].
200200
///
201-
static CLOSE_COUNT: Cell<usize> = Cell::new(0);
201+
static CLOSE_COUNT: Cell<usize> = const { Cell::new(0) };
202202
}
203203

204204
impl Collect for Registry {

0 commit comments

Comments
 (0)