Skip to content

Commit

Permalink
Merge pull request #317 from cuviper/serde-size_hint
Browse files Browse the repository at this point in the history
Limit preallocation in deserializers
  • Loading branch information
cuviper committed Feb 27, 2024
2 parents 9deec7c + fac3148 commit 43b322e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/map/serde_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use core::hash::{BuildHasher, Hash};
use core::marker::PhantomData;

use crate::map::Slice as MapSlice;
use crate::serde::cautious_capacity;
use crate::set::Slice as SetSlice;
use crate::IndexMap;

Expand Down Expand Up @@ -101,7 +102,7 @@ where
where
A: SeqAccess<'de>,
{
let capacity = seq.size_hint().unwrap_or(0);
let capacity = cautious_capacity::<K, V>(seq.size_hint());
let mut map = IndexMap::with_capacity_and_hasher(capacity, S::default());

while let Some((key, value)) = seq.next_element()? {
Expand Down
35 changes: 27 additions & 8 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@ use serde::ser::{Serialize, Serializer};
use core::fmt::{self, Formatter};
use core::hash::{BuildHasher, Hash};
use core::marker::PhantomData;

use crate::IndexMap;
use core::{cmp, mem};

use crate::{Bucket, IndexMap, IndexSet};

/// Limit our preallocated capacity from a deserializer `size_hint()`.
///
/// We do account for the `Bucket` overhead from its saved `hash` field, but we don't count the
/// `RawTable` allocation or the fact that its raw capacity will be rounded up to a power of two.
/// The "max" is an arbitrary choice anyway, not something that needs precise adherence.
///
/// This is based on the internal `serde::de::size_hint::cautious(hint)` function.
pub(crate) fn cautious_capacity<K, V>(hint: Option<usize>) -> usize {
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;

if mem::size_of::<Bucket<K, V>>() == 0 {
0
} else {
cmp::min(
hint.unwrap_or(0),
MAX_PREALLOC_BYTES / mem::size_of::<Bucket<K, V>>(),
)
}
}

impl<K, V, S> Serialize for IndexMap<K, V, S>
where
Expand Down Expand Up @@ -43,8 +64,8 @@ where
where
A: MapAccess<'de>,
{
let mut values =
IndexMap::with_capacity_and_hasher(map.size_hint().unwrap_or(0), S::default());
let capacity = cautious_capacity::<K, V>(map.size_hint());
let mut values = IndexMap::with_capacity_and_hasher(capacity, S::default());

while let Some((key, value)) = map.next_entry()? {
values.insert(key, value);
Expand Down Expand Up @@ -82,8 +103,6 @@ where
}
}

use crate::IndexSet;

impl<T, S> Serialize for IndexSet<T, S>
where
T: Serialize,
Expand Down Expand Up @@ -113,8 +132,8 @@ where
where
A: SeqAccess<'de>,
{
let mut values =
IndexSet::with_capacity_and_hasher(seq.size_hint().unwrap_or(0), S::default());
let capacity = cautious_capacity::<T, ()>(seq.size_hint());
let mut values = IndexSet::with_capacity_and_hasher(capacity, S::default());

while let Some(value) = seq.next_element()? {
values.insert(value);
Expand Down

0 comments on commit 43b322e

Please sign in to comment.