Skip to content

Commit

Permalink
Construct skipped fields only after reading all non-skipped fields
Browse files Browse the repository at this point in the history
This should increase performance if construction of default object is expensive
  • Loading branch information
Mingun committed Aug 6, 2023
1 parent f38da85 commit 53535d5
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,19 +771,26 @@ fn read_fields_in_order(
let expecting = cattrs.expecting().unwrap_or(&expecting);

let mut index_in_seq = 0_usize;
let let_values = vars.clone().zip(fields).map(|(var, field)| {
if field.attrs.skip_deserializing() {
let default = Expr(expr_is_missing(field, cattrs));
quote! {
let #var = #default;
}
} else {
let let_values = vars.clone().zip(fields).filter_map(|(var, field)| {
if !field.attrs.skip_deserializing() {
let read = read_field(params, index_in_seq, field, cattrs, expecting);
index_in_seq += 1;
quote! {

return Some(quote! {
let #var = #read;
}
});
}
None
});
let let_skipped = vars.clone().zip(fields).filter_map(|(var, field)| {
if field.attrs.skip_deserializing() {
let default = Expr(expr_is_missing(field, cattrs));

return Some(quote! {
let #var = #default;
});
}
None
});

let mut result = if is_struct {
Expand Down Expand Up @@ -822,6 +829,7 @@ fn read_fields_in_order(
quote_block! {
#let_default
#(#let_values)*
#(#let_skipped)*
_serde::__private::Ok(#result)
}
}
Expand Down Expand Up @@ -883,15 +891,9 @@ fn read_fields_in_order_in_place(
let expecting = cattrs.expecting().unwrap_or(&expecting);

let mut index_in_seq = 0usize;
let write_values = fields.iter().map(|field| {
let member = &field.member;

if field.attrs.skip_deserializing() {
let default = Expr(expr_is_missing(field, cattrs));
quote! {
self.place.#member = #default;
}
} else {
let write_values = fields.iter().filter_map(|field| {
if !field.attrs.skip_deserializing() {
let member = &field.member;
let value_if_none = expr_is_missing_seq(Some(quote!(self.place.#member = )), index_in_seq, field, cattrs, expecting);
let write = match field.attrs.deserialize_with() {
None => {
Expand Down Expand Up @@ -919,8 +921,20 @@ fn read_fields_in_order_in_place(
}
};
index_in_seq += 1;
write
return Some(write);
}
None
});
let write_skipped = fields.iter().filter_map(|field| {
if field.attrs.skip_deserializing() {
let member = &field.member;
let default = Expr(expr_is_missing(field, cattrs));

return Some(quote! {
self.place.#member = #default;
});
}
None
});

let this_type = &params.this_type;
Expand All @@ -942,6 +956,7 @@ fn read_fields_in_order_in_place(
quote_block! {
#let_default
#(#write_values)*
#(#write_skipped)*
_serde::__private::Ok(())
}
}
Expand Down

0 comments on commit 53535d5

Please sign in to comment.