From 57f7e07c13a1a692602022af4fc32c6ac352bb72 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 22 Apr 2022 16:19:08 +0530 Subject: Reland "perf(http): optimize ReadableStreams backed by a resource" (#14346) --- core/examples/http_bench_json_ops.rs | 16 ++++++++++++---- core/resources.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index 2068c3b85..7c895f326 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -83,13 +83,18 @@ struct TcpStream { } impl TcpStream { - async fn read(self: Rc, mut buf: ZeroCopyBuf) -> Result { + async fn read( + self: Rc, + mut buf: ZeroCopyBuf, + ) -> Result<(usize, ZeroCopyBuf), Error> { let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await; let cancel = RcRef::map(self, |r| &r.cancel); - rd.read(&mut buf) + let nread = rd + .read(&mut buf) .try_or_cancel(cancel) .await - .map_err(Error::from) + .map_err(Error::from)?; + Ok((nread, buf)) } async fn write(self: Rc, buf: ZeroCopyBuf) -> Result { @@ -99,7 +104,10 @@ impl TcpStream { } impl Resource for TcpStream { - fn read(self: Rc, buf: ZeroCopyBuf) -> AsyncResult { + fn read_return( + self: Rc, + buf: ZeroCopyBuf, + ) -> AsyncResult<(usize, ZeroCopyBuf)> { Box::pin(self.read(buf)) } diff --git a/core/resources.rs b/core/resources.rs index 9a1447392..ae4ef7394 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -36,7 +36,17 @@ pub trait Resource: Any + 'static { } /// Resources may implement `read()` to be a readable stream - fn read(self: Rc, _buf: ZeroCopyBuf) -> AsyncResult { + fn read(self: Rc, buf: ZeroCopyBuf) -> AsyncResult { + Box::pin(async move { + let (nread, _) = self.read_return(buf).await?; + Ok(nread) + }) + } + + fn read_return( + self: Rc, + _buf: ZeroCopyBuf, + ) -> AsyncResult<(usize, ZeroCopyBuf)> { Box::pin(futures::future::err(not_supported())) } -- cgit v1.2.3