Skip to content

Commit

Permalink
Use ptr::write_bytes to fill Array.
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed Aug 20, 2024
1 parent ee45b4c commit 273293d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Ptr<T, C>>(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),
}
}
Expand Down Expand Up @@ -370,11 +376,6 @@ impl<T: fmt::Debug, C: cfg::Config> fmt::Debug for Array<T, C> {
// === impl Ptr ===

impl<T, C: cfg::Config> Ptr<T, C> {
#[inline]
fn null() -> Self {
Self(AtomicPtr::new(ptr::null_mut()))
}

#[inline]
fn load(&self, order: Ordering) -> Option<&Shard<T, C>> {
let ptr = self.0.load(order);
Expand Down

0 comments on commit 273293d

Please sign in to comment.