refactor(dpi/quic): single-allocation connection_id_to_hex#308
Open
0xghost42 wants to merge 1 commit into
Open
refactor(dpi/quic): single-allocation connection_id_to_hex#3080xghost42 wants to merge 1 commit into
connection_id_to_hex#3080xghost42 wants to merge 1 commit into
Conversation
`connection_id_to_hex` was rendering a QUIC connection ID through the
worst-case allocation pattern: `format!("{:02x}", b)` allocated a
2-byte `String` for every input byte, the iterator collected them into
a `Vec<String>` (one more allocation), and `join(":")` then walked the
vector to build the final `String`. For an 8-byte short-form DCID that
is 8 + 1 + 1 = 10 heap allocations per render.
Replace with a single `String::with_capacity(id.len() * 3 - 1)` (exact
final size: 2 hex chars per byte + N-1 colon separators) 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
input length, except for the empty-slice case which returns an empty
`String` (no allocation). The lowercase, colon-separated output is
unchanged.
Adds four regression tests covering:
- An 8-byte representative DCID — locks the lowercase + colon-separated
contract.
- A mix of single-digit bytes (0x00..=0x0f) — locks the zero-padding
contract that `{:02x}` provides.
- A single-byte input — locks that no trailing separator is emitted.
- The empty-slice base case.
Closes domcyrus#305
|
This PR, lik the other, 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. |
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
connection_id_to_hexwas rendering a QUIC connection ID through the worst-case allocation pattern:format!("{:02x}", b)allocated a 2-byteStringfor every input byte, the iterator collected them into aVec<String>(one more allocation), andjoin(":")then walked the vector to build the finalString. For an 8-byte short-form DCID that is 8 + 1 + 1 = 10 heap allocations per render.Replace with a single
String::with_capacity(id.len() * 3 - 1)(exact final size: 2 hex chars per byte + N-1 colon separators) and a per-bytewrite!. Writing into a pre-sizedStringdoes not reallocate, so the helper now performs exactly one heap allocation per call regardless of input length, except for the empty-slice case which returns an emptyString(no allocation). The lowercase, colon-separated output is unchanged.Tests
Four regression tests added:
{:02x}provides.Local checks:
cargo test --lib— 365 passed.cargo clippy --all-targets -- -D warnings— clean.cargo fmt --check— clean.Closes #305