Skip to content

Commit

Permalink
Add size hints in order to optimize allocation during deserialization (
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdavid authored Aug 27, 2024
1 parent cfa7b54 commit 0ed73c1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rust/candid/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,14 @@ impl<'de, 'a> de::SeqAccess<'de> for Compound<'a, 'de> {
_ => Err(Error::subtype("expect vector or tuple")),
}
}

fn size_hint(&self) -> Option<usize> {
match &self.style {
Style::Vector { len, .. } => Some(*len),
Style::Struct { expect, wire, .. } => Some(expect.len().min(wire.len())),
_ => None,
}
}
}

impl<'de, 'a> de::MapAccess<'de> for Compound<'a, 'de> {
Expand Down Expand Up @@ -1221,6 +1229,14 @@ impl<'de, 'a> de::MapAccess<'de> for Compound<'a, 'de> {
}
}
}

fn size_hint(&self) -> Option<usize> {
match &self.style {
Style::Map { len, .. } => Some(*len),
Style::Struct { expect, wire, .. } => Some(expect.len().min(wire.len())),
_ => None,
}
}
}

impl<'de, 'a> de::EnumAccess<'de> for Compound<'a, 'de> {
Expand Down

0 comments on commit 0ed73c1

Please sign in to comment.