Eager prealloc no inline (#127)
* make SizeLimit a trait * always preallocate
This commit is contained in:
parent
461a694bac
commit
2b465047b9
|
|
@ -142,6 +142,7 @@ impl SizeLimit for Bounded {
|
|||
Err(Box::new(ErrorKind::SizeLimit))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn limit(&self) -> Option<u64> { Some(self.0) }
|
||||
}
|
||||
|
|
@ -149,6 +150,7 @@ impl SizeLimit for Bounded {
|
|||
impl SizeLimit for Infinite {
|
||||
#[inline(always)]
|
||||
fn add(&mut self, _: u64) -> Result<()> { Ok (()) }
|
||||
|
||||
#[inline(always)]
|
||||
fn limit(&self) -> Option<u64> { None }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,15 +137,15 @@ pub fn serialize_into<W: ?Sized, T: ?Sized, S, E>(writer: &mut W, value: &T, siz
|
|||
pub fn serialize<T: ?Sized, S, E>(value: &T, size_limit: S) -> Result<Vec<u8>>
|
||||
where T: serde::Serialize, S: SizeLimit, E: ByteOrder
|
||||
{
|
||||
// Since we are putting values directly into a vector, we can do size
|
||||
// computation out here and pre-allocate a buffer of *exactly*
|
||||
// the right size.
|
||||
let mut writer = match size_limit.limit() {
|
||||
Some(size_limit) => {
|
||||
let actual_size = try!(serialized_size_bounded(value, size_limit).ok_or(ErrorKind::SizeLimit));
|
||||
Vec::with_capacity(actual_size as usize)
|
||||
}
|
||||
None => Vec::new()
|
||||
None => {
|
||||
let size = serialized_size(value) as usize;
|
||||
Vec::with_capacity(size)
|
||||
}
|
||||
};
|
||||
|
||||
try!(serialize_into::<_, _, _, E>(&mut writer, value, super::Infinite));
|
||||
|
|
|
|||
Loading…
Reference in New Issue