From 8cb06341d58ec6d3418ffdc67a089e56c89c5e40 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Thu, 6 May 2021 17:31:11 -0400 Subject: [PATCH 1/9] use futuresordered in join_all --- futures-util/src/future/join_all.rs | 105 +++++++++++++++++++++------- futures-util/src/sink/mod.rs | 2 +- 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 427e71ce04..777815cff7 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -11,6 +11,7 @@ use core::pin::Pin; use core::task::{Context, Poll}; use super::{assert_future, MaybeDone}; +use crate::stream::{Collect, FuturesOrdered, StreamExt}; fn iter_pin_mut(slice: Pin<&mut [T]>) -> impl Iterator> { // Safety: `std` _could_ make this unsound if it were to decide Pin's @@ -19,13 +20,29 @@ fn iter_pin_mut(slice: Pin<&mut [T]>) -> impl Iterator> { unsafe { slice.get_unchecked_mut() }.iter_mut().map(|t| unsafe { Pin::new_unchecked(t) }) } -/// Future for the [`join_all`] function. #[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct JoinAll -where - F: Future, -{ - elems: Pin]>>, +pin_project_lite::pin_project! { + /// Future for the [`join_all`] function. + pub struct JoinAll + where + F: Future, + { + #[pin] + kind: JoinAllKind, + } +} + +const SMALL: usize = 30; + +pin_project_lite::pin_project! { + #[project = JoinAllKindProj] + pub enum JoinAllKind + where + F: Future, + { + Small { elems: Pin]>> }, + Big { #[pin] ordered: Collect, Vec> }, + } } impl fmt::Debug for JoinAll @@ -34,7 +51,12 @@ where F::Output: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("JoinAll").field("elems", &self.elems).finish() + match self.kind { + JoinAllKind::Small { ref elems } => { + f.debug_struct("JoinAll").field("elems", elems).finish() + } + JoinAllKind::Big { ref ordered, .. } => fmt::Debug::fmt(ordered, f), + } } } @@ -50,10 +72,7 @@ where /// /// # See Also /// -/// This is purposefully a very simple API for basic use-cases. In a lot of -/// cases you will want to use the more powerful -/// [`FuturesOrdered`][crate::stream::FuturesOrdered] APIs, or, if order does -/// not matter, [`FuturesUnordered`][crate::stream::FuturesUnordered]. +/// `join_all` will switch to the more powerful [`FuturesOrdered`] if the number of futures is large for performance reasons. If the return order does not matter and you are polling many futures, you should look into [`FuturesUnordered`][crate::stream::FuturesUnordered]. /// /// Some examples for additional functionality provided by these are: /// @@ -80,8 +99,36 @@ where I: IntoIterator, I::Item: Future, { - let elems: Box<[_]> = i.into_iter().map(MaybeDone::Future).collect(); - assert_future::::Output>, _>(JoinAll { elems: elems.into() }) + let iter = i.into_iter(); + let kind = match iter.size_hint().1 { + None => big(iter), + Some(max) => { + if max <= SMALL { + small(iter) + } else { + big(iter) + } + } + }; + assert_future::::Output>, _>(JoinAll { kind }) +} + +fn small(i: I) -> JoinAllKind +where + I: Iterator, + I::Item: Future, +{ + let elems: Box<[_]> = i.map(MaybeDone::Future).collect(); + JoinAllKind::Small { elems: elems.into() } +} + +fn big(i: I) -> JoinAllKind +where + I: Iterator, + I::Item: Future, +{ + let ordered = FuturesOrdered::from_iter(i); + JoinAllKind::Big { ordered: StreamExt::collect(ordered) } } impl Future for JoinAll @@ -90,21 +137,27 @@ where { type Output = Vec; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let mut all_done = true; + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + match self.project().kind.project() { + JoinAllKindProj::Small { elems } => { + let mut all_done = true; - for elem in iter_pin_mut(self.elems.as_mut()) { - if elem.poll(cx).is_pending() { - all_done = false; - } - } + for elem in iter_pin_mut(elems.as_mut()) { + if elem.poll(cx).is_pending() { + all_done = false; + } + } - if all_done { - let mut elems = mem::replace(&mut self.elems, Box::pin([])); - let result = iter_pin_mut(elems.as_mut()).map(|e| e.take_output().unwrap()).collect(); - Poll::Ready(result) - } else { - Poll::Pending + if all_done { + let mut elems = mem::replace(elems, Box::pin([])); + let result = + iter_pin_mut(elems.as_mut()).map(|e| e.take_output().unwrap()).collect(); + Poll::Ready(result) + } else { + Poll::Pending + } + } + JoinAllKindProj::Big { ordered } => ordered.poll(cx), } } } diff --git a/futures-util/src/sink/mod.rs b/futures-util/src/sink/mod.rs index bb3c5c46f2..6ac5bd3146 100644 --- a/futures-util/src/sink/mod.rs +++ b/futures-util/src/sink/mod.rs @@ -243,7 +243,7 @@ pub trait SinkExt: Sink { /// This future will drive the stream to keep producing items until it is /// exhausted, sending each item to the sink. It will complete once both the /// stream is exhausted, the sink has received all items, and the sink has - /// been flushed. Note that the sink is **not** closed. If the stream produces + /// been flushed. Note that the sink is **not** closed. If the stream produces /// an error, that error will be returned by this future without flushing the sink. /// /// Doing `sink.send_all(stream)` is roughly equivalent to From 8c19c8e5be6b1d455bf344517dd314004cee0711 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Thu, 6 May 2021 17:44:41 -0400 Subject: [PATCH 2/9] apply clippy lints --- futures-util/src/future/join_all.rs | 35 ++++++++--------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 777815cff7..67579e1631 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -41,7 +41,7 @@ pin_project_lite::pin_project! { F: Future, { Small { elems: Pin]>> }, - Big { #[pin] ordered: Collect, Vec> }, + Big { #[pin] fut: Collect, Vec> }, } } @@ -55,7 +55,7 @@ where JoinAllKind::Small { ref elems } => { f.debug_struct("JoinAll").field("elems", elems).finish() } - JoinAllKind::Big { ref ordered, .. } => fmt::Debug::fmt(ordered, f), + JoinAllKind::Big { ref fut, .. } => fmt::Debug::fmt(fut, f), } } } @@ -94,43 +94,26 @@ where /// assert_eq!(join_all(futures).await, [1, 2, 3]); /// # }); /// ``` -pub fn join_all(i: I) -> JoinAll +pub fn join_all(iter: I) -> JoinAll where I: IntoIterator, I::Item: Future, { - let iter = i.into_iter(); + let iter = iter.into_iter(); let kind = match iter.size_hint().1 { - None => big(iter), + None => JoinAllKind::Big { fut: iter.collect::>().collect() }, Some(max) => { if max <= SMALL { - small(iter) + let elems = iter.map(MaybeDone::Future).collect::>().into(); + JoinAllKind::Small { elems } } else { - big(iter) + JoinAllKind::Big { fut: iter.collect::>().collect() } } } }; assert_future::::Output>, _>(JoinAll { kind }) } -fn small(i: I) -> JoinAllKind -where - I: Iterator, - I::Item: Future, -{ - let elems: Box<[_]> = i.map(MaybeDone::Future).collect(); - JoinAllKind::Small { elems: elems.into() } -} - -fn big(i: I) -> JoinAllKind -where - I: Iterator, - I::Item: Future, -{ - let ordered = FuturesOrdered::from_iter(i); - JoinAllKind::Big { ordered: StreamExt::collect(ordered) } -} - impl Future for JoinAll where F: Future, @@ -157,7 +140,7 @@ where Poll::Pending } } - JoinAllKindProj::Big { ordered } => ordered.poll(cx), + JoinAllKindProj::Big { fut } => fut.poll(cx), } } } From 8f426c1a9a8dd0f8817c46b5b20cf03f3b9621d3 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Sat, 22 May 2021 23:22:12 -0400 Subject: [PATCH 3/9] feature gate use of futures unordered in join_all --- futures-util/src/future/join_all.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 67579e1631..df4219b2fc 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -41,6 +41,7 @@ pin_project_lite::pin_project! { F: Future, { Small { elems: Pin]>> }, + #[cfg(not(futures_no_atomic_cas))] Big { #[pin] fut: Collect, Vec> }, } } @@ -55,6 +56,7 @@ where JoinAllKind::Small { ref elems } => { f.debug_struct("JoinAll").field("elems", elems).finish() } + #[cfg(not(futures_no_atomic_cas))] JoinAllKind::Big { ref fut, .. } => fmt::Debug::fmt(fut, f), } } @@ -101,19 +103,35 @@ where { let iter = iter.into_iter(); let kind = match iter.size_hint().1 { - None => JoinAllKind::Big { fut: iter.collect::>().collect() }, + None => join_all_big(iter), Some(max) => { if max <= SMALL { let elems = iter.map(MaybeDone::Future).collect::>().into(); JoinAllKind::Small { elems } } else { - JoinAllKind::Big { fut: iter.collect::>().collect() } + join_all_big(iter) } } }; assert_future::::Output>, _>(JoinAll { kind }) } +fn join_all_big(iter: I) -> JoinAllKind +where + I: Iterator, + I::Item: Future, +{ + #[cfg(not(futures_no_atomic_cas))] + { + return JoinAllKind::Big { fut: iter.collect::>().collect() }; + } + #[cfg(futures_no_atomic_cas)] + { + let elems = iter.map(MaybeDone::Future).collect::>().into(); + JoinAllKind::Small { elems } + } +} + impl Future for JoinAll where F: Future, @@ -140,6 +158,7 @@ where Poll::Pending } } + #[cfg(not(futures_no_atomic_cas))] JoinAllKindProj::Big { fut } => fut.poll(cx), } } From 161cbefe069c37d1e61fc94a5e61e90820bf58a2 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Sat, 22 May 2021 23:54:42 -0400 Subject: [PATCH 4/9] clippy lints, fix imports --- futures-util/src/future/join_all.rs | 48 +++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index df4219b2fc..a9e284ef71 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -11,7 +11,10 @@ use core::pin::Pin; use core::task::{Context, Poll}; use super::{assert_future, MaybeDone}; -use crate::stream::{Collect, FuturesOrdered, StreamExt}; +use crate::stream::Collect; + +#[cfg(not(futures_no_atomic_cas))] +use crate::stream::{FuturesOrdered, StreamExt}; fn iter_pin_mut(slice: Pin<&mut [T]>) -> impl Iterator> { // Safety: `std` _could_ make this unsound if it were to decide Pin's @@ -101,34 +104,27 @@ where I: IntoIterator, I::Item: Future, { - let iter = iter.into_iter(); - let kind = match iter.size_hint().1 { - None => join_all_big(iter), - Some(max) => { - if max <= SMALL { - let elems = iter.map(MaybeDone::Future).collect::>().into(); - JoinAllKind::Small { elems } - } else { - join_all_big(iter) - } - } - }; - assert_future::::Output>, _>(JoinAll { kind }) -} - -fn join_all_big(iter: I) -> JoinAllKind -where - I: Iterator, - I::Item: Future, -{ - #[cfg(not(futures_no_atomic_cas))] - { - return JoinAllKind::Big { fut: iter.collect::>().collect() }; - } #[cfg(futures_no_atomic_cas)] { let elems = iter.map(MaybeDone::Future).collect::>().into(); - JoinAllKind::Small { elems } + let kind = JoinAllKind::Small { elems }; + assert_future::::Output>, _>(JoinAll { kind }) + } + #[cfg(not(futures_no_atomic_cas))] + { + let iter = iter.into_iter(); + let kind = match iter.size_hint().1 { + None => JoinAllKind::Big { fut: iter.collect::>().collect() }, + Some(max) => { + if max <= SMALL { + let elems = iter.map(MaybeDone::Future).collect::>().into(); + JoinAllKind::Small { elems } + } else { + JoinAllKind::Big { fut: iter.collect::>().collect() } + } + } + }; + assert_future::::Output>, _>(JoinAll { kind }) } } From 75db694fffa7859cbba6751f7b53e813a1b4cc24 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Wed, 28 Jul 2021 18:59:34 -0400 Subject: [PATCH 5/9] remove use of pin-project from future::JoinAll --- futures-util/src/future/join_all.rs | 44 ++++++++++++++--------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index a9e284ef71..957e199bf5 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -24,29 +24,27 @@ fn iter_pin_mut(slice: Pin<&mut [T]>) -> impl Iterator> { } #[must_use = "futures do nothing unless you `.await` or poll them"] -pin_project_lite::pin_project! { - /// Future for the [`join_all`] function. - pub struct JoinAll - where - F: Future, - { - #[pin] - kind: JoinAllKind, - } +/// Future for the [`join_all`] function. +pub struct JoinAll +where + F: Future, +{ + kind: JoinAllKind, } const SMALL: usize = 30; -pin_project_lite::pin_project! { - #[project = JoinAllKindProj] - pub enum JoinAllKind - where - F: Future, - { - Small { elems: Pin]>> }, - #[cfg(not(futures_no_atomic_cas))] - Big { #[pin] fut: Collect, Vec> }, - } +pub enum JoinAllKind +where + F: Future, +{ + Small { + elems: Pin]>>, + }, + #[cfg(not(futures_no_atomic_cas))] + Big { + fut: Collect, Vec>, + }, } impl fmt::Debug for JoinAll @@ -134,9 +132,9 @@ where { type Output = Vec; - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.project().kind.project() { - JoinAllKindProj::Small { elems } => { + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + match &mut self.kind { + JoinAllKind::Small { elems } => { let mut all_done = true; for elem in iter_pin_mut(elems.as_mut()) { @@ -155,7 +153,7 @@ where } } #[cfg(not(futures_no_atomic_cas))] - JoinAllKindProj::Big { fut } => fut.poll(cx), + JoinAllKind::Big { fut } => Pin::new(fut).poll(cx), } } } From 954c220164dfe4324257f39c71ffa6b2612673cc Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Wed, 28 Jul 2021 19:05:09 -0400 Subject: [PATCH 6/9] update JoinAll docs --- futures-util/src/future/join_all.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 957e199bf5..e54c5d838a 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -75,7 +75,9 @@ where /// /// # See Also /// -/// `join_all` will switch to the more powerful [`FuturesOrdered`] if the number of futures is large for performance reasons. If the return order does not matter and you are polling many futures, you should look into [`FuturesUnordered`][crate::stream::FuturesUnordered]. +/// `join_all` will switch to the more powerful [`FuturesOrdered`] for performance +/// reasons if the number of futures is large. You may want to look into using it or +/// it's counterpart [`FuturesUnordered`][crate::stream::FuturesUnordered] directly. /// /// Some examples for additional functionality provided by these are: /// From 6420af881c8549aab90e04e076dcfad5d51b149c Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Wed, 28 Jul 2021 19:03:45 -0400 Subject: [PATCH 7/9] make `JoinAllKind` crate public --- futures-util/src/future/join_all.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index e54c5d838a..031fdc6f7e 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -34,7 +34,7 @@ where const SMALL: usize = 30; -pub enum JoinAllKind +pub(crate) enum JoinAllKind where F: Future, { From 7e323154f0fed002a82846723f542a19bcb8523d Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Wed, 28 Jul 2021 19:20:08 -0400 Subject: [PATCH 8/9] fix compliation on futures_no_atomic_cas targets --- futures-util/src/future/join_all.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 031fdc6f7e..b02a78356a 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -11,10 +11,9 @@ use core::pin::Pin; use core::task::{Context, Poll}; use super::{assert_future, MaybeDone}; -use crate::stream::Collect; #[cfg(not(futures_no_atomic_cas))] -use crate::stream::{FuturesOrdered, StreamExt}; +use crate::stream::{Collect, FuturesOrdered, StreamExt}; fn iter_pin_mut(slice: Pin<&mut [T]>) -> impl Iterator> { // Safety: `std` _could_ make this unsound if it were to decide Pin's @@ -106,7 +105,7 @@ where { #[cfg(futures_no_atomic_cas)] { - let elems = iter.map(MaybeDone::Future).collect::>().into(); + let elems = iter.into_iter().map(MaybeDone::Future).collect::>().into(); let kind = JoinAllKind::Small { elems }; assert_future::::Output>, _>(JoinAll { kind }) } From 07e0c2b9e3c5567253ff1073a98cb5ab5c9085ad Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Wed, 28 Jul 2021 19:47:05 -0400 Subject: [PATCH 9/9] `SMALL` constant is unused when `JoinAllKind::Big` is disabled --- futures-util/src/future/join_all.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index b02a78356a..2e52ac17f4 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -31,6 +31,7 @@ where kind: JoinAllKind, } +#[cfg(not(futures_no_atomic_cas))] const SMALL: usize = 30; pub(crate) enum JoinAllKind