From 05e206865f57b483c3177a2d306cf95489145ac2 Mon Sep 17 00:00:00 2001 From: patrick-immometrica Date: Sat, 14 Nov 2026 12:19:31 +0100 Subject: [PATCH] perf(http1): write chunk size hex without fmt ChunkSize::new went through write! for every outgoing chunk. Write the hex digits directly instead. About 3x faster for 100 small chunks. --- src/proto/h1/encode.rs | 64 ++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index 4a9fd4348b..fbeff5557c 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -347,14 +347,20 @@ struct ChunkSize { } impl ChunkSize { + #[allow(clippy::cast_possible_truncation)] fn new(len: usize) -> ChunkSize { - use std::fmt::Write; let mut size = ChunkSize { bytes: [0; CHUNK_SIZE_MAX_BYTES + 2], pos: 0, len: 0, }; - write!(&mut size, "{len:X}\r\n").expect("CHUNK_SIZE_MAX_BYTES should fit any usize"); + // avoid going through fmt for every chunk + let digits = ((usize::BITS - len.leading_zeros() + 3) >> 2).max(1) as usize; + for i in 0..digits { + size.bytes[digits - 1 - i] = b"0123456789ABCDEF"[(len >> (i * 4)) & 0xF]; + } + size.bytes[digits..digits + 2].copy_from_slice(b"\r\n"); + size.len = (digits + 2) as u8; size } } @@ -387,19 +393,6 @@ impl fmt::Debug for ChunkSize { } } -impl fmt::Write for ChunkSize { - #[allow(clippy::cast_possible_truncation)] - fn write_str(&mut self, num: &str) -> fmt::Result { - use std::io::Write; - (&mut self.bytes[self.len.into()..]) - .write_all(num.as_bytes()) - .expect("&mut [u8].write() cannot error"); - debug_assert!(u8::try_from(num.len()).is_ok()); - self.len += num.len() as u8; // safe because bytes is never bigger than 255 - Ok(()) - } -} - impl From for EncodedBuf { fn from(buf: B) -> Self { EncodedBuf { @@ -700,4 +693,45 @@ mod tests { dst.put(buf); assert_eq!(dst, b"0\r\nchunky-trailer: trailer value\r\n\r\n"); } + + #[cfg(feature = "nightly")] + #[bench] + fn bench_encode_chunked(b: &mut ::test::Bencher) { + use bytes::{Buf, Bytes}; + let chunk = Bytes::from_static(&[b'x'; 300]); + let sizes = ::test::black_box([1usize, 15, 200, 300]); + b.iter(|| { + let mut enc = Encoder::chunked(); + let mut total = 0; + for i in 0..100 { + let buf = enc.encode(chunk.slice(..sizes[i % 4])); + total += buf.remaining(); + ::test::black_box(&buf); + } + total + }) + } + + #[test] + fn chunk_size_matches_fmt() { + use super::ChunkSize; + use bytes::Buf; + for n in [ + 0usize, + 1, + 9, + 10, + 15, + 16, + 255, + 256, + 4095, + 65535, + 1 << 20, + usize::MAX, + ] { + let s = ChunkSize::new(n); + assert_eq!(s.chunk(), format!("{n:X}\r\n").as_bytes(), "{n}"); + } + } }