From e03f29061d1359adae2741f3d20f1184a636221c Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sat, 1 Feb 2025 14:43:21 +0100 Subject: [PATCH] MSRV 1.70 + Replace `once_cell::sync::Lazy` with `std::sync::OnceLock` (#287) * Update MSRV to 1.70 * Replace `once_cell::sync::Lazy` with `std::sync::OnceLock` --- .github/workflows/ci.yml | 2 +- Cargo.toml | 1 - src/atom.rs | 6 +++--- src/dynamic_set.rs | 14 +++++++++----- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa6f952..c5cdfa7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: - rust: [1.61.0, nightly, beta, stable] + rust: [1.70.0, nightly, beta, stable] steps: - uses: actions/checkout@v2 diff --git a/Cargo.toml b/Cargo.toml index df159f1..678eda3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ default = ["serde_support"] [dependencies] precomputed-hash = "0.1" -once_cell = "1.10.0" serde = { version = "1", optional = true } phf_shared = "0.11" new_debug_unreachable = "1.0.2" diff --git a/src/atom.rs b/src/atom.rs index 7e15357..adf5f62 100644 --- a/src/atom.rs +++ b/src/atom.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::dynamic_set::{Entry, DYNAMIC_SET}; +use crate::dynamic_set::{dynamic_set, Entry}; use crate::static_sets::StaticAtomSet; use debug_unreachable::debug_unreachable; @@ -221,7 +221,7 @@ impl<'a, Static: StaticAtomSet> From> for Atom { } } else { Self::try_static_internal(&*string_to_add).unwrap_or_else(|hash| { - let ptr: std::ptr::NonNull = DYNAMIC_SET.insert(string_to_add, hash.g); + let ptr: std::ptr::NonNull = dynamic_set().insert(string_to_add, hash.g); let data = ptr.as_ptr() as u64; debug_assert!(0 == data & TAG_MASK); Atom { @@ -257,7 +257,7 @@ impl Drop for Atom { // Out of line to guide inlining. fn drop_slow(this: &mut Atom) { - DYNAMIC_SET.remove(this.unsafe_data.get() as *mut Entry); + dynamic_set().remove(this.unsafe_data.get() as *mut Entry); } } } diff --git a/src/dynamic_set.rs b/src/dynamic_set.rs index 46e7a54..4442b4d 100644 --- a/src/dynamic_set.rs +++ b/src/dynamic_set.rs @@ -7,13 +7,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use once_cell::sync::Lazy; use parking_lot::Mutex; use std::borrow::Cow; use std::mem; use std::ptr::NonNull; use std::sync::atomic::AtomicIsize; use std::sync::atomic::Ordering::SeqCst; +use std::sync::OnceLock; const NB_BUCKETS: usize = 1 << 12; // 4096 const BUCKET_MASK: u32 = (1 << 12) - 1; @@ -38,16 +38,20 @@ fn entry_alignment_is_sufficient() { assert!(mem::align_of::() >= ENTRY_ALIGNMENT); } -pub(crate) static DYNAMIC_SET: Lazy = Lazy::new(|| { +pub(crate) fn dynamic_set() -> &'static Set { // NOTE: Using const initialization for buckets breaks the small-stack test. // ``` // // buckets: [Mutex>>; NB_BUCKETS], // const MUTEX: Mutex>> = Mutex::new(None); // let buckets = Box::new([MUTEX; NB_BUCKETS]); // ``` - let buckets = (0..NB_BUCKETS).map(|_| Mutex::new(None)).collect(); - Set { buckets } -}); + static DYNAMIC_SET: OnceLock = OnceLock::new(); + + DYNAMIC_SET.get_or_init(|| { + let buckets = (0..NB_BUCKETS).map(|_| Mutex::new(None)).collect(); + Set { buckets } + }) +} impl Set { pub(crate) fn insert(&self, string: Cow, hash: u32) -> NonNull {