From 241401e937954a2a7af0175f61224b7bc41814bf Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:35:39 +0100 Subject: [PATCH] opti: use `vec::with_capacity` when possible (#1535) Co-authored-by: anondev1993 --- CHANGELOG.md | 1 + crates/primitives/snos-output/src/codec.rs | 2 +- starknet-e2e-test/src/starknet_sovereign.rs | 3 ++- starknet-rpc-test/src/lib.rs | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c16f6027..d78174cf1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- dev: change `Vec::new` to `Vec::with_capacity` where possible. - chore(rpc): clean trace api - feat(rpc): added state diff real value in trace api - chore: update cairo-vm commit and update gas per op diff --git a/crates/primitives/snos-output/src/codec.rs b/crates/primitives/snos-output/src/codec.rs index adc0b923ed..13229ccf9f 100644 --- a/crates/primitives/snos-output/src/codec.rs +++ b/crates/primitives/snos-output/src/codec.rs @@ -117,7 +117,7 @@ impl SnosCodec for Vec { fn decode(input: &mut FeltReader) -> Result { let mut segment_reader = FeltReader::new(input.read_segment()?); - let mut elements: Vec = Vec::new(); + let mut elements: Vec = Vec::with_capacity(segment_reader.remaining_len()); while segment_reader.remaining_len() > 0 { elements.push(T::decode(&mut segment_reader)?); } diff --git a/starknet-e2e-test/src/starknet_sovereign.rs b/starknet-e2e-test/src/starknet_sovereign.rs index 6b868ac5b1..78bca30710 100644 --- a/starknet-e2e-test/src/starknet_sovereign.rs +++ b/starknet-e2e-test/src/starknet_sovereign.rs @@ -160,7 +160,8 @@ impl StarknetSovereign { } pub async fn message_to_l1_exists(&self, message: &MessageL2ToL1) -> bool { - let mut payload: Vec = Vec::new(); + let message_felt_size = message.size_in_felts(); + let mut payload: Vec = Vec::with_capacity(32 * message_felt_size); message.clone().into_encoded_vec().into_iter().for_each(|felt| payload.append(&mut felt.bytes().to_vec())); let msg_hash = keccak256(payload); diff --git a/starknet-rpc-test/src/lib.rs b/starknet-rpc-test/src/lib.rs index 450edee0f8..c369725b58 100644 --- a/starknet-rpc-test/src/lib.rs +++ b/starknet-rpc-test/src/lib.rs @@ -170,7 +170,7 @@ impl MadaraClient { &mut self, transactions: Vec>, ) -> anyhow::Result>> { - let mut results = Vec::new(); + let mut results = Vec::with_capacity(transactions.len()); for tx in transactions { let result = tx.send().await; results.push(result); @@ -184,7 +184,7 @@ impl MadaraClient { &mut self, transactions: Vec>, ) -> Vec> { - let mut results = Vec::new(); + let mut results = Vec::with_capacity(transactions.len()); for tx in transactions { let result = tx.send().await; results.push(result);