From d8b355a0b87a28d948921921c603dda2995726ce Mon Sep 17 00:00:00 2001 From: Stiopa Koltsov Date: Sat, 22 Oct 2022 05:12:39 +0100 Subject: [PATCH] Do not allocate capacity in ready_chunks `read_chunks` practically often returns items when not all capacity filled. So always allocating full capacity results in excessive allocation. This is especially an issue when `capacity` parameter passed is large (to be able to handle streams efficiently under high load). --- futures-util/src/stream/stream/ready_chunks.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/futures-util/src/stream/stream/ready_chunks.rs b/futures-util/src/stream/stream/ready_chunks.rs index ab8637f27..a077a7173 100644 --- a/futures-util/src/stream/stream/ready_chunks.rs +++ b/futures-util/src/stream/stream/ready_chunks.rs @@ -45,12 +45,8 @@ impl Stream for ReadyChunks { } // Push the ready item into the buffer and check whether it is full. - // If so, replace our buffer with a new and empty one and return - // the full one. + // If so, return it. Poll::Ready(Some(item)) => { - if items.is_empty() { - items.reserve(*this.cap); - } items.push(item); if items.len() >= *this.cap { return Poll::Ready(Some(items));