From 569287b15b6482a39f2c816f103574c3b35351f8 Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Tue, 4 Oct 2022 15:48:50 +0200 Subject: perf(ext/fetch): consume body using ops (#16038) This commit adds a fast path to `Request` and `Response` that make consuming request bodies much faster when using `Body#text`, `Body#arrayBuffer`, and `Body#blob`, if the body is a FastStream. Because the response bodies for `fetch` are FastStream, this speeds up consuming `fetch` response bodies significantly. --- ext/http/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'ext/http/lib.rs') diff --git a/ext/http/lib.rs b/ext/http/lib.rs index bffe3c3d5..a8c2810bc 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -39,6 +39,8 @@ use flate2::write::GzEncoder; use flate2::Compression; use fly_accept_encoding::Encoding; use hyper::body::Bytes; +use hyper::body::HttpBody; +use hyper::body::SizeHint; use hyper::header::HeaderName; use hyper::header::HeaderValue; use hyper::server::conn::Http; @@ -309,6 +311,7 @@ pub struct HttpStreamResource { wr: AsyncRefCell, accept_encoding: Encoding, cancel_handle: CancelHandle, + size: SizeHint, } impl HttpStreamResource { @@ -318,11 +321,13 @@ impl HttpStreamResource { response_tx: oneshot::Sender>, accept_encoding: Encoding, ) -> Self { + let size = request.body().size_hint(); Self { conn: conn.clone(), rd: HttpRequestReader::Headers(request).into(), wr: HttpResponseWriter::Headers(response_tx).into(), accept_encoding, + size, cancel_handle: CancelHandle::new(), } } @@ -388,6 +393,10 @@ impl Resource for HttpStreamResource { fn close(self: Rc) { self.cancel_handle.cancel(); } + + fn size_hint(&self) -> (u64, Option) { + (self.size.lower(), self.size.upper()) + } } /// The read half of an HTTP stream. -- cgit v1.2.3