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

Improve performance of String and Vec<u8> #58

Merged
merged 19 commits into from
Mar 13, 2020
4 changes: 2 additions & 2 deletions borsh-rs/borsh-derive-internal/src/enum_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn enum_de(input: &ItemEnum) -> syn::Result<TokenStream2> {
if let Some(method_ident) = init_method {
Ok(quote! {
impl #generics borsh::de::BorshDeserialize for #name #generics where #deserializable_field_types {
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
fn deserialize<R: std::io::BufRead>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
#variant_idx
let mut return_value = match variant_idx {
#variant_arms
Expand All @@ -82,7 +82,7 @@ pub fn enum_de(input: &ItemEnum) -> syn::Result<TokenStream2> {
} else {
Ok(quote! {
impl #generics borsh::de::BorshDeserialize for #name #generics where #deserializable_field_types {
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
fn deserialize<R: std::io::BufRead>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
#variant_idx
let return_value = match variant_idx {
#variant_arms
Expand Down
4 changes: 2 additions & 2 deletions borsh-rs/borsh-derive-internal/src/struct_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn struct_de(input: &ItemStruct) -> syn::Result<TokenStream2> {
if let Some(method_ident) = init_method {
Ok(quote! {
impl #generics borsh::de::BorshDeserialize for #name #generics where #deserializable_field_types {
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
fn deserialize<R: std::io::BufRead>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
let mut return_value = #return_value;
return_value.#method_ident();
Ok(return_value)
Expand All @@ -64,7 +64,7 @@ pub fn struct_de(input: &ItemStruct) -> syn::Result<TokenStream2> {
} else {
Ok(quote! {
impl #generics borsh::de::BorshDeserialize for #name #generics where #deserializable_field_types {
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
fn deserialize<R: std::io::BufRead>(reader: &mut R) -> std::result::Result<Self, std::io::Error> {
Ok(#return_value)
}
}
Expand Down
10 changes: 10 additions & 0 deletions borsh-rs/borsh/src/de/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ pub fn cautious<T>(hint: u32) -> usize {
let el_size = std::mem::size_of::<T>() as u32;
std::cmp::max(std::cmp::min(hint, 4096/el_size), 1u32) as _
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_cautious_u8() {
assert_eq!(cautious::<u8>(10), 10);
}
}
Loading