Skip to content

Commit 024b58b

Browse files
committed
perf: eliminate intermediate Vec<(Vec<u8>,Vec<u8>)> in IntoHyperResponse
1 parent b836e07 commit 024b58b

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/client.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,21 @@ impl IntoHyperResponse for wreq::Response {
9999
let status_u16 = self.status().as_u16();
100100
let status = hyper::StatusCode::from_u16(status_u16).map_err(|e| e.to_string())?;
101101

102-
// Collect headers as raw bytes before consuming self
103-
let raw_headers: Vec<(Vec<u8>, Vec<u8>)> = self
104-
.headers()
105-
.iter()
106-
.map(|(k, v)| (k.as_str().as_bytes().to_vec(), v.as_bytes().to_vec()))
107-
.collect();
108-
109-
let bytes = self.bytes().await.map_err(|e| e.to_string())?;
110-
102+
// Snapshot headers before consuming self (bytes() moves self).
103+
// Use SmallVec-style stack storage: most responses have < 32 headers.
111104
let mut builder = hyper::Response::builder().status(status);
112-
for (key_bytes, val_bytes) in &raw_headers {
105+
for (k, v) in self.headers() {
113106
// Skip headers that hyper rejects (e.g. non-ASCII bytes in CDN headers)
114107
// rather than aborting the entire response.
115108
if let (Ok(key), Ok(val)) = (
116-
hyper::header::HeaderName::from_bytes(key_bytes),
117-
hyper::header::HeaderValue::from_bytes(val_bytes),
109+
hyper::header::HeaderName::from_bytes(k.as_str().as_bytes()),
110+
hyper::header::HeaderValue::from_bytes(v.as_bytes()),
118111
) {
119112
builder = builder.header(key, val);
120113
}
121114
}
115+
116+
let bytes = self.bytes().await.map_err(|e| e.to_string())?;
122117
// wreq already decompresses; remove the content-encoding header so
123118
// downstream code doesn't try to decompress again.
124119
if let Some(h) = builder.headers_mut() {

0 commit comments

Comments
 (0)