Skip to content

Commit baeba47

Browse files
SUPERCILEXhawkw
andauthored
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 b461897 commit baeba47

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
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::new()) };
3131
};
3232
Self { current: &CURRENT }
3333
}

tracing-core/src/dispatch.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,11 @@ enum Kind<T> {
205205

206206
#[cfg(feature = "std")]
207207
thread_local! {
208-
static CURRENT_STATE: State = State {
209-
default: RefCell::new(None),
210-
can_enter: Cell::new(true),
208+
static CURRENT_STATE: State = const {
209+
State {
210+
default: RefCell::new(None),
211+
can_enter: Cell::new(true),
212+
}
211213
};
212214
}
213215

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ pub struct FilterId(u64);
9999
///
100100
/// [`Registry`]: crate::Registry
101101
/// [`Filter`]: crate::subscribe::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+
impl FilterMap {
108+
pub(crate) const fn new() -> Self {
109+
Self { bits: 0 }
110+
}
111+
}
112+
107113
/// The current state of `enabled` calls to per-subscriber filters on this
108114
/// thread.
109115
///
@@ -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-subscriber 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`].
@@ -1072,13 +1088,13 @@ impl fmt::Binary for FilterMap {
10721088
// === impl FilterState ===
10731089

10741090
impl FilterState {
1075-
fn new() -> Self {
1091+
const fn new() -> Self {
10761092
Self {
1077-
enabled: Cell::new(FilterMap::default()),
1093+
enabled: Cell::new(FilterMap::new()),
10781094
interest: RefCell::new(None),
10791095

10801096
#[cfg(debug_assertions)]
1081-
counters: DebugCounters::default(),
1097+
counters: DebugCounters::new(),
10821098
}
10831099
}
10841100

@@ -1087,7 +1103,7 @@ impl FilterState {
10871103
{
10881104
let in_current_pass = self.counters.in_filter_pass.get();
10891105
if in_current_pass == 0 {
1090-
debug_assert_eq!(self.enabled.get(), FilterMap::default());
1106+
debug_assert_eq!(self.enabled.get(), FilterMap::new());
10911107
}
10921108
self.counters.in_filter_pass.set(in_current_pass + 1);
10931109
debug_assert_eq!(
@@ -1132,7 +1148,7 @@ impl FilterState {
11321148
#[cfg(debug_assertions)]
11331149
{
11341150
if this.counters.in_filter_pass.get() == 0 {
1135-
debug_assert_eq!(this.enabled.get(), FilterMap::default());
1151+
debug_assert_eq!(this.enabled.get(), FilterMap::new());
11361152
}
11371153

11381154
// Nothing enabled this event, we won't tick back down the
@@ -1169,7 +1185,7 @@ impl FilterState {
11691185
{
11701186
let in_current_pass = self.counters.in_filter_pass.get();
11711187
if in_current_pass <= 1 {
1172-
debug_assert_eq!(self.enabled.get(), FilterMap::default());
1188+
debug_assert_eq!(self.enabled.get(), FilterMap::new());
11731189
}
11741190
self.counters
11751191
.in_filter_pass
@@ -1199,7 +1215,7 @@ impl FilterState {
11991215
// a panic and the thread-local has been torn down, that's fine, just
12001216
// ignore it ratehr than panicking.
12011217
let _ = FILTERING.try_with(|filtering| {
1202-
filtering.enabled.set(FilterMap::default());
1218+
filtering.enabled.set(FilterMap::new());
12031219

12041220
#[cfg(debug_assertions)]
12051221
filtering.counters.in_filter_pass.set(0);
@@ -1225,7 +1241,7 @@ impl FilterState {
12251241
let map = self.enabled.get();
12261242
#[cfg(debug_assertions)]
12271243
if self.counters.in_filter_pass.get() == 0 {
1228-
debug_assert_eq!(map, FilterMap::default());
1244+
debug_assert_eq!(map, FilterMap::new());
12291245
}
12301246

12311247
map

tracing-subscriber/src/fmt/fmt_subscriber.rs

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

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

950950
BUF.with(|buf| {

tracing-subscriber/src/registry/sharded.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ thread_local! {
214214
/// track how many subscribers have processed the close.
215215
/// For additional details, see [`CloseGuard`].
216216
///
217-
static CLOSE_COUNT: Cell<usize> = Cell::new(0);
217+
static CLOSE_COUNT: Cell<usize> = const { Cell::new(0) };
218218
}
219219

220220
impl Collect for Registry {
@@ -255,7 +255,7 @@ impl Collect 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_subscriber_filters());
260260
}
261261
}
@@ -477,7 +477,7 @@ impl Default for DataInner {
477477
};
478478

479479
Self {
480-
filter_map: FilterMap::default(),
480+
filter_map: FilterMap::new(),
481481
metadata: &NULL_METADATA,
482482
parent: None,
483483
ref_count: AtomicUsize::new(0),
@@ -522,7 +522,7 @@ impl Clear for DataInner {
522522
})
523523
.clear();
524524

525-
self.filter_map = FilterMap::default();
525+
self.filter_map = FilterMap::new();
526526
}
527527
}
528528

0 commit comments

Comments
 (0)