@@ -30,22 +30,6 @@ use self::task::Task;
30
30
mod ready_to_run_queue;
31
31
use self :: ready_to_run_queue:: { ReadyToRunQueue , Dequeue } ;
32
32
33
- /// Constant used for a `FuturesUnordered` to determine how many times it is
34
- /// allowed to poll underlying futures without yielding.
35
- ///
36
- /// A single call to `poll_next` may potentially do a lot of work before
37
- /// yielding. This happens in particular if the underlying futures are awoken
38
- /// frequently but continue to return `Pending`. This is problematic if other
39
- /// tasks are waiting on the executor, since they do not get to run. This value
40
- /// caps the number of calls to `poll` on underlying futures a single call to
41
- /// `poll_next` is allowed to make.
42
- ///
43
- /// The value itself is chosen somewhat arbitrarily. It needs to be high enough
44
- /// that amortize wakeup and scheduling costs, but low enough that we do not
45
- /// starve other tasks for long.
46
- ///
47
- /// See also https://github.com/rust-lang/futures-rs/issues/2047.
48
- const YIELD_EVERY : usize = 32 ;
49
33
50
34
/// A set of futures which may complete in any order.
51
35
///
@@ -414,6 +398,22 @@ impl<Fut: Future> Stream for FuturesUnordered<Fut> {
414
398
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > )
415
399
-> Poll < Option < Self :: Item > >
416
400
{
401
+ // Variable to determine how many times it is allowed to poll underlying
402
+ // futures without yielding.
403
+ //
404
+ // A single call to `poll_next` may potentially do a lot of work before
405
+ // yielding. This happens in particular if the underlying futures are awoken
406
+ // frequently but continue to return `Pending`. This is problematic if other
407
+ // tasks are waiting on the executor, since they do not get to run. This value
408
+ // caps the number of calls to `poll` on underlying futures a single call to
409
+ // `poll_next` is allowed to make.
410
+ //
411
+ // The value is the length of FuturesUnordered. This ensures that each
412
+ // future is polled only once at most per iteration.
413
+ //
414
+ // See also https://github.com/rust-lang/futures-rs/issues/2047.
415
+ let yield_every = self . len ( ) ;
416
+
417
417
// Keep track of how many child futures we have polled,
418
418
// in case we want to forcibly yield.
419
419
let mut polled = 0 ;
@@ -548,7 +548,7 @@ impl<Fut: Future> Stream for FuturesUnordered<Fut> {
548
548
let task = bomb. task . take ( ) . unwrap ( ) ;
549
549
bomb. queue . link ( task) ;
550
550
551
- if polled == YIELD_EVERY {
551
+ if polled == yield_every {
552
552
// We have polled a large number of futures in a row without yielding.
553
553
// To ensure we do not starve other tasks waiting on the executor,
554
554
// we yield here, but immediately wake ourselves up to continue.
0 commit comments