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

chore: remove temporary allocations from num_bits #6600

Merged
merged 7 commits into from
Nov 25, 2024

Conversation

TomAFrench
Copy link
Member

Description

Problem*

Closes #4759

Summary*

I found this due to #6550.

We make heavy use of the num_bits method during compilation but this is currently doing a lot of unnecessary allocations in order to perform a full bit decomposition of the field element. We can rework this to work on the byte decomposition which removes 96% of all the temporary allocations from compilation.

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@TomAFrench TomAFrench requested a review from a team November 25, 2024 13:29
@asterite
Copy link
Collaborator

Incredible! On Friday I was looking at small and quick optimization opportunities and found a couple of them, one of them was this one (not with this algorithm, but I noticed num_bits was allocating too much).

Another thing I found is that FieldElement::hash does state.write(&self.to_be_bytes()); but I was wondering if we could directly call self.0.hash(state), which seems to compile but I don't know why that's not done (though I don't know if it's more efficient).

Another one I found was this double hashmap lookup:

if intermediate_variables.contains_key(&normalized_expr) {
let (l, iv) = intermediate_variables[&normalized_expr];

but I didn't send any PR because I wasn't sure if any of those improved the performance, really.

Copy link
Collaborator

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Looks good! I left a comment for something that might require an additional test.

@TomAFrench
Copy link
Member Author

Another thing I found is that FieldElement::hash does state.write(&self.to_be_bytes()); but I was wondering if we could directly call self.0.hash(state), which seems to compile but I don't know why that's not done (though I don't know if it's more efficient).

I can't see any meaningful benefit for this and it'll remove an allocation so let's do it.

@TomAFrench
Copy link
Member Author

Another one I found was this double hashmap lookup:

We end up hitting our peak memory usage during SSA rather than in the post-compilation transformations so I think we can ignore this for now.

@TomAFrench TomAFrench added this pull request to the merge queue Nov 25, 2024
Merged via the queue into master with commit 9a74dfa Nov 25, 2024
49 checks passed
@TomAFrench TomAFrench deleted the tf/remove-allocations-from-num-bits branch November 25, 2024 14:20
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.

Update FieldElement.num_bits() to return 1 for a zero field element.
2 participants