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

[Perf] Reduce allocations in ToBits for Plaintext #2458

Merged
merged 1 commit into from
May 23, 2024

Conversation

ljedrz
Copy link
Collaborator

@ljedrz ljedrz commented May 20, 2024

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 to Vec::shrink_to_fit should be perfectly acceptable and slightly reduce heap use.

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);
Copy link
Collaborator Author

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();
Copy link
Collaborator Author

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

@howardwu
Copy link
Contributor

We need CI here

Copy link
Contributor

@iamalwaysuncomfortable iamalwaysuncomfortable left a 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.

@howardwu howardwu merged commit 757e5d5 into AleoNet:mainnet-staging May 23, 2024
zosorock added a commit that referenced this pull request May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants