Skip to content

Commit cf9bb35

Browse files
SUPERCILEXhawkw
authored andcommitted
use const thread_locals when possible (#2838)
This results in a substantial performance improvement, and is compatible with our MSRV. Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 4744ba1 commit cf9bb35

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

examples/examples/sloggish/sloggish_subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct CurrentSpanPerThread {
3838
impl CurrentSpanPerThread {
3939
pub fn new() -> Self {
4040
thread_local! {
41-
static CURRENT: RefCell<Vec<Id>> = const { RefCell::new(vec![]) };
41+
static CURRENT: RefCell<Vec<Id>> = const { RefCell::new(Vec::new()) };
4242
};
4343
Self { current: &CURRENT }
4444
}

tracing-core/src/dispatcher.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ enum Kind<T> {
183183

184184
#[cfg(feature = "std")]
185185
thread_local! {
186-
static CURRENT_STATE: State = const { State {
187-
default: RefCell::new(None),
188-
can_enter: Cell::new(true),
189-
} };
186+
static CURRENT_STATE: State = const {
187+
State {
188+
default: RefCell::new(None),
189+
can_enter: Cell::new(true),
190+
}
191+
};
190192
}
191193

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

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ pub struct FilterId(u64);
9999
///
100100
/// [`Registry`]: crate::Registry
101101
/// [`Filter`]: crate::layer::Filter
102-
#[derive(Default, Copy, Clone, Eq, PartialEq)]
102+
#[derive(Copy, Clone, Eq, PartialEq)]
103103
pub(crate) struct FilterMap {
104104
bits: u64,
105105
}
106106

107-
/// The current state of `enabled` calls to per-layer filters on this
107+
impl FilterMap {
108+
pub(crate) const fn new() -> Self {
109+
Self { bits: 0 }
110+
}
111+
}
112+
113+
/// The current state of `enabled` calls to per-subscriber filters on this
108114
/// thread.
109115
///
110116
/// When `Filtered::enabled` is called, the filter will set the bit
@@ -145,7 +151,7 @@ pub(crate) struct FilterState {
145151

146152
/// Extra counters added to `FilterState` used only to make debug assertions.
147153
#[cfg(debug_assertions)]
148-
#[derive(Debug, Default)]
154+
#[derive(Debug)]
149155
struct DebugCounters {
150156
/// How many per-layer filters have participated in the current `enabled`
151157
/// call?
@@ -156,8 +162,18 @@ struct DebugCounters {
156162
in_interest_pass: Cell<usize>,
157163
}
158164

165+
#[cfg(debug_assertions)]
166+
impl DebugCounters {
167+
const fn new() -> Self {
168+
Self {
169+
in_filter_pass: Cell::new(0),
170+
in_interest_pass: Cell::new(0),
171+
}
172+
}
173+
}
174+
159175
thread_local! {
160-
pub(crate) static FILTERING: FilterState = FilterState::new();
176+
pub(crate) static FILTERING: FilterState = const { FilterState::new() };
161177
}
162178

163179
/// Extension trait adding [combinators] for combining [`Filter`].
@@ -1080,13 +1096,13 @@ impl fmt::Binary for FilterMap {
10801096
// === impl FilterState ===
10811097

10821098
impl FilterState {
1083-
fn new() -> Self {
1099+
const fn new() -> Self {
10841100
Self {
1085-
enabled: Cell::new(FilterMap::default()),
1101+
enabled: Cell::new(FilterMap::new()),
10861102
interest: RefCell::new(None),
10871103

10881104
#[cfg(debug_assertions)]
1089-
counters: DebugCounters::default(),
1105+
counters: DebugCounters::new(),
10901106
}
10911107
}
10921108

@@ -1095,7 +1111,7 @@ impl FilterState {
10951111
{
10961112
let in_current_pass = self.counters.in_filter_pass.get();
10971113
if in_current_pass == 0 {
1098-
debug_assert_eq!(self.enabled.get(), FilterMap::default());
1114+
debug_assert_eq!(self.enabled.get(), FilterMap::new());
10991115
}
11001116
self.counters.in_filter_pass.set(in_current_pass + 1);
11011117
debug_assert_eq!(
@@ -1140,7 +1156,7 @@ impl FilterState {
11401156
#[cfg(debug_assertions)]
11411157
{
11421158
if this.counters.in_filter_pass.get() == 0 {
1143-
debug_assert_eq!(this.enabled.get(), FilterMap::default());
1159+
debug_assert_eq!(this.enabled.get(), FilterMap::new());
11441160
}
11451161

11461162
// Nothing enabled this event, we won't tick back down the
@@ -1177,7 +1193,7 @@ impl FilterState {
11771193
{
11781194
let in_current_pass = self.counters.in_filter_pass.get();
11791195
if in_current_pass <= 1 {
1180-
debug_assert_eq!(self.enabled.get(), FilterMap::default());
1196+
debug_assert_eq!(self.enabled.get(), FilterMap::new());
11811197
}
11821198
self.counters
11831199
.in_filter_pass
@@ -1207,7 +1223,7 @@ impl FilterState {
12071223
// a panic and the thread-local has been torn down, that's fine, just
12081224
// ignore it ratehr than panicking.
12091225
let _ = FILTERING.try_with(|filtering| {
1210-
filtering.enabled.set(FilterMap::default());
1226+
filtering.enabled.set(FilterMap::new());
12111227

12121228
#[cfg(debug_assertions)]
12131229
filtering.counters.in_filter_pass.set(0);
@@ -1232,10 +1248,8 @@ impl FilterState {
12321248
pub(crate) fn filter_map(&self) -> FilterMap {
12331249
let map = self.enabled.get();
12341250
#[cfg(debug_assertions)]
1235-
{
1236-
if self.counters.in_filter_pass.get() == 0 {
1237-
debug_assert_eq!(map, FilterMap::default());
1238-
}
1251+
if self.counters.in_filter_pass.get() == 0 {
1252+
debug_assert_eq!(map, FilterMap::new());
12391253
}
12401254

12411255
map

tracing-subscriber/src/registry/sharded.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl Subscriber for Registry {
255255
data.filter_map = crate::filter::FILTERING.with(|filtering| filtering.filter_map());
256256
#[cfg(debug_assertions)]
257257
{
258-
if data.filter_map != FilterMap::default() {
258+
if data.filter_map != FilterMap::new() {
259259
debug_assert!(self.has_per_layer_filters());
260260
}
261261
}
@@ -481,7 +481,7 @@ impl Default for DataInner {
481481
};
482482

483483
Self {
484-
filter_map: FilterMap::default(),
484+
filter_map: FilterMap::new(),
485485
metadata: &NULL_METADATA,
486486
parent: None,
487487
ref_count: AtomicUsize::new(0),
@@ -526,7 +526,7 @@ impl Clear for DataInner {
526526
})
527527
.clear();
528528

529-
self.filter_map = FilterMap::default();
529+
self.filter_map = FilterMap::new();
530530
}
531531
}
532532

0 commit comments

Comments
 (0)