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 2, 2023
1 parent e383366 commit 360b946
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, 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 @@ -890,15 +898,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 = match field.attrs.default() {
attr::Default::Default => quote!(
self.place.#member = _serde::__private::Default::default();
Expand Down Expand Up @@ -936,8 +938,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 @@ -959,6 +973,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 360b946

Please sign in to comment.