From 5f0bd2c4c709b5d85b47c87d4bb742b13042893c Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Wed, 8 Jul 2020 15:32:03 -0600 Subject: [PATCH] clean up read_bytes --- src/de.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/de.rs b/src/de.rs index f2eca60..26b7f89 100644 --- a/src/de.rs +++ b/src/de.rs @@ -7,31 +7,28 @@ use std::{ str, u16, u32, u64, u8, }; -// We could use pagesize to get this across platforms, but 4K is a reasonable value -const PAGESIZE: usize = 4096; - -pub struct Deserializer { - reader: R, -} - /// Try and return a Vec of `len` bytes from a Reader #[inline] fn read_bytes(reader: R, len: usize) -> Result, std::io::Error> { - let capacity = if len > PAGESIZE { PAGESIZE } else { len }; - // Allocate at most one page to start with. Growing a Vec is fairly efficient once you get out + // Allocate at most 4096 bytes to start with. Growing a Vec is fairly efficient once you get out // of the region of the first few hundred bytes. - let mut buffer: Vec = Vec::with_capacity(capacity); + let capacity = len.min(4096); + let mut buffer = Vec::with_capacity(capacity); let read = reader.take(len as u64).read_to_end(&mut buffer)?; if read < len { Err(std::io::Error::new( std::io::ErrorKind::UnexpectedEof, - "Unexpected EOF reading number of bytes expected in field prefix" + "Unexpected EOF reading number of bytes expected in field prefix", )) } else { Ok(buffer) } } +pub struct Deserializer { + reader: R, +} + impl Deserializer { pub fn new(reader: R) -> Self { Deserializer { reader }