Skip to content

Commit

Permalink
performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Apr 12, 2024
1 parent e8ee08a commit b1da164
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ impl<R: Read> Deserializer<R> {
})
}

fn read_payload(&mut self, header: Header) -> Result<Vec<u8>> {
let mut buf = vec![0; header.payload_size];
self.reader.read_exact(&mut buf)?;
Ok(buf)
fn read_payload_string(&mut self, header: Header) -> Result<String> {
let mut str = String::with_capacity(header.payload_size);
let read = self.reader_with_limit(header)?.read_to_string(&mut str)?;
assert_eq!(read, header.payload_size);
Ok(str)
}

fn drop_payload(&mut self, header: Header) -> Result<ElementType> {
Expand Down Expand Up @@ -152,8 +153,16 @@ impl<R: Read> Deserializer<R> {
where
for<'a> T: Deserialize<'a>,
{
let mut reader = self.reader_with_limit(header)?;
Ok(crate::json::parse_json(&mut reader)?)
if header.payload_size <= 8 {
// micro-optimization: read small payloads into a stack buffer
let mut buf = [0u8; 8];
let smallbuf = &mut buf[..header.payload_size];
self.reader.read_exact(smallbuf)?;
Ok(crate::json::parse_json_slice(smallbuf)?)
} else {
let mut reader = self.reader_with_limit(header)?;
Ok(crate::json::parse_json(&mut reader)?)
}
}

fn read_json5_compatible<T>(&mut self, header: Header) -> Result<T>
Expand Down Expand Up @@ -194,7 +203,7 @@ impl<R: Read> Deserializer<R> {
fn read_string(&mut self, header: Header) -> Result<String> {
match header.element_type {
ElementType::Text | ElementType::TextRaw => {
Ok(String::from_utf8(self.read_payload(header)?)?)
self.read_payload_string(header)
}
ElementType::TextJ => self.read_json_compatible_string(header),
ElementType::Text5 => self.read_json5_compatible_string(header),
Expand Down
4 changes: 4 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#[cfg(feature = "serde_json")]
pub(crate) use serde_json::from_reader as parse_json;
#[cfg(feature = "serde_json")]
pub(crate) use serde_json::from_slice as parse_json_slice;
#[cfg(feature = "serde_json")]
pub(crate) type JsonError = serde_json::Error;

#[cfg(not(feature = "serde_json"))]
pub(crate) use serde_json5::from_reader as parse_json;
#[cfg(not(feature = "serde_json"))]
pub(crate) use serde_json5::from_slice as parse_json_slice;
#[cfg(not(feature = "serde_json"))]
pub(crate) type JsonError = serde_json5::Error;

#[cfg(feature = "serde_json5")]
Expand Down

0 comments on commit b1da164

Please sign in to comment.