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

Add size hints in order to optimize allocation during deserialization #570

Merged
merged 4 commits into from
Aug 27, 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
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
Loading