This commit is contained in:
Mateusz Gruszczyński committed 2026-10-12 11:22:42 +02:00
1 parent 68ada6680d
commit 180a0e4877
56 files changed
+9866 -205

No files matched your search

+55 -4
View File
@@ -63,6 +63,10 @@ struct Candidate {
plan: Plan,
headers: HeaderMap,
data: Vec<u8>,
// A transformed response with one uniquely owned Bytes frame can share its
// allocation with the downstream response, avoiding an extra Vec copy.
shared: Option<Bytes>,
shared_allocated: usize,
fill_gauge: FillGauge,
settings: Settings,
skip_compression: bool,
@@ -77,7 +81,7 @@ impl Candidate {
if self
.plan
.expected_length
.is_some_and(|length| self.data.len() != length)
.is_some_and(|length| self.shared.as_ref().map_or(self.data.len(), Bytes::len) != length)
{
let reason = if self.transformed {
"transform_length_mismatch"
@@ -112,8 +116,12 @@ impl Candidate {
// Keep the actual backing capacity as the HOT charge. Vec::reserve_exact
// avoids geometric multi-MiB overshoot while Bytes::from(Vec) transfers
// the allocation without copying.
let body_allocation = self.data.capacity().max(self.data.len());
let body = Bytes::from(std::mem::take(&mut self.data));
let (body, body_allocation) = if let Some(body) = self.shared.take() {
(body, self.shared_allocated)
} else {
let allocated = self.data.capacity().max(self.data.len());
(Bytes::from(std::mem::take(&mut self.data)), allocated)
};
let scope = self.ticket.scope.clone();
let scheme = self.ticket.scheme.clone();
let authority = self.ticket.authority.clone();
@@ -192,8 +200,51 @@ impl HttpBody for FillBody {
let this = self.get_mut();
match Pin::new(&mut this.inner).poll_frame(cx) {
Poll::Ready(Some(Ok(frame))) => {
let mut frame = frame;
// Only the transformed single-frame case is eligible. Taking
// unique ownership lets Bytes reveal its true allocation
// capacity; we must never undercharge an arbitrary shared
// Bytes slice that could retain a much larger parent buffer.
if this.inner.is_end_stream() {
if let Some(candidate) = &mut this.candidate {
let single_frame = candidate.transformed
&& candidate.data.is_empty()
&& candidate.shared.is_none()
&& frame.data_ref().is_some_and(|data| {
candidate.plan.expected_length == Some(data.len())
&& data.len() <= candidate.settings.max_object_bytes
});
if single_frame {
match frame.into_data() {
Ok(bytes) => {
match bytes.try_into_mut() {
Ok(mutable) => {
let allocation = mutable.capacity();
let bytes = mutable.freeze();
// A very oversized backing Vec is cheaper to
// copy into a smaller HOT allocation instead.
let acceptable_spare = bytes.len().saturating_div(8).min(64 * 1024);
if allocation <= bytes.len().saturating_add(acceptable_spare)
&& allocation <= candidate.settings.max_object_bytes
&& candidate.fill_gauge.grow(allocation, &candidate.settings)
{
candidate.shared = Some(bytes.clone());
candidate.shared_allocated = allocation;
}
frame = Frame::data(bytes);
}
Err(bytes) => frame = Frame::data(bytes),
}
}
Err(original) => frame = original,
}
}
}
}
if let Some(candidate) = &mut this.candidate {
if let Some(data) = frame.data_ref() {
if candidate.shared.is_some() {
// The cache now owns the very same backing allocation.
} else if let Some(data) = frame.data_ref() {
let needed = candidate.data.len().saturating_add(data.len());
if needed <= candidate.settings.max_object_bytes {
let before = candidate.data.capacity();