Skip to content

Commit

Permalink
perf: use dedicated Iterator methods instead of Vec pushing
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <ljedrz@gmail.com>
  • Loading branch information
ljedrz committed Oct 1, 2020
1 parent 24b8bb5 commit c358282
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 27 deletions.
7 changes: 1 addition & 6 deletions algorithms/src/encryption/tests.rs
Expand Up @@ -30,12 +30,7 @@ type TestEncryptionScheme = GroupEncryption<EdwardsProjective>;
pub const ITERATIONS: usize = 1000;

fn generate_input<G: Group + ProjectiveCurve, R: Rng>(input_size: usize, rng: &mut R) -> Vec<G> {
let mut input = vec![];
for _ in 0..input_size {
input.push(G::rand(rng))
}

input
(0..input_size).map(|_| G::rand(rng)).collect()
}

#[test]
Expand Down
5 changes: 1 addition & 4 deletions algorithms/src/fft/polynomial/dense.rs
Expand Up @@ -140,10 +140,7 @@ impl<F: Field> DensePolynomial<F> {
/// Outputs a polynomial of degree `d` where each coefficient is sampled uniformly at random
/// from the field `F`.
pub fn rand<R: Rng>(d: usize, rng: &mut R) -> Self {
let mut random_coeffs = Vec::new();
for _ in 0..(d + 1) {
random_coeffs.push(F::rand(rng));
}
let random_coeffs = (0..(d + 1)).map(|_| F::rand(rng)).collect();
Self::from_coefficients_vec(random_coeffs)
}
}
Expand Down
6 changes: 1 addition & 5 deletions models/src/gadgets/utilities/uint/unsigned_integer.rs
Expand Up @@ -100,11 +100,7 @@ pub trait UInt: Debug + Clone + PartialOrd + Eq + PartialEq {
impl UInt8 {
/// Construct a constant vector of `UInt8` from a vector of `u8`
pub fn constant_vec(values: &[u8]) -> Vec<Self> {
let mut result = Vec::new();
for value in values {
result.push(UInt8::constant(*value));
}
result
values.iter().copied().map(UInt8::constant).collect()
}

pub fn alloc_vec<F, CS, T>(mut cs: CS, values: &[T]) -> Result<Vec<Self>, SynthesisError>
Expand Down
6 changes: 1 addition & 5 deletions rpc/src/rpc_impl.rs
Expand Up @@ -305,11 +305,7 @@ impl RpcFunctions for RpcImpl {
// Create a temporary tokio runtime to make an asynchronous function call
let peer_book = Runtime::new()?.block_on(self.server_context.peer_book.read());

let mut peers = vec![];

for peer in peer_book.get_connected().keys() {
peers.push(*peer);
}
let peers = peer_book.get_connected().keys().cloned().collect();

Ok(PeerInfo { peers })
}
Expand Down
12 changes: 5 additions & 7 deletions utilities/derives/src/lib.rs
Expand Up @@ -138,13 +138,11 @@ fn impl_deserialize_field(ty: &Type) -> (TokenStream, TokenStream) {
// Check if type is a tuple.
match ty {
Type::Tuple(tuple) => {
let mut compressed_fields = Vec::new();
let mut uncompressed_fields = Vec::new();
for elem_ty in tuple.elems.iter() {
let (compressed, uncompressed) = impl_deserialize_field(elem_ty);
compressed_fields.push(compressed);
uncompressed_fields.push(uncompressed);
}
let (compressed_fields, uncompressed_fields): (Vec<_>, Vec<_>) = tuple
.elems
.iter()
.map(|elem_ty| impl_deserialize_field(elem_ty))
.unzip();
(
quote! { (#(#compressed_fields)*), },
quote! { (#(#uncompressed_fields)*), },
Expand Down

0 comments on commit c358282

Please sign in to comment.