Skip to content

Commit

Permalink
Merge pull request #2174 from ljedrz/perf/better_ToBits_allocs
Browse files Browse the repository at this point in the history
Reserve the exact capacity in more ToBits impls
  • Loading branch information
howardwu authored Nov 24, 2023
2 parents cd0f6d4 + 1e90fc3 commit 00c02e0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions fields/src/fp_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,10 @@ impl<P: Fp256Parameters> ToBits for Fp256<P> {
self.write_bits_le(vec);
vec[initial_len..].reverse();
}

fn num_bits() -> Option<usize> {
Some(256)
}
}

impl<P: Fp256Parameters> ToBytes for Fp256<P> {
Expand Down
4 changes: 4 additions & 0 deletions fields/src/fp_384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ impl<P: Fp384Parameters> ToBits for Fp384<P> {
self.write_bits_le(vec);
vec[initial_len..].reverse();
}

fn num_bits() -> Option<usize> {
Some(384)
}
}

impl<P: Fp384Parameters> ToBytes for Fp384<P> {
Expand Down
22 changes: 19 additions & 3 deletions utilities/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ pub trait ToBits: Sized {

/// Returns `self` as a boolean array in little-endian order.
fn to_bits_le(&self) -> Vec<bool> {
let mut bits = Vec::with_capacity(32);
let mut bits = Vec::new();
self.write_bits_le(&mut bits);
bits
}

/// Returns `self` as a boolean array in big-endian order.
fn to_bits_be(&self) -> Vec<bool> {
let mut bits = Vec::with_capacity(32);
let mut bits = Vec::new();
self.write_bits_be(&mut bits);
bits
}

/// An optional indication of how many bits an object can be represented with.
fn num_bits() -> Option<usize> {
None
}
}

pub trait FromBits: Sized {
Expand Down Expand Up @@ -144,7 +149,6 @@ macro_rules! impl_bits_for_integer {
/// Returns `self` as a boolean array in little-endian order.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
vec.reserve(<$int>::BITS as usize);
let mut value = *self;
for _ in 0..<$int>::BITS {
vec.push(value & 1 == 1);
Expand All @@ -158,6 +162,10 @@ macro_rules! impl_bits_for_integer {
let reversed = self.reverse_bits();
reversed.write_bits_le(vec);
}

fn num_bits() -> Option<usize> {
Some(<$int>::BITS as usize)
}
}

impl FromBits for $int {
Expand Down Expand Up @@ -266,6 +274,10 @@ impl<C: ToBits> ToBits for &[C] {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
if let Some(num_bits) = C::num_bits() {
vec.reserve(num_bits * self.len());
}

for elem in self.iter() {
elem.write_bits_le(vec);
}
Expand All @@ -274,6 +286,10 @@ impl<C: ToBits> ToBits for &[C] {
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
if let Some(num_bits) = C::num_bits() {
vec.reserve(num_bits * self.len());
}

for elem in self.iter() {
elem.write_bits_be(vec);
}
Expand Down

0 comments on commit 00c02e0

Please sign in to comment.