From 792a5f7502b7dc6d2546fbd860ea2a7c74b46108 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 19 Feb 2017 13:45:23 -0800 Subject: [PATCH] Also clamp the collection impls --- serde/src/de/impls.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index fa3cd214..c50574de 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -30,6 +30,8 @@ use std::collections::{ #[cfg(feature = "collections")] use collections::borrow::ToOwned; +#[cfg(any(feature = "std", feature = "collections"))] +use core::cmp; use core::fmt; #[cfg(feature = "std")] use core::hash::{Hash, BuildHasher}; @@ -467,7 +469,7 @@ seq_impl!( BinaryHeapVisitor, visitor, BinaryHeap::new(), - BinaryHeap::with_capacity(visitor.size_hint().0), + BinaryHeap::with_capacity(cmp::min(visitor.size_hint().0, 4096)), BinaryHeap::push); #[cfg(any(feature = "std", feature = "collections"))] @@ -495,7 +497,7 @@ seq_impl!( S: BuildHasher + Default>, visitor, HashSet::with_hasher(S::default()), - HashSet::with_capacity_and_hasher(visitor.size_hint().0, S::default()), + HashSet::with_capacity_and_hasher(cmp::min(visitor.size_hint().0, 4096), S::default()), HashSet::insert); #[cfg(any(feature = "std", feature = "collections"))] @@ -504,7 +506,7 @@ seq_impl!( VecVisitor, visitor, Vec::new(), - Vec::with_capacity(visitor.size_hint().0), + Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)), Vec::push); #[cfg(any(feature = "std", feature = "collections"))] @@ -513,7 +515,7 @@ seq_impl!( VecDequeVisitor, visitor, VecDeque::new(), - VecDeque::with_capacity(visitor.size_hint().0), + VecDeque::with_capacity(cmp::min(visitor.size_hint().0, 4096)), VecDeque::push_back); /////////////////////////////////////////////////////////////////////////////// @@ -791,7 +793,7 @@ map_impl!( S: BuildHasher + Default>, visitor, HashMap::with_hasher(S::default()), - HashMap::with_capacity_and_hasher(visitor.size_hint().0, S::default())); + HashMap::with_capacity_and_hasher(cmp::min(visitor.size_hint().0, 4096), S::default())); ///////////////////////////////////////////////////////////////////////////////