perf(h1): remove unused error branches from writing body

This commit is contained in:
Sean McArthur
2018-05-07 11:19:19 -07:00
parent 325b7e519a
commit 0d104deced
2 changed files with 7 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ use std::io::{self};
use std::marker::PhantomData; use std::marker::PhantomData;
use bytes::{Buf, Bytes}; use bytes::{Buf, Bytes};
use futures::{Async, AsyncSink, Poll, StartSend}; use futures::{Async, Poll};
use http::{Method, Version}; use http::{Method, Version};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
@@ -451,25 +451,14 @@ where I: AsyncRead + AsyncWrite,
} }
} }
pub fn write_body(&mut self, chunk: Option<B>) -> StartSend<Option<B>, io::Error> { pub fn write_body(&mut self, chunk: Option<B>) {
debug_assert!(self.can_write_body()); debug_assert!(self.can_write_body() && self.can_buffer_body());
if !self.can_buffer_body() {
if let Async::NotReady = self.flush()? {
// if chunk is Some(&[]), aka empty, whatever, just skip it
if chunk.as_ref().map(|c| c.remaining() == 0).unwrap_or(false) {
return Ok(AsyncSink::Ready);
} else {
return Ok(AsyncSink::NotReady(chunk));
}
}
}
let state = match self.state.writing { let state = match self.state.writing {
Writing::Body(ref mut encoder) => { Writing::Body(ref mut encoder) => {
if let Some(chunk) = chunk { if let Some(chunk) = chunk {
if chunk.remaining() == 0 { if chunk.remaining() == 0 {
return Ok(AsyncSink::Ready); return;
} }
let encoded = encoder.encode(chunk); let encoded = encoder.encode(chunk);
@@ -482,7 +471,7 @@ where I: AsyncRead + AsyncWrite,
Writing::KeepAlive Writing::KeepAlive
} }
} else { } else {
return Ok(AsyncSink::Ready); return;
} }
} else { } else {
// end of stream, that means we should try to eof // end of stream, that means we should try to eof
@@ -505,7 +494,6 @@ where I: AsyncRead + AsyncWrite,
}; };
self.state.writing = state; self.state.writing = state;
Ok(AsyncSink::Ready)
} }
// When we get a parse error, depending on what side we are, we might be able // When we get a parse error, depending on what side we are, we might be able

View File

@@ -242,7 +242,7 @@ where
}, },
Async::Ready(None) => { Async::Ready(None) => {
if self.conn.can_write_body() { if self.conn.can_write_body() {
self.conn.write_body(None).map_err(::Error::new_body_write)?; self.conn.write_body(None);
} }
continue; continue;
}, },
@@ -253,7 +253,7 @@ where
}; };
if self.conn.can_write_body() { if self.conn.can_write_body() {
self.conn.write_body(Some(chunk)).map_err(::Error::new_body_write)?; self.conn.write_body(Some(chunk));
// This allows when chunk is `None`, or `Some([])`. // This allows when chunk is `None`, or `Some([])`.
} else if chunk.remaining() == 0 { } else if chunk.remaining() == 0 {
// ok // ok