refactor(dpi/bittorrent): single-allocation hex_encode for info-hash render#307
Merged
domcyrus merged 1 commit intoMay 21, 2026
Merged
Conversation
…render
`hex_encode` was building one heap-allocated `String` per byte via
`format!("{b:02x}")` and then collecting them into the final `String`.
A 20-byte BitTorrent info-hash therefore went through 20 intermediate
heap allocations on every peer-handshake analyze.
Replace the iterator with a single `String::with_capacity(bytes.len() * 2)`
and a per-byte `write!`. Writing into a pre-sized `String` does not
reallocate, so the helper now performs exactly one heap allocation per
call, regardless of the input length. The lowercase `{:02x}` output
contract is unchanged.
Adds three regression tests covering:
- The exact-size 20-byte info-hash case (the only size BitTorrent uses
in the wild) to lock the lowercase, fixed-width output.
- The empty-slice base case.
- A mix of single-digit bytes (0x00..=0x0f) to lock the zero-padding
contract that `{:02x}` provides.
Closes domcyrus#304
|
This PR is lacking any proof that this is a performance gain and or that the compiler does not optimize the current code to a single string write. |
Owner
|
@laundmo Thanks for your comment, I also think that without a benchmark the perf gain does not stand. On the other hand this does look to be a bit more correct to me. |
Owner
|
@0xghost42 LGTM! |
This was referenced May 25, 2026
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.
Summary
hex_encodewas building one heap-allocatedStringper byte viaformat!("{b:02x}")and then collecting them into the finalString. A 20-byte BitTorrent info-hash therefore went through 20 intermediate heap allocations on every peer-handshake analyze.Replace the iterator with a single
String::with_capacity(bytes.len() * 2)and a per-bytewrite!. Writing into a pre-sizedStringdoes not reallocate, so the helper now performs exactly one heap allocation per call regardless of the input length. The lowercase{:02x}output contract is unchanged.Tests
Three regression tests added:
{:02x}provides.Local checks:
cargo test --lib— 364 passed.cargo clippy --all-targets -- -D warnings— clean.cargo fmt --check— clean.Closes #304