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

MSRV 1.70 + Replace once_cell::sync::Lazy with std::sync::OnceLock #287

Merged
merged 2 commits into from
Feb 1, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
}
} else {
Self::try_static_internal(&*string_to_add).unwrap_or_else(|hash| {
let ptr: std::ptr::NonNull<Entry> = DYNAMIC_SET.insert(string_to_add, hash.g);
let ptr: std::ptr::NonNull<Entry> = dynamic_set().insert(string_to_add, hash.g);
let data = ptr.as_ptr() as u64;
debug_assert!(0 == data & TAG_MASK);
Atom {
Expand Down Expand Up @@ -257,7 +257,7 @@ impl<Static> Drop for Atom<Static> {

// Out of line to guide inlining.
fn drop_slow<Static>(this: &mut Atom<Static>) {
DYNAMIC_SET.remove(this.unsafe_data.get() as *mut Entry);
dynamic_set().remove(this.unsafe_data.get() as *mut Entry);
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/dynamic_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,16 +38,20 @@ fn entry_alignment_is_sufficient() {
assert!(mem::align_of::<Entry>() >= ENTRY_ALIGNMENT);
}

pub(crate) static DYNAMIC_SET: Lazy<Set> = Lazy::new(|| {
pub(crate) fn dynamic_set() -> &'static Set {
// NOTE: Using const initialization for buckets breaks the small-stack test.
// ```
// // buckets: [Mutex<Option<Box<Entry>>>; NB_BUCKETS],
// const MUTEX: Mutex<Option<Box<Entry>>> = 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<Set> = 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<str>, hash: u32) -> NonNull<Entry> {
Expand Down
Loading