Skip to content

Commit

Permalink
Use Vec::with_capacity() when we know vec capacity (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
danlaine authored Dec 20, 2024
1 parent 8b239f1 commit ba27ef8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 3 additions & 1 deletion consensus/src/simplex/mocks/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand::{Rng, RngCore};
use rand_distr::{Distribution, Normal};
use std::{
collections::{HashMap, HashSet},
mem::size_of,
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -225,7 +226,8 @@ impl<E: Clock + RngCore, H: Hasher> Application<E, H> {
}

// Generate the payload
let mut payload = Vec::new();
let payload_len = size_of::<u64>() + context.parent.1.len() + size_of::<u64>();
let mut payload = Vec::with_capacity(payload_len);
payload.extend_from_slice(&context.view.to_be_bytes());
payload.extend_from_slice(&context.parent.1);
payload.put_u64(self.runtime.gen::<u64>()); // Ensures we always have a unique payload
Expand Down
7 changes: 4 additions & 3 deletions cryptography/src/bls12381/primitives/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::bls12381::primitives::{
Error,
};
use rand::{rngs::OsRng, RngCore};
use std::collections::BTreeMap;
use std::{collections::BTreeMap, mem::size_of};

/// Private polynomials are used to generate secret shares.
pub type Private = Poly<group::Private>;
Expand All @@ -33,9 +33,10 @@ pub struct Eval<C: Element> {
impl<C: Element> Eval<C> {
/// Canonically serializes the evaluation.
pub fn serialize(&self) -> Vec<u8> {
let mut bytes = Vec::new();
let value_serialized = self.value.serialize();
let mut bytes = Vec::with_capacity(size_of::<u32>() + value_serialized.len());
bytes.extend_from_slice(&self.index.to_be_bytes());
bytes.extend_from_slice(&self.value.serialize());
bytes.extend_from_slice(&value_serialized);
bytes
}

Expand Down
3 changes: 2 additions & 1 deletion examples/vrf/src/handlers/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use commonware_cryptography::bls12381::primitives::{group::Element, poly};
use commonware_utils::hex;
use std::mem::size_of;

pub const SHARE_NAMESPACE: &[u8] = b"_COMMONWARE_DKG_SHARE_";

Expand All @@ -8,7 +9,7 @@ pub const SHARE_NAMESPACE: &[u8] = b"_COMMONWARE_DKG_SHARE_";
/// This payload is used to verify that a particular dealer shared an
/// invalid secret during the DKG/Resharing procedure.
pub fn payload(round: u64, dealer: u32, share: &[u8]) -> Vec<u8> {
let mut payload = Vec::new();
let mut payload = Vec::with_capacity(size_of::<u64>() + size_of::<u32>() + share.len());
payload.extend_from_slice(&round.to_be_bytes());
payload.extend_from_slice(&dealer.to_be_bytes());
payload.extend_from_slice(share);
Expand Down
9 changes: 6 additions & 3 deletions p2p/src/authenticated/actors/tracker/address.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::Error;
use crate::authenticated::wire::Peer;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use std::{
mem::size_of,
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
};

#[derive(Clone)]
pub enum Address {
Expand All @@ -18,15 +21,15 @@ pub struct Signature {
}

pub fn wire_peer_payload(peer: &Peer) -> Vec<u8> {
let mut payload = Vec::new();
let mut payload = Vec::with_capacity(peer.socket.len() + size_of::<u64>());
payload.extend_from_slice(&peer.socket);
payload.extend_from_slice(&peer.timestamp.to_be_bytes());
payload
}

pub fn socket_peer_payload(socket: &SocketAddr, timestamp: u64) -> (Vec<u8>, Vec<u8>) {
let socket = bytes(socket);
let mut payload = Vec::new();
let mut payload = Vec::with_capacity(socket.len() + size_of::<u64>());
payload.extend_from_slice(&socket);
payload.extend_from_slice(&timestamp.to_be_bytes());
(socket, payload)
Expand Down
16 changes: 12 additions & 4 deletions stream/src/public_key/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use commonware_macros::select;
use commonware_runtime::{Clock, Sink, Spawner, Stream};
use commonware_utils::SystemTimeExt;
use prost::Message;
use std::time::{Duration, SystemTime};
use std::{
mem::size_of,
time::{Duration, SystemTime},
};

pub fn create_handshake<C: Scheme>(
crypto: &mut C,
Expand All @@ -16,9 +19,12 @@ pub fn create_handshake<C: Scheme>(
ephemeral_public_key: x25519_dalek::PublicKey,
) -> Result<Bytes, Error> {
// Sign their public key
let mut payload = Vec::new();
let ephemeral_public_key_bytes = ephemeral_public_key.as_bytes();
let payload_len =
recipient_public_key.len() + ephemeral_public_key_bytes.len() + size_of::<u64>();
let mut payload = Vec::with_capacity(payload_len);
payload.extend_from_slice(&recipient_public_key);
payload.extend_from_slice(ephemeral_public_key.as_bytes());
payload.extend_from_slice(ephemeral_public_key_bytes);
payload.extend_from_slice(&timestamp.to_be_bytes());
let signature = crypto.sign(Some(namespace), &payload);

Expand Down Expand Up @@ -94,7 +100,9 @@ impl Handshake {
}

// Construct signing payload (ephemeral public key + my public key + timestamp)
let mut payload = Vec::new();
let payload_len =
our_public_key.len() + handshake.ephemeral_public_key.len() + size_of::<u64>();
let mut payload = Vec::with_capacity(payload_len);
payload.extend_from_slice(&our_public_key);
payload.extend_from_slice(&handshake.ephemeral_public_key);
payload.extend_from_slice(&handshake.timestamp.to_be_bytes());
Expand Down

0 comments on commit ba27ef8

Please sign in to comment.