From 273293d66e6105572829a8798032703481139ea4 Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Tue, 20 Aug 2024 00:33:15 -0400 Subject: [PATCH] Use `ptr::write_bytes` to fill `Array`. --- src/shard.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/shard.rs b/src/shard.rs index b77a9fc..2ea3ba8 100644 --- a/src/shard.rs +++ b/src/shard.rs @@ -271,12 +271,18 @@ where { pub(crate) fn new() -> Self { let mut shards = Vec::with_capacity(C::MAX_SHARDS); - for _ in 0..C::MAX_SHARDS { - // XXX(eliza): T_T this could be avoided with maybeuninit or something... - shards.push(Ptr::null()); + + // Use ptr::write_bytes to fill the Vec very fast. + // Safety: This is safe because ptr::null() is defined as having an address of 0 in Rust, and being equivalent + // to `MaybeUninit::<*const T>::zeroed().assume_init()`, and the `AtomicPtr` that underlies our ptr type is + // just an `UnsafeCell`, which is `#[repr(transparent)]`. + unsafe { + ptr::write_bytes::>(shards.as_mut_ptr(), 0, C::MAX_SHARDS); + shards.set_len(C::MAX_SHARDS); } + Self { - shards: shards.into(), + shards: shards.into_boxed_slice(), max: AtomicUsize::new(0), } } @@ -370,11 +376,6 @@ impl fmt::Debug for Array { // === impl Ptr === impl Ptr { - #[inline] - fn null() -> Self { - Self(AtomicPtr::new(ptr::null_mut())) - } - #[inline] fn load(&self, order: Ordering) -> Option<&Shard> { let ptr = self.0.load(order);