-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[Perf] Reduce allocations in ToBits for Plaintext #2458
[Perf] Reduce allocations in ToBits for Plaintext #2458
Conversation
Signed-off-by: ljedrz <ljedrz@gmail.com>
U8::constant(console::U8::new(members.len() as u8)).write_bits_le(&mut bits_le); | ||
for (identifier, value) in members { | ||
let value_bits = value.to_bits_le(); | ||
identifier.size_in_bits().write_bits_le(&mut bits_le); | ||
identifier.write_bits_le(&mut bits_le); | ||
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le); | ||
bits_le.extend_from_slice(&value_bits); | ||
bits_le.extend(value_bits); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Vec::extend
is preferable here, as it avoids cloning the members of of value_bits
.
@@ -23,10 +23,12 @@ impl<A: Aleo> ToBits for Plaintext<A> { | |||
Self::Literal(literal, bits_le) => { | |||
// Compute the bits of the literal. | |||
let bits = bits_le.get_or_init(|| { | |||
let mut bits_le = vec![Boolean::constant(false), Boolean::constant(false)]; // Variant bit. | |||
let mut bits_le = Vec::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this is a conservative tweak which ensures that the vector starts out with a capacity larger than 2 elements
We need CI here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These look like safe wins, but I'd agree that it needs CI before merge.
Calls to
Plaintext::write_bits_le
are responsible for a large number of allocations when performing deployments and executions; using a few simple tweaks we can reduce that number by as much as ~18% in some scenarios (my run was performed on a large deployment and execution).Also, since we are dealing with
OnceCell
here, the additional calls toVec::shrink_to_fit
should be perfectly acceptable and slightly reduce heap use.