From 253f053cd62ca3e5879ef795363ca42fd3dc4834 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 05:25:29 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/a2/protocol-buffering.md Quest-Claim: fc03784d30d8067f From 70573866205bd3e7a7d758d4cc587652a4bb1b2c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 05:41:30 -0800 Subject: [PATCH 2/5] Remove measured protocol payload staging and string copies Measure frame lengths before encoding and reserve final async buffers. Retain bounded reads and validate framing across partial I/O and prefix-width boundaries. Co-Authored-By: GPT-6.1 Sol --- quest/a2/README.md | 1 - quest/a2/protocol-buffering.md | 19 ------------------- 2 files changed, 20 deletions(-) delete mode 100644 quest/a2/protocol-buffering.md diff --git a/quest/a2/README.md b/quest/a2/README.md index dc5516cb..d4976dc6 100644 --- a/quest/a2/README.md +++ b/quest/a2/README.md @@ -14,7 +14,6 @@ The Required list is ordered by priority. Independent children may proceed concu - [Native metric integration](/quest/a2/native-stream-stats.md) - actual native delivered-versus-queued progress - [Native metric availability](/quest/a2/native-stats-upstream.md) - advance external accessor prerequisites - [QMux draft readiness](/quest/a2/qmux-draft-readiness.md) - establish strict-validation compatibility boundary -- [Protocol buffering](/quest/a2/protocol-buffering.md) - measure and remove redundant copying - [Quiche send-rate audit](/quest/a2/quiche-delivery-rate.md) - app-limited samples under legacy controllers - [Quinn provider-neutral rustls release](/quest/a2/quinn-provider-release.md) - waiting on quinn-rs/quinn#2341 - [Quinn adapter removal](/quest/a2/quinn-adapter-removal.md) - switch to upstream and delete the port diff --git a/quest/a2/protocol-buffering.md b/quest/a2/protocol-buffering.md deleted file mode 100644 index 7a28fd69..00000000 --- a/quest/a2/protocol-buffering.md +++ /dev/null @@ -1,19 +0,0 @@ -# [M] Remove measured redundant protocol buffering - -## Goal - -CONNECT/settings encoding and decoding avoid demonstrably redundant copies while preserving protocol correctness and a maintainable implementation. - -## Plan - -Measure allocation and copying in representative encode/write and read/decode paths before changing them. Start with the temporary QPACK buffer copied into a frame and then buffered again for async write. Prefer a small reusable encoding change over a second parser or broad lifetime-heavy public API. - -Preserve size limits, partial-I/O behavior and exact frame consumption so following capsules remain unread. Compare before/after allocation/copy behavior and retain wire-format regressions. Owned decoded headers may still require allocation; do not promise universal zero-copy reads or invent a performance threshold. Coordinate with response-header work if both touch the same codec. - -## Closes - -- [#169](https://github.com/moq-dev/web-transport/issues/169) - the implementation completes this report - -## Related - -- [Response headers](/quest/a1/response-headers.md) - overlapping codec surface; no hard dependency From c9f69ef3b1a1599886dfce08cf6fc19e48ce2bb9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 05:41:44 -0800 Subject: [PATCH 3/5] Encode protocol frames directly into measured buffers Remove QPACK payload staging, pseudo-header sorting storage, and duplicate string copies. Add allocation/copy benchmarks and framing regressions while preserving owned decoded headers and bounded reads. Co-Authored-By: GPT-6.1 Sol --- rs/web-transport-proto/Cargo.toml | 4 + rs/web-transport-proto/README.md | 15 ++ rs/web-transport-proto/benches/buffering.rs | 272 ++++++++++++++++++++ rs/web-transport-proto/src/connect.rs | 59 +++-- rs/web-transport-proto/src/huffman.rs | 4 +- rs/web-transport-proto/src/qpack.rs | 109 +++++++- rs/web-transport-proto/src/settings.rs | 35 ++- rs/web-transport-proto/tests/framing.rs | 151 +++++++++++ 8 files changed, 603 insertions(+), 46 deletions(-) create mode 100644 rs/web-transport-proto/benches/buffering.rs create mode 100644 rs/web-transport-proto/tests/framing.rs diff --git a/rs/web-transport-proto/Cargo.toml b/rs/web-transport-proto/Cargo.toml index ae2ab7ab..db30cdeb 100644 --- a/rs/web-transport-proto/Cargo.toml +++ b/rs/web-transport-proto/Cargo.toml @@ -24,3 +24,7 @@ url = "2" [dev-dependencies] tokio = { version = "1", features = ["macros", "rt", "io-util"] } + +[[bench]] +name = "buffering" +harness = false diff --git a/rs/web-transport-proto/README.md b/rs/web-transport-proto/README.md index 96c22185..74709c7e 100644 --- a/rs/web-transport-proto/README.md +++ b/rs/web-transport-proto/README.md @@ -16,3 +16,18 @@ these paths report `ConnectError::FieldSectionTooLarge`. `Settings::enable_webtransport` advertises this bound through `MAX_FIELD_SECTION_SIZE`. SETTINGS, capsule, and GREASE frames retain their 64 KiB payload bound. + +Encoding measures field sections and SETTINGS pairs before writing them directly +into the destination. Async writes allocate one frame buffer at its measured size. +Decoding keeps owned header strings; synchronous decoding borrows the frame, and +async reads buffer only the bounded payload without consuming the next frame. + +Run `cargo bench -p web-transport-proto --bench buffering` to measure allocations, +reallocations, requested allocation bytes and encode boundary copies for CONNECT +and SETTINGS. It covers header count, value size, Huffman strings, borrowed and +owned input, and partial async writes. Fixture and runtime setup are excluded. +`largest_encode_slice` is the largest `put_slice` into caller-provided storage, +not a count of all internal memory copies. Requested bytes include every allocation +and reallocation request, not peak live memory; header map order can affect buffer +growth. Timing includes allocation instrumentation and is diagnostic, not a gate. +The benchmark also runs under the existing `just test` all-targets check. diff --git a/rs/web-transport-proto/benches/buffering.rs b/rs/web-transport-proto/benches/buffering.rs new file mode 100644 index 00000000..8cb99c36 --- /dev/null +++ b/rs/web-transport-proto/benches/buffering.rs @@ -0,0 +1,272 @@ +//! Allocation and boundary-copy measurements, also exercised by `just test`. +use std::{ + alloc::{GlobalAlloc, Layout, System}, + cell::Cell, + hint::black_box, + io::Cursor, + pin::Pin, + task::{Context, Poll}, + time::Instant, +}; + +use bytes::{buf::UninitSlice, BufMut, Bytes}; +use tokio::io::AsyncWrite; +use web_transport_proto::{ + http, ConnectRequest, ConnectResponse, Frame, Setting, Settings, VarInt, +}; + +#[derive(Clone, Copy, Default)] +struct Allocations { + allocations: usize, + reallocations: usize, + requested: usize, +} + +thread_local! { + static ALLOCATIONS: Cell> = const { Cell::new(None) }; +} + +struct Allocator; + +fn record(size: usize, reallocation: bool) { + ALLOCATIONS.with(|cell| { + if let Some(mut stats) = cell.get() { + stats.allocations += usize::from(!reallocation); + stats.reallocations += usize::from(reallocation); + stats.requested += size; + cell.set(Some(stats)); + } + }); +} + +// SAFETY: every operation forwards its pointer and layout unchanged to System. +unsafe impl GlobalAlloc for Allocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + record(layout.size(), false); + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + record(layout.size(), false); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 { + record(size, true); + unsafe { System.realloc(ptr, layout, size) } + } +} + +#[global_allocator] +static ALLOCATOR: Allocator = Allocator; + +struct Output<'a> { + remaining: &'a mut [u8], + largest_slice: usize, +} + +// SAFETY: storage and advancement are delegated to the slice's BufMut impl. +unsafe impl BufMut for Output<'_> { + fn remaining_mut(&self) -> usize { + self.remaining.remaining_mut() + } + + fn chunk_mut(&mut self) -> &mut UninitSlice { + self.remaining.chunk_mut() + } + + unsafe fn advance_mut(&mut self, cnt: usize) { + unsafe { self.remaining.advance_mut(cnt) } + } + + fn put_slice(&mut self, src: &[u8]) { + self.largest_slice = self.largest_slice.max(src.len()); + self.remaining.put_slice(src); + } +} + +struct Writer(usize); + +impl AsyncWrite for Writer { + fn poll_write( + mut self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + // Exercise partial writes without allocating another output buffer. + let size = buf.len().min(7); + black_box(&buf[..size]); + self.0 += size; + Poll::Ready(Ok(size)) + } + + fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } +} + +fn measure(name: &str, wire_size: usize, mut operation: impl FnMut() -> usize) { + // Warm up runtime and thread-local initialization outside the measurement. + black_box(operation()); + ALLOCATIONS.with(|cell| cell.set(Some(Allocations::default()))); + let start = Instant::now(); + let mut observed = 0; + const ITERATIONS: usize = 256; + for _ in 0..ITERATIONS { + observed += black_box(operation()); + } + let elapsed = start.elapsed().as_nanos() / ITERATIONS as u128; + let stats = ALLOCATIONS.with(|cell| cell.take().unwrap()); + println!( + "{name},{wire_size},{},{},{},{elapsed},{}", + stats.allocations / ITERATIONS, + stats.reallocations / ITERATIONS, + stats.requested / ITERATIONS, + observed / ITERATIONS, + ); +} + +fn main() { + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + println!( + "path,wire_bytes,allocations,reallocations,requested_bytes,ns_per_op,largest_encode_slice" + ); + let mut storage = vec![0; 65560]; + + macro_rules! paths { + ($name:expr, $value:expr, $ty:ty) => {{ + let value = $value; + let mut wire = Vec::new(); + value.encode(&mut wire).unwrap(); + measure(&format!("{}/encode", $name), wire.len(), || { + let mut output = Output { + remaining: &mut storage, + largest_slice: 0, + }; + value.encode(&mut output).unwrap(); + output.largest_slice + }); + measure(&format!("{}/write", $name), wire.len(), || { + let mut writer = Writer(0); + runtime.block_on(value.write(&mut writer)).unwrap(); + assert_eq!(writer.0, wire.len()); + 0 + }); + measure(&format!("{}/decode", $name), wire.len(), || { + black_box(<$ty>::decode(&mut wire.as_slice()).unwrap()); + 0 + }); + let owned = Bytes::copy_from_slice(&wire); + measure(&format!("{}/decode_bytes", $name), wire.len(), || { + black_box(<$ty>::decode(&mut owned.clone()).unwrap()); + 0 + }); + measure(&format!("{}/read", $name), wire.len(), || { + black_box( + runtime + .block_on(<$ty>::read(&mut Cursor::new(&wire))) + .unwrap(), + ); + 0 + }); + }}; + } + + for (fields, value_size) in [(0, 0), (8, 128), (1, 12000)] { + let mut request = ConnectRequest::new( + "https://example.com/path?query=1" + .parse::() + .unwrap(), + ) + .with_protocol("moq"); + for i in 0..fields { + request.headers.append( + http::HeaderName::from_bytes(format!("x-field-{i}").as_bytes()).unwrap(), + http::HeaderValue::from_str(&"a".repeat(value_size)).unwrap(), + ); + } + paths!( + &format!("request/{fields}x{value_size}"), + request, + ConnectRequest + ); + } + paths!( + "response", + ConnectResponse::OK.with_protocol("moq"), + ConnectResponse + ); + + // 1024 Huffman-coded 'a' bytes, using RFC 7541 Appendix B's five-bit code. + let mut section = vec![0, 0, 0xd9, 0x26]; + section.extend_from_slice(b"x-data"); + section.extend_from_slice(&[0xff, 0x81, 0x04]); // Huffman flag and length 640. + for _ in 0..128 { + section.extend_from_slice(&[0x18, 0xc6, 0x31, 0x8c, 0x63]); + } + let mut wire = Vec::new(); + Frame::HEADERS.encode(&mut wire); + VarInt::from_u32(section.len() as u32).encode(&mut wire); + wire.extend_from_slice(§ion); + measure("huffman/decode", wire.len(), || { + black_box(ConnectResponse::decode(&mut wire.as_slice()).unwrap()); + 0 + }); + measure("huffman/read", wire.len(), || { + black_box( + runtime + .block_on(ConnectResponse::read(&mut Cursor::new(&wire))) + .unwrap(), + ); + 0 + }); + + for count in [0, 64] { + let mut settings = Settings::default(); + settings.enable_webtransport(4); + for i in 0..count { + settings.insert( + Setting(VarInt::from_u32(0x1000 + i)), + VarInt::from_u32(1 << 20), + ); + } + let mut wire = Vec::new(); + settings.encode(&mut wire); + measure(&format!("settings/{count}/encode"), wire.len(), || { + let mut output = Output { + remaining: &mut storage, + largest_slice: 0, + }; + settings.encode(&mut output); + output.largest_slice + }); + measure(&format!("settings/{count}/write"), wire.len(), || { + let mut writer = Writer(0); + runtime.block_on(settings.write(&mut writer)).unwrap(); + assert_eq!(writer.0, wire.len()); + 0 + }); + measure(&format!("settings/{count}/decode"), wire.len(), || { + black_box(Settings::decode(&mut wire.as_slice()).unwrap()); + 0 + }); + measure(&format!("settings/{count}/read"), wire.len(), || { + black_box( + runtime + .block_on(Settings::read(&mut Cursor::new(&wire))) + .unwrap(), + ); + 0 + }); + } +} diff --git a/rs/web-transport-proto/src/connect.rs b/rs/web-transport-proto/src/connect.rs index ecb62710..96942a53 100644 --- a/rs/web-transport-proto/src/connect.rs +++ b/rs/web-transport-proto/src/connect.rs @@ -221,6 +221,11 @@ impl ConnectRequest { } pub fn encode(&self, buf: &mut B) -> Result<(), ConnectError> { + self.frame()?.encode(buf); + Ok(()) + } + + fn frame(&self) -> Result { let mut headers = qpack::Headers::default(); for (item_header_name, item_header_value) in self.headers.iter() { // Skip protocol negotiation headers; they are derived from `self.protocols`. @@ -249,14 +254,11 @@ impl ConnectRequest { headers.set(protocol_negotiation::AVAILABLE_NAME, &encoded); } - encode_headers_frame(&headers, buf) + HeadersFrame::new(headers) } pub async fn write(&self, stream: &mut S) -> Result<(), ConnectError> { - let mut buf = BytesMut::new(); - self.encode(&mut buf)?; - stream.write_all_buf(&mut buf).await?; - Ok(()) + self.frame()?.write(stream).await } } @@ -398,6 +400,11 @@ impl ConnectResponse { } pub fn encode(&self, buf: &mut B) -> Result<(), ConnectError> { + self.frame()?.encode(buf); + Ok(()) + } + + fn frame(&self) -> Result { let mut headers = qpack::Headers::default(); headers.set(":status", self.status.as_str()); headers.set("sec-webtransport-http3-draft", "draft02"); @@ -407,14 +414,11 @@ impl ConnectResponse { headers.set(protocol_negotiation::SELECTED_NAME, &encoded); } - encode_headers_frame(&headers, buf) + HeadersFrame::new(headers) } pub async fn write(&self, stream: &mut S) -> Result<(), ConnectError> { - let mut buf = BytesMut::new(); - self.encode(&mut buf)?; - stream.write_all_buf(&mut buf).await?; - Ok(()) + self.frame()?.write(stream).await } } @@ -443,20 +447,31 @@ fn check_frame_size(typ: Frame, size: u64) -> Result<(), ConnectError> { Ok(()) } -fn encode_headers_frame( - headers: &qpack::Headers, - buf: &mut B, -) -> Result<(), ConnectError> { - // Use a temporary buffer so we can compute the size and write nothing on error. - let mut tmp = Vec::new(); - headers.encode(&mut tmp)?; - let size = VarInt::from_u32(tmp.len() as u32); +// A validated field section and its wire length, shared by sync and async encoding. +struct HeadersFrame { + headers: qpack::Headers, + size: VarInt, +} - Frame::HEADERS.encode(buf); - size.encode(buf); - buf.put_slice(&tmp); +impl HeadersFrame { + fn new(headers: qpack::Headers) -> Result { + let size = VarInt::from_u32(headers.encoded_size()? as u32); + Ok(Self { headers, size }) + } - Ok(()) + fn encode(&self, buf: &mut B) { + Frame::HEADERS.encode(buf); + self.size.encode(buf); + self.headers.encode(buf); + } + + async fn write(&self, stream: &mut S) -> Result<(), ConnectError> { + let capacity = Frame::HEADERS.0.size() + self.size.size() + self.size.into_inner() as usize; + let mut buf = BytesMut::with_capacity(capacity); + self.encode(&mut buf); + stream.write_all_buf(&mut buf).await?; + Ok(()) + } } fn decode_headers_frame(buf: &mut B) -> Result, ConnectError> { diff --git a/rs/web-transport-proto/src/huffman.rs b/rs/web-transport-proto/src/huffman.rs index 333ed3af..6517bafc 100644 --- a/rs/web-transport-proto/src/huffman.rs +++ b/rs/web-transport-proto/src/huffman.rs @@ -335,7 +335,7 @@ bits_decode![ pub struct DecodeIter<'a> { bit_pos: BitWindow, - content: &'a Vec, + content: &'a [u8], } impl Iterator for DecodeIter<'_> { @@ -354,7 +354,7 @@ pub trait HpackStringDecode { fn hpack_decode(&self) -> DecodeIter<'_>; } -impl HpackStringDecode for Vec { +impl HpackStringDecode for [u8] { fn hpack_decode(&self) -> DecodeIter<'_> { DecodeIter { bit_pos: BitWindow::new(), diff --git a/rs/web-transport-proto/src/qpack.rs b/rs/web-transport-proto/src/qpack.rs index 9695691b..cc1c5b80 100644 --- a/rs/web-transport-proto/src/qpack.rs +++ b/rs/web-transport-proto/src/qpack.rs @@ -195,28 +195,44 @@ impl Headers { Ok((name, value)) } - /// Encodes the field section, refusing one the decoder would reject as too large. + /// Measures the field section, refusing one the decoder would reject as too large. /// - /// Nothing is written on error. Without Huffman coding, each encoded field line - /// is shorter than its decoded cost, so the encoded section also fits the limit. - pub(crate) fn encode(&self, buf: &mut B) -> Result<(), FieldSectionError> { + /// Without Huffman coding, each encoded field line is shorter than its decoded + /// cost, so the encoded section also fits the limit. + pub(crate) fn encoded_size(&self) -> Result { let mut budget = Budget::new(); + let mut size = 2; // Required Insert Count and Delta Base. for (name, value) in &self.fields { budget.charge(FIELD_LINE_OVERHEAD)?; budget.charge(name.len())?; budget.charge(value.len())?; + + size += if let Some(index) = StaticTable::find(name, value) { + prefix_size(6, index) + } else if let Some(index) = StaticTable::find_name(name) { + prefix_size(4, index) + prefix_size(7, value.len()) + value.len() + } else { + prefix_size(3, name.len()) + name.len() + prefix_size(7, value.len()) + value.len() + }; } + Ok(size) + } + /// Encodes a field section validated by `encoded_size`. + pub(crate) fn encode(&self, buf: &mut B) { // We don't support dynamic entries so we can skip these. encode_prefix(buf, 8, 0, 0); encode_prefix(buf, 7, 0, 0); // We must encode pseudo-headers first. // https://datatracker.ietf.org/doc/html/rfc9114#section-4.1.2 - let mut headers: Vec<_> = self.fields.iter().collect(); - headers.sort_by_key(|&(key, _)| !key.starts_with(':')); + let headers = self + .fields + .iter() + .filter(|(key, _)| key.starts_with(':')) + .chain(self.fields.iter().filter(|(key, _)| !key.starts_with(':'))); - for (name, value) in headers.iter() { + for (name, value) in headers { if let Some(index) = StaticTable::find(name, value) { Self::encode_index(buf, index) } else if let Some(index) = StaticTable::find_name(name) { @@ -225,7 +241,6 @@ impl Headers { Self::encode_literal(buf, name, value) } } - Ok(()) } fn encode_index(buf: &mut B, index: usize) { @@ -360,6 +375,20 @@ pub fn encode_prefix(buf: &mut B, size: u8, flags: u8, value: usize) buf.put_u8(remaining as u8); } +fn prefix_size(bits: u8, value: usize) -> usize { + let mask = (1 << bits) - 1; + if value < mask { + return 1; + } + let mut remaining = value - mask; + let mut size = 2; + while remaining >= 128 { + remaining /= 128; + size += 1; + } + size +} + fn decode_string( buf: &mut B, size: u8, @@ -375,9 +404,11 @@ fn decode_string( let value = if flags & 1 == 0 { budget.charge(len)?; - buf.copy_to_bytes(len).to_vec() + let mut value = vec![0; len]; + buf.copy_to_slice(&mut value); + value } else { - let encoded = buf.copy_to_bytes(len).to_vec(); + let encoded = buf.copy_to_bytes(len); let mut decoded = Vec::new(); for byte in encoded.hpack_decode() { let byte = byte.map_err(DecodeError::from)?; @@ -686,6 +717,64 @@ const PREDEFINED_HEADERS: [(&str, &str); 99] = [ mod tests { use super::*; + #[test] + fn measured_prefixes_match_wire_at_width_boundaries() { + for bits in 1..=8 { + let mask = (1 << bits) - 1; + for value in [ + 0, + mask - 1, + mask, + mask + 1, + mask + 127, + mask + 128, + mask + 16383, + mask + 16384, + usize::MAX, + ] { + let mut wire = Vec::new(); + encode_prefix(&mut wire, bits, 0, value); + assert_eq!(prefix_size(bits, value), wire.len()); + assert_eq!(decode_prefix(&mut wire.as_slice(), bits).unwrap().1, value); + } + } + } + + #[test] + fn measured_sections_cover_each_field_representation() { + for len in [0, 6, 7, 126, 127, 128, 254, 255, 256, 4096] { + let mut headers = Headers::default(); + headers.set("x-first", "ordinary"); + headers.set(":status", "200"); // Full static index. + headers.set("user-agent", &"a".repeat(len)); // Static name index > 15. + headers.set(&"b".repeat(len), &"c".repeat(len)); // Literal name and value. + let size = headers.encoded_size().unwrap(); + let mut wire = Vec::new(); + headers.encode(&mut wire); + assert_eq!(size, wire.len()); + assert_eq!(&wire[..3], &[0, 0, 0xd9], "pseudo-header is first"); + + // Split every byte to exercise string copying across arbitrary Buf chunks. + struct Chunks<'a>(&'a [u8]); + impl Buf for Chunks<'_> { + fn remaining(&self) -> usize { + self.0.len() + } + fn chunk(&self) -> &[u8] { + &self.0[..self.0.len().min(1)] + } + fn advance(&mut self, cnt: usize) { + self.0 = &self.0[cnt..]; + } + } + let decoded = Headers::decode(&mut Chunks(&wire)).unwrap(); + assert_eq!(decoded.fields.len(), headers.fields.len()); + for (name, value) in &headers.fields { + assert_eq!(decoded.get(name), Some(value.as_str())); + } + } + } + #[test] fn strings_charge_before_output_and_validate_at_exact_budget() { let mut plain = Vec::new(); diff --git a/rs/web-transport-proto/src/settings.rs b/rs/web-transport-proto/src/settings.rs index 0d2e0e08..44c7c5c1 100644 --- a/rs/web-transport-proto/src/settings.rs +++ b/rs/web-transport-proto/src/settings.rs @@ -209,25 +209,36 @@ impl Settings { } pub fn encode(&self, buf: &mut B) { + self.encode_frame(self.payload_size(), buf); + } + + fn payload_size(&self) -> VarInt { + let size: usize = self + .0 + .iter() + .map(|(id, value)| id.0.size() + value.size()) + .sum(); + VarInt::try_from(size).expect("settings size fits a varint") + } + + fn encode_frame(&self, size: VarInt, buf: &mut B) { StreamUni::CONTROL.encode(buf); Frame::SETTINGS.encode(buf); - - // Encode to a temporary buffer so we can learn the length. - // TODO avoid doing this, just use a fixed size varint. - let mut tmp = Vec::new(); + size.encode(buf); for (id, value) in &self.0 { - id.encode(&mut tmp); - value.encode(&mut tmp); + id.encode(buf); + value.encode(buf); } - - VarInt::from_u32(tmp.len() as u32).encode(buf); - buf.put_slice(&tmp); } pub async fn write(&self, stream: &mut S) -> Result<(), SettingsError> { - // TODO avoid allocating to the heap - let mut buf = BytesMut::new(); - self.encode(&mut buf); + let size = self.payload_size(); + let capacity = StreamUni::CONTROL.0.size() + + Frame::SETTINGS.0.size() + + size.size() + + size.into_inner() as usize; + let mut buf = BytesMut::with_capacity(capacity); + self.encode_frame(size, &mut buf); stream.write_all_buf(&mut buf).await?; Ok(()) } diff --git a/rs/web-transport-proto/tests/framing.rs b/rs/web-transport-proto/tests/framing.rs new file mode 100644 index 00000000..77a01e90 --- /dev/null +++ b/rs/web-transport-proto/tests/framing.rs @@ -0,0 +1,151 @@ +use std::{ + pin::Pin, + task::{Context, Poll}, +}; + +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; +use web_transport_proto::{ + http, Capsule, ConnectRequest, ConnectResponse, Setting, Settings, VarInt, +}; + +struct Fragmented { + bytes: Vec, + position: usize, + fragment: usize, + pending: bool, +} + +impl Fragmented { + fn new(fragment: usize) -> Self { + Self { + bytes: Vec::new(), + position: 0, + fragment, + pending: true, + } + } + + fn pause(&mut self, cx: &mut Context<'_>) -> bool { + let pending = self.pending; + self.pending = !pending; + if pending { + cx.waker().wake_by_ref(); + } + pending + } +} + +impl AsyncRead for Fragmented { + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut ReadBuf<'_>, + ) -> Poll> { + if self.pause(cx) { + return Poll::Pending; + } + let size = self + .fragment + .min(buf.remaining()) + .min(self.bytes.len() - self.position); + buf.put_slice(&self.bytes[self.position..self.position + size]); + self.position += size; + Poll::Ready(Ok(())) + } +} + +impl AsyncWrite for Fragmented { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + if self.pause(cx) { + return Poll::Pending; + } + let size = self.fragment.min(buf.len()); + self.bytes.extend_from_slice(&buf[..size]); + Poll::Ready(Ok(size)) + } + + fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } +} + +#[tokio::test] +async fn fragmented_connect_keeps_following_capsules_unread() { + let capsule = Capsule::CloseWebTransportSession { + code: 42, + reason: "done".into(), + }; + let request = ConnectRequest::new( + "https://example.com/path?query=1" + .parse::() + .unwrap(), + ) + .with_protocol("moq") + .with_header( + http::HeaderName::from_static("x-large"), + http::HeaderValue::from_str(&"a".repeat(4096)).unwrap(), + ); + let response = ConnectResponse::OK.with_protocol("moq"); + for fragment in [1, 7, 4096] { + let mut stream = Fragmented::new(fragment); + request.write(&mut stream).await.unwrap(); + let size = stream.bytes.len(); + capsule.encode(&mut stream.bytes); + let decoded = ConnectRequest::read(&mut stream).await.unwrap(); + assert_eq!(decoded.url, request.url); + assert_eq!(decoded.headers, request.headers); + assert_eq!(decoded.protocols, request.protocols); + assert_eq!(stream.position, size); + assert_eq!( + Capsule::read(&mut stream).await.unwrap(), + Some(capsule.clone()) + ); + + let mut stream = Fragmented::new(fragment); + response.write(&mut stream).await.unwrap(); + let size = stream.bytes.len(); + capsule.encode(&mut stream.bytes); + let decoded = ConnectResponse::read(&mut stream).await.unwrap(); + assert_eq!(decoded.status, response.status); + assert_eq!(decoded.protocol, response.protocol); + assert_eq!(stream.position, size); + assert_eq!( + Capsule::read(&mut stream).await.unwrap(), + Some(capsule.clone()) + ); + } +} + +#[tokio::test] +async fn settings_lengths_cover_varint_widths_and_partial_io() { + let mut settings = Settings::default(); + for id in [0, 63, 64, 16383, 16384, 1 << 30, VarInt::MAX.into_inner()] { + settings.insert( + Setting(VarInt::try_from(id).unwrap()), + VarInt::try_from(id).unwrap(), + ); + } + settings.enable_webtransport(4); + for fragment in [1, 7, 4096] { + let mut stream = Fragmented::new(fragment); + settings.write(&mut stream).await.unwrap(); + let mut expected = Vec::new(); + settings.encode(&mut expected); + assert_eq!(stream.bytes, expected); + let size = stream.bytes.len(); + stream.bytes.extend_from_slice(b"next frame"); + let decoded = Settings::read(&mut stream).await.unwrap(); + for (id, value) in settings.iter().filter(|(id, _)| !id.is_grease()) { + assert_eq!(decoded.get(id), Some(value)); + } + assert_eq!(stream.position, size); + } +} From 1c5ea07ab4da0201dc8c6af62f972995858a4d7c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 05:51:08 -0800 Subject: [PATCH 4/5] Share QPACK wire choices and borrow contiguous Huffman input Preserve Huffman string consumption on errors and cover fragmented input. Report exact benchmark counter totals so occasional reallocations remain visible. Co-Authored-By: GPT-6.1 Sol --- rs/web-transport-proto/README.md | 4 +- rs/web-transport-proto/benches/buffering.rs | 13 +- rs/web-transport-proto/src/qpack.rs | 180 +++++++++++--------- rs/web-transport-proto/tests/framing.rs | 2 +- 4 files changed, 111 insertions(+), 88 deletions(-) diff --git a/rs/web-transport-proto/README.md b/rs/web-transport-proto/README.md index 74709c7e..677d53a1 100644 --- a/rs/web-transport-proto/README.md +++ b/rs/web-transport-proto/README.md @@ -19,13 +19,15 @@ these paths report `ConnectError::FieldSectionTooLarge`. Encoding measures field sections and SETTINGS pairs before writing them directly into the destination. Async writes allocate one frame buffer at its measured size. -Decoding keeps owned header strings; synchronous decoding borrows the frame, and +Huffman decoding borrows contiguous encoded strings and coalesces fragmented +ones for the existing decoder. Decoding keeps owned header strings; synchronous decoding borrows the frame, and async reads buffer only the bounded payload without consuming the next frame. Run `cargo bench -p web-transport-proto --bench buffering` to measure allocations, reallocations, requested allocation bytes and encode boundary copies for CONNECT and SETTINGS. It covers header count, value size, Huffman strings, borrowed and owned input, and partial async writes. Fixture and runtime setup are excluded. +The CSV records allocation totals and the iteration count without rounding. `largest_encode_slice` is the largest `put_slice` into caller-provided storage, not a count of all internal memory copies. Requested bytes include every allocation and reallocation request, not peak live memory; header map order can affect buffer diff --git a/rs/web-transport-proto/benches/buffering.rs b/rs/web-transport-proto/benches/buffering.rs index 8cb99c36..8896552b 100644 --- a/rs/web-transport-proto/benches/buffering.rs +++ b/rs/web-transport-proto/benches/buffering.rs @@ -121,16 +121,13 @@ fn measure(name: &str, wire_size: usize, mut operation: impl FnMut() -> usize) { let mut observed = 0; const ITERATIONS: usize = 256; for _ in 0..ITERATIONS { - observed += black_box(operation()); + observed = observed.max(black_box(operation())); } let elapsed = start.elapsed().as_nanos() / ITERATIONS as u128; let stats = ALLOCATIONS.with(|cell| cell.take().unwrap()); println!( - "{name},{wire_size},{},{},{},{elapsed},{}", - stats.allocations / ITERATIONS, - stats.reallocations / ITERATIONS, - stats.requested / ITERATIONS, - observed / ITERATIONS, + "{name},{wire_size},{ITERATIONS},{},{},{},{elapsed},{observed}", + stats.allocations, stats.reallocations, stats.requested, ); } @@ -139,7 +136,7 @@ fn main() { .build() .unwrap(); println!( - "path,wire_bytes,allocations,reallocations,requested_bytes,ns_per_op,largest_encode_slice" + "path,wire_bytes,iterations,allocations,reallocations,requested_bytes,ns_per_op,largest_encode_slice" ); let mut storage = vec![0; 65560]; @@ -203,7 +200,7 @@ fn main() { } paths!( "response", - ConnectResponse::OK.with_protocol("moq"), + ConnectResponse::default().with_protocol("moq"), ConnectResponse ); diff --git a/rs/web-transport-proto/src/qpack.rs b/rs/web-transport-proto/src/qpack.rs index cc1c5b80..3f806870 100644 --- a/rs/web-transport-proto/src/qpack.rs +++ b/rs/web-transport-proto/src/qpack.rs @@ -207,13 +207,10 @@ impl Headers { budget.charge(name.len())?; budget.charge(value.len())?; - size += if let Some(index) = StaticTable::find(name, value) { - prefix_size(6, index) - } else if let Some(index) = StaticTable::find_name(name) { - prefix_size(4, index) + prefix_size(7, value.len()) + value.len() - } else { - prefix_size(3, name.len()) + name.len() + prefix_size(7, value.len()) + value.len() - }; + size += Self::field_parts(name, value) + .iter() + .map(Part::size) + .sum::(); } Ok(size) } @@ -233,64 +230,55 @@ impl Headers { .chain(self.fields.iter().filter(|(key, _)| !key.starts_with(':'))); for (name, value) in headers { - if let Some(index) = StaticTable::find(name, value) { - Self::encode_index(buf, index) - } else if let Some(index) = StaticTable::find_name(name) { - Self::encode_literal_value(buf, index, value) - } else { - Self::encode_literal(buf, name, value) + for part in Self::field_parts(name, value) { + part.encode(buf); } } } - fn encode_index(buf: &mut B, index: usize) { - /* - 0 1 2 3 4 5 6 7 - +---+---+---+---+---+---+---+---+ - | 1 | 1 | Index (6+) | - +---+---+-----------------------+ - */ - - encode_prefix(buf, 6, 0b11, index); + // RFC 9204 section 4.5.2: choose the representation once for measurement and output. + fn field_parts<'a>(name: &'a str, value: &'a str) -> [Part<'a>; 4] { + let empty = Part::Bytes(&[]); + if let Some(index) = StaticTable::find(name, value) { + [Part::Prefix(6, 0b11, index), empty, empty, empty] + } else if let Some(index) = StaticTable::find_name(name) { + [ + Part::Prefix(4, 0b0101, index), + empty, + Part::Prefix(7, 0, value.len()), + Part::Bytes(value.as_bytes()), + ] + } else { + [ + Part::Prefix(3, 0b00100, name.len()), + Part::Bytes(name.as_bytes()), + Part::Prefix(7, 0, value.len()), + Part::Bytes(value.as_bytes()), + ] + } } +} - fn encode_literal_value(buf: &mut B, name: usize, value: &str) { - /* - 0 1 2 3 4 5 6 7 - +---+---+---+---+---+---+---+---+ - | 0 | 1 | N | 1 |Name Index (4+)| - +---+---+---+---+---------------+ - | H | Value Length (7+) | - +---+---------------------------+ - | Value String (Length bytes) | - +-------------------------------+ - */ - - encode_prefix(buf, 4, 0b0101, name); - encode_prefix(buf, 7, 0b0, value.len()); +// Wire pieces shared by field-size measurement and encoding, without payload storage. +#[derive(Clone, Copy)] +enum Part<'a> { + Prefix(u8, u8, usize), + Bytes(&'a [u8]), +} - buf.put_slice(value.as_bytes()); +impl Part<'_> { + fn size(&self) -> usize { + match self { + Self::Prefix(bits, _, value) => prefix_size(*bits, *value), + Self::Bytes(bytes) => bytes.len(), + } } - fn encode_literal(buf: &mut B, name: &str, value: &str) { - /* - 0 1 2 3 4 5 6 7 - +---+---+---+---+---+---+---+---+ - | 0 | 0 | 1 | N | H |NameLen(3+)| - +---+---+---+---+---+-----------+ - | Name String (Length bytes) | - +---+---------------------------+ - | H | Value Length (7+) | - +---+---------------------------+ - | Value String (Length bytes) | - +-------------------------------+ - */ - - encode_prefix(buf, 3, 0b00100, name.len()); - buf.put_slice(name.as_bytes()); - - encode_prefix(buf, 7, 0b0, value.len()); - buf.put_slice(value.as_bytes()); + fn encode(&self, buf: &mut B) { + match self { + Self::Prefix(bits, flags, value) => encode_prefix(buf, *bits, *flags, *value), + Self::Bytes(bytes) => buf.put_slice(bytes), + } } } @@ -407,19 +395,27 @@ fn decode_string( let mut value = vec![0; len]; buf.copy_to_slice(&mut value); value + } else if buf.chunk().len() >= len { + let result = decode_huffman(&buf.chunk()[..len], budget); + // Consume the encoded string even on a decoding or budget error. + buf.advance(len); + result? } else { - let encoded = buf.copy_to_bytes(len); - let mut decoded = Vec::new(); - for byte in encoded.hpack_decode() { - let byte = byte.map_err(DecodeError::from)?; - budget.charge(1)?; - decoded.push(byte); - } - decoded + decode_huffman(&buf.copy_to_bytes(len), budget)? }; String::from_utf8(value).map_err(|err| DecodeError::Utf8Error(err.utf8_error()).into()) } +fn decode_huffman(encoded: &[u8], budget: &mut Budget) -> Result, FieldSectionError> { + let mut decoded = Vec::new(); + for byte in encoded.hpack_decode() { + let byte = byte.map_err(DecodeError::from)?; + budget.charge(1)?; + decoded.push(byte); + } + Ok(decoded) +} + // Based on https://github.com/hyperium/h3/blob/master/h3/src/qpack/static_.rs // I switched over to str because it's nicer in Rust... even though HTTP doesn't use utf8. struct StaticTable {} @@ -717,6 +713,20 @@ const PREDEFINED_HEADERS: [(&str, &str); 99] = [ mod tests { use super::*; + // Split every byte to exercise string copying across arbitrary Buf chunks. + struct Chunks<'a>(&'a [u8]); + impl Buf for Chunks<'_> { + fn remaining(&self) -> usize { + self.0.len() + } + fn chunk(&self) -> &[u8] { + &self.0[..self.0.len().min(1)] + } + fn advance(&mut self, cnt: usize) { + self.0 = &self.0[cnt..]; + } + } + #[test] fn measured_prefixes_match_wire_at_width_boundaries() { for bits in 1..=8 { @@ -754,19 +764,6 @@ mod tests { assert_eq!(size, wire.len()); assert_eq!(&wire[..3], &[0, 0, 0xd9], "pseudo-header is first"); - // Split every byte to exercise string copying across arbitrary Buf chunks. - struct Chunks<'a>(&'a [u8]); - impl Buf for Chunks<'_> { - fn remaining(&self) -> usize { - self.0.len() - } - fn chunk(&self) -> &[u8] { - &self.0[..self.0.len().min(1)] - } - fn advance(&mut self, cnt: usize) { - self.0 = &self.0[cnt..]; - } - } let decoded = Headers::decode(&mut Chunks(&wire)).unwrap(); assert_eq!(decoded.fields.len(), headers.fields.len()); for (name, value) in &headers.fields { @@ -775,6 +772,31 @@ mod tests { } } + #[test] + fn huffman_contiguous_and_fragmented_inputs_consume_the_same_string_on_error() { + for padding in [0xff, 0xf8] { + for budget in [1, 2] { + // Two 'a' symbols and six padding bits, followed by another string. + let wire = [0x82, 0x18, padding, 0x01, b'x']; + let mut contiguous = wire.as_slice(); + let mut fragmented = Chunks(&wire); + let result = decode_string(&mut contiguous, 8, &mut Budget(budget)); + let other = decode_string(&mut fragmented, 8, &mut Budget(budget)); + if padding == 0xff && budget == 2 { + assert_eq!(result.unwrap(), "aa"); + assert_eq!(other.unwrap(), "aa"); + } else { + assert_eq!( + result.unwrap_err().to_string(), + other.unwrap_err().to_string() + ); + } + assert_eq!(contiguous, &wire[3..]); + assert_eq!(fragmented.0, &wire[3..]); + } + } + } + #[test] fn strings_charge_before_output_and_validate_at_exact_budget() { let mut plain = Vec::new(); @@ -812,7 +834,9 @@ mod tests { let headers = Headers::decode(&mut wire.as_slice()).unwrap(); assert_eq!(headers.get(&name), Some("")); // A second occurrence still costs its own 32-byte overhead. - Headers::encode_literal(&mut wire, "x", ""); + for part in Headers::field_parts("x", "") { + part.encode(&mut wire); + } assert!(matches!( Headers::decode(&mut wire.as_slice()), Err(FieldSectionError::TooLarge) diff --git a/rs/web-transport-proto/tests/framing.rs b/rs/web-transport-proto/tests/framing.rs index 77a01e90..316b94d1 100644 --- a/rs/web-transport-proto/tests/framing.rs +++ b/rs/web-transport-proto/tests/framing.rs @@ -93,7 +93,7 @@ async fn fragmented_connect_keeps_following_capsules_unread() { http::HeaderName::from_static("x-large"), http::HeaderValue::from_str(&"a".repeat(4096)).unwrap(), ); - let response = ConnectResponse::OK.with_protocol("moq"); + let response = ConnectResponse::default().with_protocol("moq"); for fragment in [1, 7, 4096] { let mut stream = Fragmented::new(fragment); request.write(&mut stream).await.unwrap(); From adaf0ce6841de2ba3b26c6545d0ad8b92092c97a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 05:53:18 -0800 Subject: [PATCH 5/5] Keep Huffman decoding inlined after removing staging copies Preserve optimizer visibility for the shared loop; measured separately against the base and non-inlined versions. Co-Authored-By: GPT-6.1 Sol --- rs/web-transport-proto/src/qpack.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/web-transport-proto/src/qpack.rs b/rs/web-transport-proto/src/qpack.rs index 3f806870..9f7375a9 100644 --- a/rs/web-transport-proto/src/qpack.rs +++ b/rs/web-transport-proto/src/qpack.rs @@ -406,6 +406,7 @@ fn decode_string( String::from_utf8(value).map_err(|err| DecodeError::Utf8Error(err.utf8_error()).into()) } +#[inline] fn decode_huffman(encoded: &[u8], budget: &mut Budget) -> Result, FieldSectionError> { let mut decoded = Vec::new(); for byte in encoded.hpack_decode() {