Skip to content

dpi(quic): connection_id_to_hex allocates per-byte Strings plus a Vec for join #305

@0xghost42

Description

@0xghost42

Summary

quic::connection_id_to_hex (src/network/dpi/quic.rs:1881-1886) renders a QUIC connection ID to a colon-separated lowercase hex string via the worst-case allocation pattern:

fn connection_id_to_hex(id: &[u8]) -> String {
    id.iter()
        .map(|b| format!("{:02x}", b))
        .collect::<Vec<String>>()
        .join(":")
}

Each byte goes through format!("{:02x}", b), allocating a 2-byte String. The whole iterator then collects into a Vec<String> (one more heap allocation), and join(":") finally walks the vector to build the result String. For an 8-byte connection ID (common short form) that is 8 String allocations + 1 Vec allocation + 1 final String allocation per render.

Evidence

  • Helper definition: src/network/dpi/quic.rs:1881-1886
  • Helper is invoked when rendering QuicInfo.connection_id for the UI.

Proposed fix

Replace with a single accumulator String::with_capacity(id.len() * 3) (2*N hex + N-1 separators), iterating bytes and using write!(buf, "{b:02x}") (writing to an in-place String does not allocate when capacity is reserved), inserting ':' between bytes. One allocation per call, output byte-for-byte identical.

Happy to open a PR with a regression test that locks the existing aa:bb:cc:dd lowercase-with-colon format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions