Skip to content

Commit 925fa81

Browse files
committed
Sync the size_hint changes from serde
Upstream serde has a new implementation of the cautious size hint function, which takes into consideration the size of the elements. Use the same implementation in serde_with. serde-rs/serde#2495
1 parent b81abda commit 925fa81

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

serde_with/src/content/de.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<'de> Visitor<'de> for ContentVisitor<'de> {
283283
where
284284
V: SeqAccess<'de>,
285285
{
286-
let mut vec = Vec::with_capacity(size_hint_cautious(visitor.size_hint()));
286+
let mut vec = Vec::with_capacity(size_hint_cautious::<Content<'_>>(visitor.size_hint()));
287287
while let Some(e) = visitor.next_element()? {
288288
vec.push(e);
289289
}
@@ -294,7 +294,9 @@ impl<'de> Visitor<'de> for ContentVisitor<'de> {
294294
where
295295
V: MapAccess<'de>,
296296
{
297-
let mut vec = Vec::with_capacity(size_hint_cautious(visitor.size_hint()));
297+
let mut vec = Vec::with_capacity(size_hint_cautious::<(Content<'_>, Content<'_>)>(
298+
visitor.size_hint(),
299+
));
298300
while let Some(kv) = visitor.next_entry()? {
299301
vec.push(kv);
300302
}

serde_with/src/de/impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ macro_rules! seq_impl {
400400
A: SeqAccess<'de>,
401401
{
402402
#[allow(clippy::redundant_closure_call)]
403-
let mut values = ($with_capacity)(utils::size_hint_cautious(seq.size_hint()));
403+
let mut values = ($with_capacity)(utils::size_hint_cautious::<T>(seq.size_hint()));
404404

405405
while let Some(value) = seq
406406
.next_element()?
@@ -465,7 +465,7 @@ macro_rules! map_impl {
465465
A: MapAccess<'de>,
466466
{
467467
#[allow(clippy::redundant_closure_call)]
468-
let mut values = ($with_capacity)(utils::size_hint_cautious(map.size_hint()));
468+
let mut values = ($with_capacity)(utils::size_hint_cautious::<(K, V)>(map.size_hint()));
469469

470470
while let Some((key, value)) = (map.next_entry())?.map(|(k, v): (DeserializeAsWrap::<K, KU>, DeserializeAsWrap::<V, VU>)| (k.into_inner(), v.into_inner())) {
471471
values.insert(key, value);

serde_with/src/utils.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ use crate::prelude::*;
55
/// Re-Implementation of `serde::private::de::size_hint::cautious`
66
#[cfg(feature = "alloc")]
77
#[inline]
8-
pub(crate) fn size_hint_cautious(hint: Option<usize>) -> usize {
9-
core::cmp::min(hint.unwrap_or(0), 4096)
8+
pub(crate) fn size_hint_cautious<Element>(hint: Option<usize>) -> usize {
9+
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
10+
11+
if core::mem::size_of::<Element>() == 0 {
12+
0
13+
} else {
14+
core::cmp::min(
15+
hint.unwrap_or(0),
16+
MAX_PREALLOC_BYTES / core::mem::size_of::<Element>(),
17+
)
18+
}
1019
}
1120

1221
/// Re-Implementation of `serde::private::de::size_hint::from_bounds`

0 commit comments

Comments
 (0)