Skip to content

Commit

Permalink
New reader API that allows providing the scratch buffer.
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Turner-Trauring <[email protected]>
  • Loading branch information
pythonspeed authored and rjzak committed Oct 4, 2023
1 parent fa74215 commit 09134e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
22 changes: 20 additions & 2 deletions ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,17 +815,35 @@ where
}
}

/// Deserializes as CBOR from a type with [`impl ciborium_io::Read`](ciborium_io::Read)
/// Deserializes as CBOR from a type with [`impl
/// ciborium_io::Read`](ciborium_io::Read) using a 4KB buffer on the stack.
///
/// If you want to deserialize faster at the cost of more memory, consider using
/// [`from_reader_with_buffer`](from_reader_with_buffer) with a larger buffer,
/// for example 64KB.
#[inline]
pub fn from_reader<T: de::DeserializeOwned, R: Read>(reader: R) -> Result<T, Error<R::Error>>
where
R::Error: core::fmt::Debug,
{
let mut scratch = [0; 4096];
from_reader_with_buffer(reader, &mut scratch)
}

/// Deserializes as CBOR from a type with [`impl
/// ciborium_io::Read`](ciborium_io::Read), using a caller-specific buffer as a
/// temporary scratch space.
#[inline]
pub fn from_reader_with_buffer<T: de::DeserializeOwned, R: Read>(
reader: R,
scratch_buffer: &mut [u8],
) -> Result<T, Error<R::Error>>
where
R::Error: core::fmt::Debug,
{
let mut reader = Deserializer {
decoder: reader.into(),
scratch: &mut scratch,
scratch: scratch_buffer,
recurse: 256,
};

Expand Down
2 changes: 2 additions & 0 deletions ciborium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub mod value;
// Re-export the [items recommended by serde](https://serde.rs/conventions.html).
#[doc(inline)]
pub use crate::de::from_reader;
#[doc(inline)]
pub use crate::de::from_reader_with_buffer;

#[doc(inline)]
pub use crate::ser::into_writer;
Expand Down
7 changes: 6 additions & 1 deletion ciborium/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::convert::TryFrom;
use std::fmt::Debug;

use ciborium::value::Value;
use ciborium::{cbor, de::from_reader, ser::into_writer};
use ciborium::{cbor, de::from_reader, de::from_reader_with_buffer, ser::into_writer};

use rstest::rstest;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
Expand Down Expand Up @@ -309,6 +309,11 @@ fn codec<'de, T: Serialize + Clone, V: Debug + PartialEq + DeserializeOwned, F:
eprintln!("{:x?} == {:x?}", &value, &decoded);
assert!(veq(&value, &decoded));

let mut scratch = vec![0; 65536];
let decoded: Value = from_reader_with_buffer(&bytes[..], &mut scratch).unwrap();
eprintln!("{:x?} == {:x?}", &value, &decoded);
assert!(veq(&value, &decoded));

let decoded: V = value.deserialized().unwrap();
let answer = equality(input);
eprintln!("{:x?} == {:x?}", answer, decoded);
Expand Down

0 comments on commit 09134e6

Please sign in to comment.