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

Conversation

nkysg
Copy link

@nkysg nkysg commented Oct 31, 2024

Pull Request type

test: use Vec::with_capacity avoid realloc mem

Please add the labels corresponding to the type of changes your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build-related changes
  • Documentation content changes
  • Testing
  • Other (please describe):

What is the current behavior?

Resolves: #NA

What is the new behavior?

Does this introduce a breaking change?

Other information

@@ -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 {

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants