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

Key deserializer improvements #467

Merged
merged 7 commits into from
Oct 4, 2021
Merged
Changes from 1 commit
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
92 changes: 15 additions & 77 deletions packages/storage-plus/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ use crate::keys::{IntKey, TimestampKey};
pub trait KeyDeserialize {
type Output: Sized;

fn from_slice(value: &[u8]) -> StdResult<Self::Output>;
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output>;

fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
Self::from_vec(value.to_vec())
}
}

impl KeyDeserialize for () {
type Output = ();

#[inline(always)]
fn from_slice(_value: &[u8]) -> StdResult<Self::Output> {
Ok(())
}

#[inline(always)]
fn from_vec(_value: Vec<u8>) -> StdResult<Self::Output> {
Ok(())
Expand All @@ -29,11 +27,6 @@ impl KeyDeserialize for () {
impl KeyDeserialize for Vec<u8> {
type Output = Vec<u8>;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
Ok(value.to_vec())
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Ok(value)
Expand All @@ -43,11 +36,6 @@ impl KeyDeserialize for Vec<u8> {
impl KeyDeserialize for &Vec<u8> {
type Output = Vec<u8>;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
<Vec<u8>>::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Ok(value)
Expand All @@ -57,11 +45,6 @@ impl KeyDeserialize for &Vec<u8> {
impl KeyDeserialize for &[u8] {
type Output = Vec<u8>;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
<Vec<u8>>::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Ok(value)
Expand All @@ -72,71 +55,46 @@ impl KeyDeserialize for String {
type Output = String;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
String::from_utf8(value.to_vec())
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
String::from_utf8(value)
// FIXME: Add and use StdError utf-8 error From helper
.map_err(|err| StdError::generic_err(err.to_string()))
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
String::from_slice(value.as_slice())
}
}

impl KeyDeserialize for &String {
type Output = String;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
String::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
String::from_slice(value.as_slice())
String::from_vec(value)
}
}

impl KeyDeserialize for &str {
type Output = String;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
String::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
String::from_slice(value.as_slice())
String::from_vec(value)
}
}

impl KeyDeserialize for Addr {
type Output = Addr;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
Ok(Addr::unchecked(String::from_slice(value)?))
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Addr::from_slice(value.as_slice())
Ok(Addr::unchecked(String::from_vec(value)?))
}
}

impl KeyDeserialize for &Addr {
type Output = Addr;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
Addr::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Addr::from_slice(value.as_slice())
Addr::from_vec(value)
}
}

Expand All @@ -146,16 +104,11 @@ macro_rules! integer_de {
type Output = $t;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
Ok(<$t>::from_be_bytes(value.try_into()
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Ok(<$t>::from_be_bytes(value.as_slice().try_into()
// FIXME: Add and use StdError try-from error From helper
.map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?))
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
<IntKey<$t>>::from_slice(value.as_slice())
}
})*
}
}
Expand All @@ -165,22 +118,17 @@ integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
impl KeyDeserialize for TimestampKey {
type Output = u64;

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
<IntKey<u64>>::from_slice(value)
}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
<IntKey<u64>>::from_slice(value.as_slice())
<IntKey<u64>>::from_vec(value)
}
}

impl<T: KeyDeserialize, U: KeyDeserialize> KeyDeserialize for (T, U) {
type Output = (T::Output, U::Output);

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
let (len, data) = value.split_at(2);
let t_len = u16::from_be_bytes(
len.try_into()
Expand All @@ -191,18 +139,13 @@ impl<T: KeyDeserialize, U: KeyDeserialize> KeyDeserialize for (T, U) {

Ok((T::from_slice(t)?, U::from_slice(u)?))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm.. could you use from_vec here as well?

I guess you would need value.split_off rather than split_at. But should be more consistent/efficient

}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
<(T, U)>::from_slice(value.as_slice())
}
}

impl<T: KeyDeserialize, U: KeyDeserialize, V: KeyDeserialize> KeyDeserialize for (T, U, V) {
type Output = (T::Output, U::Output, V::Output);

#[inline(always)]
fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
let (len, data) = value.split_at(2);
let t_len = u16::from_be_bytes(
len.try_into()
Expand All @@ -221,11 +164,6 @@ impl<T: KeyDeserialize, U: KeyDeserialize, V: KeyDeserialize> KeyDeserialize for

Ok((T::from_slice(t)?, U::from_slice(u)?, V::from_slice(v)?))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the (T, U) tuples (from_vec)

Copy link
Contributor Author

@maurolacy maurolacy Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it. It's a little unclear with the var names. And, I doubt it's more efficient.

If we wanted a self-contained version (that is, one that doesn't depend on from_slice), we could always use from_vec(t.to_vec()) and still work with slices / references in the mean time.

Copy link
Contributor Author

@maurolacy maurolacy Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, for triples, the version with split_off() has four vec allocations instead of three; because of the small vec for the length of v. Plus, it requieres a conversion to to_slice() for calling try_into(). Plus mut vecs.

I really like the previous version better.

}

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
<(T, U, V)>::from_slice(value.as_slice())
}
}

#[cfg(test)]
Expand Down