feat: make BigUint::serialized_size allocation-free#1058
Open
Snezhkko wants to merge 1 commit intoarkworks-rs:masterfrom
Open
feat: make BigUint::serialized_size allocation-free#1058Snezhkko wants to merge 1 commit intoarkworks-rs:masterfrom
Snezhkko wants to merge 1 commit intoarkworks-rs:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, BigUint::serialized_size in serialize/src/impls/int_like.rs computed the size by calling to_bytes_le() and delegating to Vec’s size calculation. This performed a temporary Vec allocation solely to obtain the byte length, which is unnecessary and introduces avoidable overhead. The change replaces this with an allocation-free calculation based on BigUint::bits(), preserving the exact on-wire format including the zero edge case. For zero values, to_bytes_le() returns a single byte [0], so we return 8 + 1 for the length-prefixed encoding. For non-zero values, the payload length equals ceil(bits/8), so we compute 8 + ((bits + 7) / 8) directly. The behavior of serialization remains unchanged; only the size computation path is optimized to avoid allocation.