Skip to content

Commit

Permalink
Replace use of optimized pad function
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Sep 16, 2024
1 parent a72408a commit cf5b389
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions rkyv/src/collections/swiss_table/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use core::{
error::Error,
fmt,
marker::PhantomData,
mem::size_of,
mem::{size_of, MaybeUninit},
ptr::{self, null, NonNull},
slice::from_raw_parts,
};

use munge::munge;
Expand Down Expand Up @@ -446,6 +447,16 @@ impl<T> ArchivedHashTable<T> {
}
}

let mut zeros = MaybeUninit::<T>::uninit();
unsafe {
zeros.as_mut_ptr().write_bytes(0, 1);
}
let zeros = unsafe {
from_raw_parts(
zeros.as_ptr().cast::<u8>(),
size_of::<T>(),
)
};
SerVec::with_capacity(
serializer,
len,
Expand All @@ -471,7 +482,7 @@ impl<T> ArchivedHashTable<T> {
)?;
}
} else {
serializer.pad(size_of::<T>())?;
serializer.write(zeros)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion rkyv/src/ser/writer/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
};

#[test]
fn zeroes_padding() {
fn zeros_padding() {
use core::mem::size_of;

use crate::{Archive, Serialize};
Expand Down
8 changes: 4 additions & 4 deletions rkyv/src/ser/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ where
pub trait WriterExt<E>: Writer<E> {
/// Advances the given number of bytes as padding.
fn pad(&mut self, padding: usize) -> Result<(), E> {
const MAX_ZEROES: usize = 32;
const ZEROES: [u8; MAX_ZEROES] = [0; MAX_ZEROES];
debug_assert!(padding < MAX_ZEROES);
const MAX_ZEROS: usize = 32;
const ZEROS: [u8; MAX_ZEROS] = [0; MAX_ZEROS];
debug_assert!(padding < MAX_ZEROS);

self.write(&ZEROES[0..padding])
self.write(&ZEROS[0..padding])
}

/// Aligns the position of the serializer to the given alignment.
Expand Down

0 comments on commit cf5b389

Please sign in to comment.