Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes#194
Open
TheDistributor wants to merge 1 commit into
Open
Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes#194TheDistributor wants to merge 1 commit into
TheDistributor wants to merge 1 commit into
Conversation
Optimize wire-protocol integer serialization and deserialization utilities in pynuodb/crypt.py by replacing manual byte-shifting loops with native C-level `int.to_bytes()` and `int.from_bytes()` functions. Detailed changes: - toSignedByteString: Migrated to `int.to_bytes(..., signed=True)`. Uses the expression `((-value - 1).bit_length() + 8) // 8` for negative numbers to precisely calculate the minimal big-endian two's-complement byte-width without over-allocating an extra byte for border cases. - fromSignedByteString: Replaced manual backward bitwise loop parsing with a direct `int.from_bytes(..., signed=True)` call. - toByteString: Streamlined non-negative integer conversions using `int.to_bytes()`. Preserves original byte-identical behavior for 0, -1, and includes an explicit legacy fallback loop for other negative values to prevent unexpected edge cases. These changes eliminate high-overhead Python loops from critical data parsing paths, significantly improving throughput on integer-heavy protocol streams
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.
Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes
Replaces hand-rolled Python byte-shift loops with the C-implemented
int.to_bytes / int.from_bytes. Roughly 5-10x faster on typical integer sizes;
most visible during SRP handshake (large bignums) and on every encoded int
written to the wire.
About a 7% performance improvement all told, using FetchTest.py against the 512k row dataset.