Skip to content

Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes#194

Open
TheDistributor wants to merge 1 commit into
masterfrom
mgallwey/accelerate-crypt
Open

Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes#194
TheDistributor wants to merge 1 commit into
masterfrom
mgallwey/accelerate-crypt

Conversation

@TheDistributor

@TheDistributor TheDistributor commented Jun 29, 2026

Copy link
Copy Markdown

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.

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
@TheDistributor TheDistributor self-assigned this Jun 29, 2026
@TheDistributor TheDistributor changed the title Optimize integer serialization with native builtins Optimize the crypt objects to/fromByteString methods to use native primitives where possible Jun 29, 2026
@TheDistributor TheDistributor changed the title Optimize the crypt objects to/fromByteString methods to use native primitives where possible Speed up int<->bytes conversions in crypt with int.to_bytes/from_bytes Jun 29, 2026
@TheDistributor TheDistributor requested review from bkelly-ndb and ribram and removed request for madscientist June 30, 2026 07:53
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.

1 participant