From 1d6ea73dbb2d0daf3cc4b1e4af095eba84a18386 Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 31 Oct 2024 16:19:55 +0800 Subject: [PATCH 1/2] test: use Vec::with_capacity avoid realloc mem --- crates/starknet-types-core/src/felt/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet-types-core/src/felt/mod.rs b/crates/starknet-types-core/src/felt/mod.rs index 068f156..99d1f74 100644 --- a/crates/starknet-types-core/src/felt/mod.rs +++ b/crates/starknet-types-core/src/felt/mod.rs @@ -1076,7 +1076,7 @@ mod test { // Helper function to generate a vector of bits for testing purposes fn generate_be_bits(x: &Felt) -> Vec { // Initialize an empty vector to store the expected bits - let mut bits = Vec::new(); + let mut bits = Vec::with_capacity(x.0.representative().limbs.len() * 8 * 8); // Iterate over each limb in the representative of x for limb in x.0.representative().limbs { @@ -1831,7 +1831,7 @@ mod test { bytes[31 - (i >> 1)] |= 15 << (4 * (i & 1)); } let h = Felt::from_bytes_be(&bytes); - let mut res = Vec::new(); + let mut res = Vec::with_capacity(enc_len(n_nibbles)); assert!(h.serialize(&mut res).is_ok()); assert_eq!(res.len(), enc_len(n_nibbles)); let mut reader = &res[..]; From 7254cbfd6a9da6e2d536933afd7a075d2740ccaa Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Sat, 22 Mar 2025 19:45:27 +0800 Subject: [PATCH 2/2] update review content --- crates/starknet-types-core/src/felt/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/starknet-types-core/src/felt/mod.rs b/crates/starknet-types-core/src/felt/mod.rs index 99d1f74..3e691ca 100644 --- a/crates/starknet-types-core/src/felt/mod.rs +++ b/crates/starknet-types-core/src/felt/mod.rs @@ -1076,10 +1076,11 @@ mod test { // Helper function to generate a vector of bits for testing purposes fn generate_be_bits(x: &Felt) -> Vec { // Initialize an empty vector to store the expected bits - let mut bits = Vec::with_capacity(x.0.representative().limbs.len() * 8 * 8); + let representative = x.0.representative(); + let mut bits = Vec::with_capacity(limbs.len() * 8 * 8); // Iterate over each limb in the representative of x - for limb in x.0.representative().limbs { + for limb in bits.limbs { // Convert the limb to a sequence of 8 bytes (u8) in big-endian let bytes = limb.to_be_bytes(); @@ -1831,7 +1832,7 @@ mod test { bytes[31 - (i >> 1)] |= 15 << (4 * (i & 1)); } let h = Felt::from_bytes_be(&bytes); - let mut res = Vec::with_capacity(enc_len(n_nibbles)); + let mut res = Vec::new(); assert!(h.serialize(&mut res).is_ok()); assert_eq!(res.len(), enc_len(n_nibbles)); let mut reader = &res[..];