Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use Vec::with_capacity avoid realloc mem #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ mod test {
// Helper function to generate a vector of bits for testing purposes
fn generate_be_bits(x: &Felt) -> Vec<bool> {
// 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to representative is not free at all.
Now there are two of those.

If you want to do this you should

let representative = x.0.representative();

let mut bits = Vec::with_capacity(limbs.len() * 8 * 8);

for limb in representative.limbs {


// Iterate over each limb in the representative of x
for limb in x.0.representative().limbs {
Expand Down Expand Up @@ -1836,7 +1836,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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test. No need to to optimize it. Especially when guessing the len require computation

assert!(h.serialize(&mut res).is_ok());
assert_eq!(res.len(), enc_len(n_nibbles));
let mut reader = &res[..];
Expand Down