Skip to content

Commit

Permalink
Reduce branching when writing inline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Sep 16, 2024
1 parent 04aa8ce commit a72408a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[build]
rustflags = ["-C", "target-cpu=native"]
rustdocflags = ["--cfg", "docsrs"]

[env]
16 changes: 8 additions & 8 deletions benchlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use self::generate::*;
macro_rules! bench_dataset {
($ty:ty = $generate:expr) => {
#[$crate::divan::bench(min_time = std::time::Duration::from_secs(3))]
pub fn serialize(bencher: $crate::divan::Bencher) {
pub fn ser(bencher: $crate::divan::Bencher) {
let data = $generate;
let mut bytes = rkyv::util::AlignedVec::<16>::new();

Expand All @@ -19,7 +19,7 @@ macro_rules! bench_dataset {
buffer.clear();

bytes = $crate::divan::black_box(
rkyv::api::high::to_bytes_in::<_, rkyv::rancor::Panic>(
rkyv::api::high::to_bytes_in::<_, rkyv::rancor::Failure>(
$crate::divan::black_box(&data),
$crate::divan::black_box(buffer),
)
Expand All @@ -29,27 +29,27 @@ macro_rules! bench_dataset {
}

#[$crate::divan::bench(min_time = std::time::Duration::from_secs(3))]
pub fn deserialize(bencher: $crate::divan::Bencher) {
pub fn de(bencher: $crate::divan::Bencher) {
let bytes =
rkyv::api::high::to_bytes::<rkyv::rancor::Panic>(&$generate)
rkyv::api::high::to_bytes::<rkyv::rancor::Failure>(&$generate)
.unwrap();

bencher.bench_local(|| {
rkyv::from_bytes::<$ty, rkyv::rancor::Panic>(
rkyv::from_bytes::<$ty, rkyv::rancor::Failure>(
$crate::divan::black_box(&bytes),
)
.unwrap()
})
}

#[$crate::divan::bench(min_time = std::time::Duration::from_secs(3))]
pub fn check_bytes(bencher: $crate::divan::Bencher) {
pub fn check(bencher: $crate::divan::Bencher) {
let bytes =
rkyv::api::high::to_bytes::<rkyv::rancor::Panic>(&$generate)
rkyv::api::high::to_bytes::<rkyv::rancor::Failure>(&$generate)
.unwrap();

bencher.bench_local(|| {
rkyv::access::<rkyv::Archived<$ty>, rkyv::rancor::Panic>(
rkyv::access::<rkyv::Archived<$ty>, rkyv::rancor::Failure>(
$crate::divan::black_box(&bytes),
)
})
Expand Down
19 changes: 12 additions & 7 deletions rkyv/src/string/repr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! An archived string representation that supports inlining short strings.

use core::{marker::PhantomPinned, mem, ptr, slice, str};
use core::{
marker::PhantomPinned,
mem,
ptr::{self, copy_nonoverlapping, write_bytes},
slice, str,
};

use munge::munge;
use rancor::{Panic, ResultExt as _, Source};
Expand Down Expand Up @@ -175,12 +180,12 @@ impl ArchivedStringRepr {
// valid pointer to bytes because it is a subfield of `out` which the
// caller has guaranteed points to a valid location.
unsafe {
for i in 0..value.len() {
out_bytes.cast::<u8>().add(i).write(value.as_bytes()[i]);
}
for i in value.len()..INLINE_CAPACITY {
out_bytes.cast::<u8>().add(i).write(0xff);
}
write_bytes(out_bytes, 0xff, 1);
copy_nonoverlapping(
value.as_bytes().as_ptr(),
out_bytes.cast(),
value.len(),
);
}
}

Expand Down
18 changes: 8 additions & 10 deletions rkyv/src/util/alloc/aligned_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,15 @@ impl<const ALIGNMENT: usize> AlignedVec<ALIGNMENT> {
/// assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
/// ```
pub fn extend_from_slice(&mut self, other: &[u8]) {
if !other.is_empty() {
self.reserve(other.len());
unsafe {
core::ptr::copy_nonoverlapping(
other.as_ptr(),
self.as_mut_ptr().add(self.len()),
other.len(),
);
}
self.len += other.len();
self.reserve(other.len());
unsafe {
core::ptr::copy_nonoverlapping(
other.as_ptr(),
self.as_mut_ptr().add(self.len()),
other.len(),
);
}
self.len += other.len();
}

/// Removes the last element from a vector and returns it, or `None` if it
Expand Down

0 comments on commit a72408a

Please sign in to comment.