[web] [WIP] Optimize IO slightly
This commit is contained in:
parent
5d47635ce8
commit
e55442c47f
|
|
@ -3,6 +3,7 @@ pub mod contrast;
|
||||||
pub mod statics;
|
pub mod statics;
|
||||||
pub mod configuration;
|
pub mod configuration;
|
||||||
|
|
||||||
|
use std::io::IoSlice;
|
||||||
use crate::statics::StaticAsset;
|
use crate::statics::StaticAsset;
|
||||||
use smol::io::AsyncWriteExt;
|
use smol::io::AsyncWriteExt;
|
||||||
use smol::stream::StreamExt;
|
use smol::stream::StreamExt;
|
||||||
|
|
@ -151,10 +152,10 @@ async fn handle_request(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = stream.write_all(
|
if let Err(e) = stream.write_vectored(
|
||||||
route_request(&req)
|
route_request(&req)
|
||||||
.generate_response(settings)
|
.generate_response(settings)
|
||||||
.into_bytes()
|
.into_io_slices()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
).await {
|
).await {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
|
|
@ -484,32 +485,32 @@ pub struct Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
fn into_bytes(self) -> Vec<u8> {
|
fn into_io_slices(&self) -> Vec<IoSlice<'_>> {
|
||||||
let mut output = Vec::with_capacity(
|
let mut output = Vec::with_capacity(
|
||||||
// Is computing the exact length of the output necessary? No.
|
// Is computing the exact length of the output necessary? No.
|
||||||
// Do we gain any even vaguely measurable performance gain by preventing reallocation? No.
|
// Do we gain any even vaguely measurable performance gain by preventing reallocation? No.
|
||||||
// Am I doing it anyway? Yes.
|
// Am I doing it anyway? Yes.
|
||||||
// It is late and I am tired and you are nothing to me you can't tell me what to do
|
// It is late and I am tired and you are nothing to me you can't tell me what to do
|
||||||
14 + // HTTP/1.1 200\r\n
|
5 + 4 * self.headers.len()
|
||||||
self.headers
|
|
||||||
.iter()
|
|
||||||
.map(|(h1, h2)| h1.len() + h2.len() + 4)
|
|
||||||
.sum::<usize>() +
|
|
||||||
2 + // \r\n
|
|
||||||
self.body.len()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
output.extend_from_slice(b"HTTP/1.1 ");
|
output.extend_from_slice(&[
|
||||||
output.extend_from_slice(self.status.to_string().as_bytes());
|
IoSlice::new(b"HTTP/1.1 "),
|
||||||
output.extend_from_slice(b"\r\n");
|
IoSlice::new(self.status.to_string().as_bytes()),
|
||||||
|
IoSlice::new(b"\r\n"),
|
||||||
|
]);
|
||||||
for (h1, h2) in self.headers.iter() {
|
for (h1, h2) in self.headers.iter() {
|
||||||
output.extend_from_slice(h1);
|
output.extend_from_slice(&[
|
||||||
output.extend_from_slice(b": ");
|
IoSlice::new(h1),
|
||||||
output.extend_from_slice(h2);
|
IoSlice::new(b": "),
|
||||||
output.extend_from_slice(b"\r\n");
|
IoSlice::new(h2),
|
||||||
|
IoSlice::new(b"\r\n"),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
output.extend_from_slice(b"\r\n");
|
output.extend_from_slice(&[
|
||||||
output.extend_from_slice(&*self.body);
|
IoSlice::new(b"\r\n"),
|
||||||
|
IoSlice::new(&*self.body),
|
||||||
|
]);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue