Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In ready_chunks, reserve capacity based on size_hint #2661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion futures-util/src/stream/stream/ready_chunks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::stream::Fuse;
use alloc::vec::Vec;
use core::cmp;
use core::pin::Pin;
use futures_core::stream::{FusedStream, Stream};
use futures_core::task::{Context, Poll};
Expand Down Expand Up @@ -49,7 +50,11 @@ impl<St: Stream> Stream for ReadyChunks<St> {
// the full one.
Poll::Ready(Some(item)) => {
if items.is_empty() {
items.reserve(*this.cap);
// Note we reserve capacity here, not when `items` is created,
// because stream may know the remaining size,
// but items might not be readily available.
let size_hint = this.stream.as_mut().size_hint().0.wrapping_add(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this wrapping_add instead of saturating_add?

items.reserve(cmp::min(*this.cap, size_hint));
}
items.push(item);
if items.len() >= *this.cap {
Expand Down