Skip to content

Commit

Permalink
core, subscriber: migrate from lazy_static to once_cell (tokio-rs…
Browse files Browse the repository at this point in the history
…#2147)

Replace `lazy_static` with `once_cell`. Fixes tokio-rs#2146.

## Motivation

`lazy_static!`, while a declarative macro, is a macro nonetheless. It
can add quite a bit of additional compilation time cost.
`once_cell::sync::Lazy` does the same thing with generics, and can be
used more flexibly (i.e. non-static lazily initialized values), and has
been proposed to be added to `std` (see linked issue).

I'm trying to reduce the compile time and dependency tree complexity of
a dependent project: [bevy](https://bevyengine.org), which is using
tracing. `lazy_static` and `once_cell` are both in our dependency tree
and both end up doing the same thing.

## Solution

Migrate to `once_cell`.
  • Loading branch information
james7132 authored and kaffarell committed May 22, 2024
1 parent 8736f3c commit b243b55
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions tracing-log/src/interest_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ahash::AHasher;
use log::{Level, Metadata};
use lru::LruCache;
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::hash::Hasher;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -140,12 +141,10 @@ static SENTINEL_METADATA: tracing_core::Metadata<'static> = tracing_core::Metada
tracing_core::metadata::Kind::EVENT,
);

lazy_static::lazy_static! {
static ref CONFIG: Mutex<InterestCacheConfig> = {
tracing_core::callsite::register(&SENTINEL_CALLSITE);
Mutex::new(InterestCacheConfig::disabled())
};
}
static CONFIG: Lazy<Mutex<InterestCacheConfig>> = Lazy::new(|| {
tracing_core::callsite::register(&SENTINEL_CALLSITE);
Mutex::new(InterestCacheConfig::disabled())
});

thread_local! {
static STATE: RefCell<State> = {
Expand Down Expand Up @@ -236,10 +235,7 @@ mod tests {

fn lock_for_test() -> impl Drop {
// We need to make sure only one test runs at a time.

lazy_static::lazy_static! {
static ref LOCK: Mutex<()> = Mutex::new(());
}
static LOCK: Lazy<Mutex<()>> = Lazy::new(Mutex::new);

match LOCK.lock() {
Ok(guard) => guard,
Expand Down

0 comments on commit b243b55

Please sign in to comment.