Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto/h1/server: avoid copy-allocation in request path parsing #3575

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/common/io/rewind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{cmp, io};
Expand Down
27 changes: 23 additions & 4 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ impl Http1Transaction for Server {
let version;
let len;
let headers_len;
let method;
let path_range;

// Both headers_indices and headers are using uninitialized memory,
// but we *never* read any of it until after httparse has assigned
Expand Down Expand Up @@ -162,10 +164,8 @@ impl Http1Transaction for Server {
if uri.len() > MAX_URI_LEN {
return Err(Parse::UriTooLong);
}
subject = RequestLine(
Method::from_bytes(req.method.unwrap().as_bytes())?,
uri.parse()?,
);
method = Method::from_bytes(req.method.unwrap().as_bytes())?;
path_range = Server::record_path_range(bytes, uri);
version = if req.version.unwrap() == 1 {
keep_alive = true;
is_http_11 = true;
Expand Down Expand Up @@ -198,6 +198,12 @@ impl Http1Transaction for Server {
};

let slice = buf.split_to(len).freeze();
let uri = {
let uri_bytes = slice.slice_ref(&slice[path_range]);
// TODO(lucab): switch to `Uri::from_shared()` once public.
http::Uri::from_maybe_shared(uri_bytes)?
};
subject = RequestLine(method, uri);

// According to https://tools.ietf.org/html/rfc7230#section-3.3.3
// 1. (irrelevant to Request)
Expand Down Expand Up @@ -945,6 +951,15 @@ impl Server {

Ok(encoder.set_last(is_last))
}

/// Helper for zero-copy parsing of request path URI.
#[inline]
fn record_path_range(bytes: &[u8], req_path: &str) -> std::ops::Range<usize> {
let bytes_ptr = bytes.as_ptr() as usize;
let start = req_path.as_ptr() as usize - bytes_ptr;
let end = start + req_path.len();
std::ops::Range { start, end }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be start..end fyi

}
}

#[cfg(feature = "server")]
Expand Down Expand Up @@ -2936,8 +2951,12 @@ mod tests {
.unwrap()
.unwrap();
::test::black_box(&msg);

// Remove all references pointing into BytesMut.
msg.head.headers.clear();
headers = Some(msg.head.headers);
std::mem::take(&mut msg.head.subject);

restart(&mut raw, len);
});

Expand Down
1 change: 0 additions & 1 deletion src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ use std::error::Error as StdError;
use std::fmt;
use std::future::Future;
use std::io;
use std::marker::Unpin;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
Expand Down