Skip to content

Commit

Permalink
Merge pull request #2458 from ljedrz/perf/plaintext_bits_allocs
Browse files Browse the repository at this point in the history
[Perf] Reduce allocations in ToBits for Plaintext
  • Loading branch information
howardwu authored May 23, 2024
2 parents 140ff26 + 5835a86 commit 757e5d5
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions circuit/program/src/data/plaintext/to_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
bits_le.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
literal.variant().write_bits_le(&mut bits_le);
literal.size_in_bits().write_bits_le(&mut bits_le);
literal.write_bits_le(&mut bits_le);
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the literal.
Expand All @@ -35,15 +37,17 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Struct(members, bits_le) => {
// Compute the bits of the struct.
let bits = bits_le.get_or_init(|| {
let mut bits_le = vec![Boolean::constant(false), Boolean::constant(true)]; // Variant bit.
let mut bits_le = Vec::new();
bits_le.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
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);
}
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the struct.
Expand All @@ -52,13 +56,15 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Array(elements, bits_le) => {
// Compute the bits of the array.
let bits = bits_le.get_or_init(|| {
let mut bits_le = vec![Boolean::constant(true), Boolean::constant(false)]; // Variant bit.
let mut bits_le = Vec::new();
bits_le.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
U32::constant(console::U32::new(elements.len() as u32)).write_bits_le(&mut bits_le);
for value in elements {
let value_bits = value.to_bits_le();
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le);
bits_le.extend(value_bits);
}
bits_le.shrink_to_fit();
bits_le
});
// Extend the vector with the bits of the array.
Expand All @@ -73,10 +79,12 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Literal(literal, bits_be) => {
// Compute the bits of the literal.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(false), Boolean::constant(false)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
literal.variant().write_bits_be(&mut bits_be);
literal.size_in_bits().write_bits_be(&mut bits_be);
literal.write_bits_be(&mut bits_be);
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the literal.
Expand All @@ -85,15 +93,17 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Struct(members, bits_be) => {
// Compute the bits of the struct.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(false), Boolean::constant(true)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
U8::constant(console::U8::new(members.len() as u8)).write_bits_be(&mut bits_be);
for (identifier, value) in members {
let value_bits = value.to_bits_be();
identifier.size_in_bits().write_bits_be(&mut bits_be);
identifier.write_bits_be(&mut bits_be);
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
bits_be.extend_from_slice(&value_bits);
bits_be.extend(value_bits);
}
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the struct.
Expand All @@ -102,13 +112,15 @@ impl<A: Aleo> ToBits for Plaintext<A> {
Self::Array(elements, bits_be) => {
// Compute the bits of the array.
let bits = bits_be.get_or_init(|| {
let mut bits_be = vec![Boolean::constant(true), Boolean::constant(false)]; // Variant bit.
let mut bits_be = Vec::new();
bits_be.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
U32::constant(console::U32::new(elements.len() as u32)).write_bits_be(&mut bits_be);
for value in elements {
let value_bits = value.to_bits_be();
U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
bits_be.extend(value_bits);
}
bits_be.shrink_to_fit();
bits_be
});
// Extend the vector with the bits of the array.
Expand Down

0 comments on commit 757e5d5

Please sign in to comment.