Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Add into_bytes to Storable to avoid cloning where possible #249

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ where
/// key.to_bytes().len() <= max_size(Key)
/// value.to_bytes().len() <= max_size(Value)
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
let value = value.to_bytes_checked().into_owned();
let value = value.into_bytes_checked();

let root = if self.root_addr == NULL {
// No root present. Allocate one.
Expand Down
69 changes: 46 additions & 23 deletions src/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod tuples;
mod tests;

/// A trait with convenience methods for storing an element into a stable structure.
pub trait Storable {
pub trait Storable: Sized {
/// Converts an element into bytes.
///
/// NOTE: `Cow` is used here to avoid unnecessary cloning.
Expand All @@ -22,36 +22,55 @@ pub trait Storable {
/// The size bounds of the type.
const BOUND: Bound;

/// Consumes self and converts it into bytes.
/// Uses `to_bytes` by default.
fn into_bytes(self) -> Vec<u8> {
self.to_bytes().into_owned()
}

/// Like `to_bytes`, but includes additional checks to ensure the element's serialized bytes
/// are within the element's bounds.
fn to_bytes_checked(&self) -> Cow<[u8]> {
let bytes = self.to_bytes();
if let Bound::Bounded {
max_size,
is_fixed_size,
} = Self::BOUND
{
if is_fixed_size {
assert_eq!(
bytes.len(),
max_size as usize,
"expected a fixed-size element with length {} bytes, but found {} bytes",
max_size,
bytes.len()
);
} else {
assert!(
bytes.len() <= max_size as usize,
"expected an element with length <= {} bytes, but found {} bytes",
max_size,
bytes.len()
);
}
}
check_bounds(&bytes, Self::BOUND);
bytes
}

/// Like `into_bytes`, but includes additional checks to ensure the element's serialized bytes
/// are within the element's bounds.
fn into_bytes_checked(self) -> Vec<u8> {
let bytes = self.into_bytes();
check_bounds(&bytes, Self::BOUND);
bytes
}
}

#[inline]
fn check_bounds(bytes: &[u8], bounds: Bound) {
if let Bound::Bounded {
max_size,
is_fixed_size,
} = bounds
{
if is_fixed_size {
assert_eq!(
bytes.len(),
max_size as usize,
"expected a fixed-size element with length {} bytes, but found {} bytes",
max_size,
bytes.len()
);
} else {
assert!(
bytes.len() <= max_size as usize,
"expected an element with length <= {} bytes, but found {} bytes",
max_size,
bytes.len()
);
}
}
}

#[derive(Debug, PartialEq)]
/// States whether the type's size is bounded or unbounded.
pub enum Bound {
Expand Down Expand Up @@ -227,6 +246,10 @@ impl Storable for Vec<u8> {
bytes.to_vec()
}

fn into_bytes(self) -> Vec<u8> {
self
}

const BOUND: Bound = Bound::Unbounded;
}

Expand Down
Loading