diff --git a/src/atom.rs b/src/atom.rs index c02651b..7856947 100644 --- a/src/atom.rs +++ b/src/atom.rs @@ -200,8 +200,7 @@ impl<'a, Static: StaticAtomSet> From> for Atom { phantom: PhantomData, } } else { - let ptr: std::ptr::NonNull = - DYNAMIC_SET.lock().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 { @@ -237,9 +236,7 @@ impl Drop for Atom { // Out of line to guide inlining. fn drop_slow(this: &mut Atom) { - DYNAMIC_SET - .lock() - .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 229a79f..6ea4ba6 100644 --- a/src/dynamic_set.rs +++ b/src/dynamic_set.rs @@ -19,7 +19,7 @@ const NB_BUCKETS: usize = 1 << 12; // 4096 const BUCKET_MASK: u32 = (1 << 12) - 1; pub(crate) struct Set { - buckets: Box<[Option>; NB_BUCKETS]>, + buckets: Box<[Mutex>>]>, } pub(crate) struct Entry { @@ -38,22 +38,24 @@ fn entry_alignment_is_sufficient() { assert!(mem::align_of::() >= ENTRY_ALIGNMENT); } -pub(crate) static DYNAMIC_SET: Lazy> = Lazy::new(|| { - Mutex::new({ - type T = Option>; - let _static_assert_size_eq = std::mem::transmute::; - let vec = std::mem::ManuallyDrop::new(vec![0_usize; NB_BUCKETS]); - Set { - buckets: unsafe { Box::from_raw(vec.as_ptr() as *mut [T; NB_BUCKETS]) }, - } - }) +pub(crate) static DYNAMIC_SET: Lazy = Lazy::new(|| { + // 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 } }); impl Set { - pub(crate) fn insert(&mut self, string: Cow, hash: u32) -> NonNull { + pub(crate) fn insert(&self, string: Cow, hash: u32) -> NonNull { let bucket_index = (hash & BUCKET_MASK) as usize; + let mut linked_list = self.buckets[bucket_index].lock(); + { - let mut ptr: Option<&mut Box> = self.buckets[bucket_index].as_mut(); + let mut ptr: Option<&mut Box> = linked_list.as_mut(); while let Some(entry) = ptr.take() { if entry.hash == hash && *entry.string == *string { @@ -74,25 +76,25 @@ impl Set { debug_assert!(mem::align_of::() >= ENTRY_ALIGNMENT); let string = string.into_owned(); let mut entry = Box::new(Entry { - next_in_bucket: self.buckets[bucket_index].take(), + next_in_bucket: linked_list.take(), hash, ref_count: AtomicIsize::new(1), string: string.into_boxed_str(), }); let ptr = NonNull::from(&mut *entry); - self.buckets[bucket_index] = Some(entry); - + *linked_list = Some(entry); ptr } - pub(crate) fn remove(&mut self, ptr: *mut Entry) { + pub(crate) fn remove(&self, ptr: *mut Entry) { let bucket_index = { let value: &Entry = unsafe { &*ptr }; debug_assert!(value.ref_count.load(SeqCst) == 0); (value.hash & BUCKET_MASK) as usize }; - let mut current: &mut Option> = &mut self.buckets[bucket_index]; + let mut linked_list = self.buckets[bucket_index].lock(); + let mut current: &mut Option> = &mut linked_list; while let Some(entry_ptr) = current.as_mut() { let entry_ptr: *mut Entry = &mut **entry_ptr;