Skip to content

Commit fdfea72

Browse files
committed
Perf: Add BufferedIoRead for ~28.6% faster from_reader parsing
1 parent 4468b00 commit fdfea72

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/de.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ use serde::forward_to_deserialize_any;
1919
#[cfg(feature = "arbitrary_precision")]
2020
use crate::number::NumberDeserializer;
2121

22-
pub use crate::read::{BufferedIoRead, Read, SliceRead, StrRead};
22+
pub use crate::read::{Read, SliceRead, StrRead};
2323

2424
#[cfg(feature = "std")]
2525
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
26-
pub use crate::read::IoRead;
26+
pub use crate::read::{BufferedIoRead, IoRead};
2727

2828
//////////////////////////////////////////////////////////////////////////////
2929

@@ -2729,7 +2729,7 @@ where
27292729
/// parsing from a very slow I/O source or need to control the buffer
27302730
/// size (e.g., to use a larger 8KB buffer) or its allocation, you can
27312731
/// construct a [`read::BufferedIoRead`] manually with your own buffer
2732-
/// and pass it to the generic [`from_trait`] function.
2732+
/// and pass it to the Deserializer.
27332733
///
27342734
/// ### Features
27352735
///
@@ -2776,6 +2776,7 @@ where
27762776
R: crate::io::Read,
27772777
T: de::DeserializeOwned,
27782778
{
2779-
let b_rdr: read::BufferedIoRead<R> = read::BufferedIoRead::new(rdr, [0; _]);
2779+
let b_rdr: read::BufferedIoRead<R> =
2780+
read::BufferedIoRead::new(rdr, [0; read::AVERAGE_BUF_CAPACITY]);
27802781
from_trait(b_rdr)
27812782
}

src/read.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ impl<'a> Read<'a> for StrRead<'a> {
758758
//////////////////////////////////////////////////////////////////////////////
759759

760760
/// The default capacity of the internal buffer.
761-
const AVERAGE_BUF_CAPACITY: usize = 128; // 128 bytes
761+
#[cfg(feature = "std")]
762+
pub(crate) const AVERAGE_BUF_CAPACITY: usize = 128; // 128 bytes
762763

763764
/// A JSON input source that reads from a std::io stream, using an internal
764765
/// buffer to allow for `SliceRead`-like string parsing optimizations.
@@ -806,6 +807,7 @@ where
806807
raw_buffering_start_index: usize,
807808
}
808809

810+
#[cfg(feature = "std")]
809811
impl<R, B> BufferedIoRead<R, B>
810812
where
811813
R: io::Read,

tests/buffered_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn test_error_at_boundary() {
146146
assert_eq!(err.column(), 11); // Error is at the `\xFF`
147147
}
148148

149-
/// RawValue
149+
// ---- RawValue ----
150150

151151
#[cfg(feature = "raw_value")]
152152
#[derive(Deserialize)]

0 commit comments

Comments
 (0)