Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static with LazyLock #5716

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,6 @@ kvdb-memorydb = { version = "0.13.0" }
kvdb-rocksdb = { version = "0.19.0" }
kvdb-shared-tests = { version = "0.11.0" }
landlock = { version = "0.3.0" }
lazy_static = { version = "1.5.0" }
libc = { version = "0.2.155" }
libfuzzer-sys = { version = "0.4" }
libp2p = { version = "0.52.4" }
Expand Down
1 change: 0 additions & 1 deletion cumulus/pallets/parachain-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cumulus-primitives-proof-size-hostfunction = { workspace = true }
[dev-dependencies]
assert_matches = { workspace = true }
hex-literal = { workspace = true, default-features = true }
lazy_static = { workspace = true }
trie-standardmap = { workspace = true }
rand = { workspace = true, default-features = true }
futures = { workspace = true }
Expand Down
15 changes: 6 additions & 9 deletions cumulus/pallets/parachain-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use hex_literal::hex;
use rand::Rng;
use relay_chain::HrmpChannelId;
use sp_core::H256;
use std::sync::OnceLock;

#[test]
#[should_panic]
Expand Down Expand Up @@ -754,31 +755,27 @@ fn message_queue_chain() {
#[test]
#[cfg(not(feature = "runtime-benchmarks"))]
fn receive_dmp() {
lazy_static::lazy_static! {
static ref MSG: InboundDownwardMessage = InboundDownwardMessage {
sent_at: 1,
msg: b"down".to_vec(),
};
}
static MSG: OnceLock<InboundDownwardMessage> = OnceLock::new();
MSG.get_or_init(|| InboundDownwardMessage { sent_at: 1, msg: b"down".to_vec() });

BlockTests::new()
.with_relay_sproof_builder(|_, relay_block_num, sproof| match relay_block_num {
1 => {
sproof.dmq_mqc_head =
Some(MessageQueueChain::default().extend_downward(&MSG).head());
Some(MessageQueueChain::default().extend_downward(MSG.get().unwrap()).head());
},
_ => unreachable!(),
})
.with_inherent_data(|_, relay_block_num, data| match relay_block_num {
1 => {
data.downward_messages.push(MSG.clone());
data.downward_messages.push(MSG.get().unwrap().clone());
},
_ => unreachable!(),
})
.add(1, || {
HANDLED_DMP_MESSAGES.with(|m| {
let mut m = m.borrow_mut();
assert_eq!(&*m, &[(MSG.msg.clone())]);
assert_eq!(&*m, &[(MSG.get().unwrap().msg.clone())]);
m.clear();
});
});
Expand Down
1 change: 0 additions & 1 deletion cumulus/xcm/xcm-emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ workspace = true
codec = { workspace = true, default-features = true }
paste = { workspace = true, default-features = true }
log = { workspace = true }
lazy_static = { workspace = true }
impl-trait-for-tuples = { workspace = true }
array-bytes = { workspace = true }

Expand Down
46 changes: 23 additions & 23 deletions cumulus/xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ extern crate alloc;

pub use array_bytes;
pub use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
pub use lazy_static::lazy_static;
pub use log;
pub use paste;
pub use std::{
any::type_name, collections::HashMap, error::Error, fmt, marker::PhantomData, ops::Deref,
sync::Mutex,
any::type_name,
collections::HashMap,
error::Error,
fmt,
marker::PhantomData,
ops::Deref,
sync::{LazyLock, Mutex},
};

// Substrate
Expand Down Expand Up @@ -443,10 +447,8 @@ macro_rules! __impl_test_ext_for_relay_chain {
= $crate::RefCell::new($crate::TestExternalities::new($genesis));
}

$crate::lazy_static! {
pub static ref $global_ext: $crate::Mutex<$crate::RefCell<$crate::HashMap<String, $crate::TestExternalities>>>
= $crate::Mutex::new($crate::RefCell::new($crate::HashMap::new()));
}
pub static $global_ext: $crate::LazyLock<$crate::Mutex<$crate::RefCell<$crate::HashMap<String, $crate::TestExternalities>>>>
= $crate::LazyLock::new(|| $crate::Mutex::new($crate::RefCell::new($crate::HashMap::new())));

impl<$network: $crate::Network> $crate::TestExt for $name<$network> {
fn build_new_ext(storage: $crate::Storage) -> $crate::TestExternalities {
Expand Down Expand Up @@ -478,10 +480,10 @@ macro_rules! __impl_test_ext_for_relay_chain {
v.take()
});

// Get TestExternality from lazy_static
// Get TestExternality from LazyLock
let global_ext_guard = $global_ext.lock().unwrap();

// Replace TestExternality in lazy_static by TestExternality from thread_local
// Replace TestExternality in LazyLock by TestExternality from thread_local
global_ext_guard.deref().borrow_mut().insert(id.to_string(), local_ext);
}

Expand All @@ -490,10 +492,10 @@ macro_rules! __impl_test_ext_for_relay_chain {

let mut global_ext_unlocked = false;

// Keep the mutex unlocked until TesExternality from lazy_static
// Keep the mutex unlocked until TesExternality from LazyLock
// has been updated
while !global_ext_unlocked {
// Get TesExternality from lazy_static
// Get TesExternality from LazyLock
let global_ext_result = $global_ext.try_lock();

if let Ok(global_ext_guard) = global_ext_result {
Expand All @@ -506,10 +508,10 @@ macro_rules! __impl_test_ext_for_relay_chain {
}
}

// Now that we know that lazy_static TestExt has been updated, we lock its mutex
// Now that we know that TestExt has been updated, we lock its mutex
let mut global_ext_guard = $global_ext.lock().unwrap();

// and set TesExternality from lazy_static into TesExternality for local_thread
// and set TesExternality from LazyLock into TesExternality for local_thread
let global_ext = global_ext_guard.deref();

$local_ext.with(|v| {
Expand Down Expand Up @@ -744,10 +746,8 @@ macro_rules! __impl_test_ext_for_parachain {
= $crate::RefCell::new($crate::TestExternalities::new($genesis));
}

$crate::lazy_static! {
pub static ref $global_ext: $crate::Mutex<$crate::RefCell<$crate::HashMap<String, $crate::TestExternalities>>>
= $crate::Mutex::new($crate::RefCell::new($crate::HashMap::new()));
}
pub static $global_ext: $crate::LazyLock<$crate::Mutex<$crate::RefCell<$crate::HashMap<String, $crate::TestExternalities>>>>
= $crate::LazyLock::new(|| $crate::Mutex::new($crate::RefCell::new($crate::HashMap::new())));

impl<$network: $crate::Network> $crate::TestExt for $name<$network> {
fn build_new_ext(storage: $crate::Storage) -> $crate::TestExternalities {
Expand Down Expand Up @@ -777,10 +777,10 @@ macro_rules! __impl_test_ext_for_parachain {
v.take()
});

// Get TestExternality from lazy_static
// Get TestExternality from LazyLock
let global_ext_guard = $global_ext.lock().unwrap();

// Replace TestExternality in lazy_static by TestExternality from thread_local
// Replace TestExternality in LazyLock by TestExternality from thread_local
global_ext_guard.deref().borrow_mut().insert(id.to_string(), local_ext);
}

Expand All @@ -789,10 +789,10 @@ macro_rules! __impl_test_ext_for_parachain {

let mut global_ext_unlocked = false;

// Keep the mutex unlocked until TesExternality from lazy_static
// Keep the mutex unlocked until TesExternality from LazyLock
// has been updated
while !global_ext_unlocked {
// Get TesExternality from lazy_static
// Get TesExternality from LazyLock
let global_ext_result = $global_ext.try_lock();

if let Ok(global_ext_guard) = global_ext_result {
Expand All @@ -805,10 +805,10 @@ macro_rules! __impl_test_ext_for_parachain {
}
}

// Now that we know that lazy_static TestExt has been updated, we lock its mutex
// Now that we know that TestExt has been updated, we lock its mutex
let mut global_ext_guard = $global_ext.lock().unwrap();

// and set TesExternality from lazy_static into TesExternality for local_thread
// and set TesExternality from LazyLock into TesExternality for local_thread
let global_ext = global_ext_guard.deref();

$local_ext.with(|v| {
Expand Down
1 change: 0 additions & 1 deletion polkadot/node/jaeger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ workspace = true

[dependencies]
mick-jaeger = { workspace = true }
lazy_static = { workspace = true }
parking_lot = { workspace = true, default-features = true }
polkadot-primitives = { workspace = true, default-features = true }
polkadot-node-primitives = { workspace = true, default-features = true }
Expand Down
9 changes: 5 additions & 4 deletions polkadot/node/jaeger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ use self::spans::TraceIdentifier;
use sp_core::traits::SpawnNamed;

use parking_lot::RwLock;
use std::{result, sync::Arc};
use std::{
result,
sync::{Arc, LazyLock},
};

lazy_static::lazy_static! {
static ref INSTANCE: RwLock<Jaeger> = RwLock::new(Jaeger::None);
}
static INSTANCE: LazyLock<RwLock<Jaeger>> = LazyLock::new(|| RwLock::new(Jaeger::None));

/// Stateful convenience wrapper around [`mick_jaeger`].
pub enum Jaeger {
Expand Down
1 change: 0 additions & 1 deletion polkadot/node/network/dispute-distribution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ sp-tracing = { workspace = true, default-features = true }
sc-keystore = { workspace = true, default-features = true }
futures-timer = { workspace = true }
assert_matches = { workspace = true }
lazy_static = { workspace = true }
polkadot-primitives-test-helpers = { workspace = true }
Loading
Loading