Skip to content

Commit 052cbd9

Browse files
authored
Merge pull request #2175 from ljedrz/perf/fewer_verify_allocs
Fewer prove/verify allocations
2 parents 6e14400 + ad69aea commit 052cbd9

File tree

7 files changed

+21
-8
lines changed

7 files changed

+21
-8
lines changed

algorithms/src/crypto_hash/poseidon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
464464
};
465465
let bits = self.get_bits(num_bits_per_nonnative * num_elements);
466466

467-
let mut lookup_table = Vec::<TargetField>::new();
467+
let mut lookup_table = Vec::<TargetField>::with_capacity(num_bits_per_nonnative);
468468
let mut cur = TargetField::one();
469469
for _ in 0..num_bits_per_nonnative {
470470
lookup_table.push(cur);

algorithms/src/fft/polynomial/multiplier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
102102
}
103103
let fft_pc = &self.fft_precomputation.unwrap();
104104
let ifft_pc = &self.ifft_precomputation.unwrap();
105-
let mut pool = ExecutionPool::new();
105+
let mut pool = ExecutionPool::with_capacity(self.polynomials.len() + self.evaluations.len());
106106
for (_, p) in self.polynomials {
107107
pool.add_job(move || {
108108
let mut p = p.clone().into_owned().coeffs;
@@ -146,7 +146,7 @@ impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
146146
Some(Cow::Owned(self.fft_precomputation.as_ref().unwrap().to_ifft_precomputation()));
147147
}
148148
let fft_pc = self.fft_precomputation.as_ref().unwrap();
149-
let mut pool = ExecutionPool::new();
149+
let mut pool = ExecutionPool::with_capacity(self.polynomials.len() + self.evaluations.len());
150150
for (l, p) in self.polynomials {
151151
pool.add_job(move || {
152152
let mut p = p.clone().into_owned().coeffs;

algorithms/src/msm/variable_base/batched.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub(super) fn batch_add<G: AffineCurve>(
192192
let mut number_of_bases_in_batch = 0;
193193

194194
let mut instr = Vec::<(u32, u32)>::with_capacity(batch_size);
195-
let mut new_bases = Vec::with_capacity(bases.len() * 3 / 8);
195+
let mut new_bases = Vec::with_capacity(bases.len());
196196
let mut scratch_space = Vec::with_capacity(batch_size / 2);
197197

198198
// In the first loop, copy the results of the first in-place addition tree to the vector `new_bases`.

algorithms/src/snark/varuna/ahp/ahp.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use snarkvm_fields::{Field, PrimeField};
3232

3333
use core::{borrow::Borrow, marker::PhantomData};
3434
use itertools::Itertools;
35-
use std::collections::BTreeMap;
35+
use std::{collections::BTreeMap, fmt::Write};
3636

3737
/// The algebraic holographic proof defined in [CHMMVW19](https://eprint.iacr.org/2019/1047).
3838
/// Currently, this AHP only supports inputs of size one
@@ -43,7 +43,9 @@ pub struct AHPForR1CS<F: Field, SM: SNARKMode> {
4343
}
4444

4545
pub(crate) fn witness_label(circuit_id: CircuitId, poly: &str, i: usize) -> String {
46-
format!("circuit_{circuit_id}_{poly}_{i:0>8}")
46+
let mut label = String::with_capacity(82 + poly.len());
47+
let _ = write!(&mut label, "circuit_{circuit_id}_{poly}_{i:0>8}");
48+
label
4749
}
4850

4951
pub(crate) struct NonZeroDomains<F: PrimeField> {

curves/src/templates/bls12/g2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<P: Bls12Parameters> G2Prepared<P> {
9292
let mut r = G2HomProjective { x: q.x, y: q.y, z: Fp2::one() };
9393

9494
let bit_iterator = BitIteratorBE::new(P::X);
95-
let mut ell_coeffs = Vec::with_capacity(bit_iterator.len());
95+
let mut ell_coeffs = Vec::with_capacity(bit_iterator.len() * 3 / 2);
9696

9797
// `one_half` = 1/2 in the field.
9898
let one_half = P::Fp::half();

fields/src/traits/poseidon_grain_lfsr.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ impl PoseidonGrainLFSR {
124124
let mut output = Vec::with_capacity(num_elems);
125125
for _ in 0..num_elems {
126126
// Obtain `n` bits and make it most-significant-bit first.
127-
let mut bits = self.get_bits(self.field_size_in_bits as usize).collect::<Vec<_>>();
127+
let bits_iter = self.get_bits(self.field_size_in_bits as usize);
128+
let mut bits = Vec::with_capacity(bits_iter.len());
129+
for bit in bits_iter {
130+
bits.push(bit);
131+
}
128132
bits.reverse();
129133

130134
let bytes = bits
@@ -199,3 +203,9 @@ impl<'a> Iterator for LFSRIter<'a> {
199203
}
200204
}
201205
}
206+
207+
impl<'a> ExactSizeIterator for LFSRIter<'a> {
208+
fn len(&self) -> usize {
209+
self.num_bits
210+
}
211+
}

utilities/src/serialize/impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Vec<T> {
424424
) -> Result<Self, SerializationError> {
425425
let len = u64::deserialize_with_mode(&mut reader, compress, validate)?;
426426
let mut values = Vec::new();
427+
let _ = values.try_reserve(len as usize);
427428
for _ in 0..len {
428429
values.push(T::deserialize_with_mode(&mut reader, compress, Validate::No)?);
429430
}

0 commit comments

Comments
 (0)