Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove explicit ContentDeserializer construction in visit functions #2822

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ mod content {
}
}

impl<'a, 'de, E> de::IntoDeserializer<'de, E> for &'a Content<'de>
where
E: de::Error,
{
type Deserializer = ContentRefDeserializer<'a, 'de, E>;

fn into_deserializer(self) -> Self::Deserializer {
ContentRefDeserializer::new(self)
}
}

/// Used to capture data in [`Content`] from other deserializers.
/// Cannot capture externally tagged enums, `i128` and `u128`.
struct ContentVisitor<'de> {
Expand Down Expand Up @@ -1100,8 +1111,7 @@ mod content {
V: Visitor<'de>,
E: de::Error,
{
let seq = content.into_iter().map(ContentDeserializer::new);
let mut seq_visitor = SeqDeserializer::new(seq);
let mut seq_visitor = SeqDeserializer::new(content.into_iter());
let value = tri!(visitor.visit_seq(&mut seq_visitor));
tri!(seq_visitor.end());
Ok(value)
Expand All @@ -1115,14 +1125,7 @@ mod content {
V: Visitor<'de>,
E: de::Error,
{
fn content_deserializer_pair<'de, E>(
(k, v): (Content<'de>, Content<'de>),
) -> (ContentDeserializer<'de, E>, ContentDeserializer<'de, E>) {
(ContentDeserializer::new(k), ContentDeserializer::new(v))
}

let map = content.into_iter().map(content_deserializer_pair);
let mut map_visitor = MapDeserializer::new(map);
let mut map_visitor = MapDeserializer::new(content.into_iter());
let value = tri!(visitor.visit_map(&mut map_visitor));
tri!(map_visitor.end());
Ok(value)
Expand Down Expand Up @@ -1700,8 +1703,7 @@ mod content {
V: Visitor<'de>,
E: de::Error,
{
let seq = content.iter().map(ContentRefDeserializer::new);
let mut seq_visitor = SeqDeserializer::new(seq);
let mut seq_visitor = SeqDeserializer::new(content.iter());
let value = tri!(visitor.visit_seq(&mut seq_visitor));
tri!(seq_visitor.end());
Ok(value)
Expand All @@ -1715,16 +1717,10 @@ mod content {
V: Visitor<'de>,
E: de::Error,
{
fn content_ref_deserializer_pair<'a, 'de, E>(
fn content_ref_deserializer_pair<'a, 'de>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried adding:

impl<'a, A, B> Pair for &'a (A, B) {
    type First = &'a A;
    type Second = &'a B;
    fn split(self) -> (&'a A, &'a B) {
        (&self.0, &self.1)
    }
}

to make this function go away, like in the visit_content_map case. See #2823.

But somehow that made the rlib go back up from 111K to 124K, negating the benefit from #2821. I do not fully understand why this happens.

(k, v): &'a (Content<'de>, Content<'de>),
) -> (
ContentRefDeserializer<'a, 'de, E>,
ContentRefDeserializer<'a, 'de, E>,
) {
(
ContentRefDeserializer::new(k),
ContentRefDeserializer::new(v),
)
) -> (&'a Content<'de>, &'a Content<'de>) {
(k, v)
}

let map = content.iter().map(content_ref_deserializer_pair);
Expand Down