From 106f43e874360bdb9f0afd187a027b27c5b4aea1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 14 Dec 2017 20:48:31 -0800 Subject: [PATCH] better SharedBytes usage for h2 --- README.md | 12 ++++++------ src/encoding.rs | 2 +- src/h2.rs | 14 +++++++++----- src/h2writer.rs | 8 +++++--- src/helpers.rs | 4 ++++ src/httprequest.rs | 1 - 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index fbc08be60..edade3a03 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,12 @@ Each result is best of five runs. All measurements are req/sec. Name | 1 thread | 1 pipeline | 3 thread | 3 pipeline | 8 thread | 8 pipeline ---- | -------- | ---------- | -------- | ---------- | -------- | ---------- -Actix | 81400 | 710200 | 121000 | 1684000 | 106300 | 2206000 -Gotham | 61000 | 178000 | | | | -Iron | | | | | 94500 | 78000 -Rocket | | | | | 95500 | failed -Shio | 71800 | 317800 | | | | | -tokio-minihttp | 106900 | 1047000 | | | | +Actix | 81.400 | 710.200 | 121.000 | 1.684.000 | 106.300 | 2.206.000 +Gotham | 61..000 | 178.000 | | | | +Iron | | | | | 94.500 | 78.000 +Rocket | | | | | 95.500 | failed +Shio | 71.800 | 317.800 | | | | | +tokio-minihttp | 106.900 | 1.047.000 | | | | Some notes on results. Iron and Rocket got tested with 8 threads, which showed best results. Gothan and tokio-minihttp seem does not support diff --git a/src/encoding.rs b/src/encoding.rs index 7918e20d9..b632a1a3c 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -569,7 +569,7 @@ impl ContentEncoder { #[inline(always)] pub fn write_eof(&mut self) -> Result<(), io::Error> { let encoder = mem::replace( - self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::default()))); + self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::empty()))); match encoder { ContentEncoder::Br(encoder) => { diff --git a/src/h2.rs b/src/h2.rs index 4b9b6c1d4..5a3e81ac2 100644 --- a/src/h2.rs +++ b/src/h2.rs @@ -246,11 +246,15 @@ impl Entry { // Payload and Content-Encoding let (psender, payload) = Payload::new(false); - let mut req = HttpRequest::new( - parts.method, parts.uri, parts.version, parts.headers, Some(payload)); + let msg = settings.get_http_message(); + msg.get_mut().uri = parts.uri; + msg.get_mut().method = parts.method; + msg.get_mut().version = parts.version; + msg.get_mut().headers = parts.headers; + msg.get_mut().payload = Some(payload); + msg.get_mut().addr = addr; - // set remote addr - req.set_peer_addr(addr); + let mut req = HttpRequest::from_message(msg); // Payload sender let psender = PayloadType::new(req.headers(), psender); @@ -270,7 +274,7 @@ impl Entry { Entry {task: task.unwrap_or_else(|| Pipeline::error(HTTPNotFound)), payload: psender, recv: recv, - stream: H2Writer::new(resp), + stream: H2Writer::new(resp, settings.get_shared_bytes()), flags: EntryFlags::empty(), capacity: 0, } diff --git a/src/h2writer.rs b/src/h2writer.rs index e022432d7..1c19010c1 100644 --- a/src/h2writer.rs +++ b/src/h2writer.rs @@ -31,17 +31,19 @@ pub(crate) struct H2Writer { encoder: PayloadEncoder, flags: Flags, written: u64, + buffer: SharedBytes, } impl H2Writer { - pub fn new(respond: Respond) -> H2Writer { + pub fn new(respond: Respond, buf: SharedBytes) -> H2Writer { H2Writer { respond: respond, stream: None, - encoder: PayloadEncoder::empty(SharedBytes::default()), + encoder: PayloadEncoder::empty(buf.clone()), flags: Flags::empty(), written: 0, + buffer: buf, } } @@ -116,7 +118,7 @@ impl Writer for H2Writer { // prepare response self.flags.insert(Flags::STARTED); - self.encoder = PayloadEncoder::new(SharedBytes::default(), req, msg); + self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg); if let Body::Empty = *msg.body() { self.flags.insert(Flags::EOF); } diff --git a/src/helpers.rs b/src/helpers.rs index e7733a1ec..57ecb70d0 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -99,6 +99,10 @@ impl Drop for SharedBytes { impl SharedBytes { + pub fn empty() -> Self { + SharedBytes(None, None) + } + pub fn new(bytes: Rc, pool: Rc) -> SharedBytes { SharedBytes(Some(bytes), Some(pool)) } diff --git a/src/httprequest.rs b/src/httprequest.rs index b2adeb03a..b06740ebb 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -154,7 +154,6 @@ impl HttpRequest { // get mutable reference for inner message // mutable reference should not be returned as result for request's method #[inline] - #[allow(mutable_transmutes)] #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))] fn as_mut(&self) -> &mut HttpMessage { self.0.get_mut()