Skip to content

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

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 {

Copy link
Author

Choose a reason for hiding this comment

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

Done. Thank you.

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 not what you did.

@nkysg nkysg force-pushed the test_use_Vec_with_capacity branch from af7a414 to a7774fb Compare March 22, 2025 11:40
@nkysg nkysg force-pushed the test_use_Vec_with_capacity branch from a7774fb to 7254cbf Compare March 22, 2025 11:45
@nkysg nkysg requested a review from tdelabro March 22, 2025 11:46
@tdelabro
Copy link
Collaborator

@nkysg run the tests locally before you push


// Iterate over each limb in the representative of x
for limb in x.0.representative().limbs {
for limb in bits.limbs {
Copy link
Collaborator

Choose a reason for hiding this comment

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

won't compile

@tdelabro
Copy link
Collaborator

inactive

@tdelabro tdelabro closed this Jun 25, 2025
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