Skip to content

Commit b6afb23

Browse files
committed
tweak heuristic
1 parent dd86ee1 commit b6afb23

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

futures-util/src/stream/futures_unordered/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ impl<Fut: Future> Stream for FuturesUnordered<Fut> {
390390
// Keep track of how many child futures we have polled,
391391
// in case we want to forcibly yield.
392392
let mut polled = 0;
393+
let mut yielded = 0;
393394

394395
// Ensure `parent` is correctly set.
395396
self.ready_to_run_queue.waker.register(cx.waker());
@@ -519,15 +520,15 @@ impl<Fut: Future> Stream for FuturesUnordered<Fut> {
519520
let task = bomb.task.take().unwrap();
520521
// If the future was awoken during polling, we assume
521522
// the future wanted to explicitly yield.
522-
let yielded = task.woken.load(Relaxed);
523+
yielded += task.woken.load(Relaxed) as usize;
523524
bomb.queue.link(task);
524525

525526
// If a future yields, we respect it and yield here.
526527
// If all futures have been polled, we also yield here to
527528
// avoid starving other tasks waiting on the executor.
528529
// (polling the same future twice per iteration may cause
529530
// the problem: https://github.com/rust-lang/futures-rs/pull/2333)
530-
if yielded || polled == len {
531+
if yielded >= 2 || polled == len {
531532
cx.waker().wake_by_ref();
532533
return Poll::Pending;
533534
}

futures-util/src/stream/futures_unordered/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(super) struct Task<Fut> {
3232
// Whether or not this task is currently in the ready to run queue
3333
pub(super) queued: AtomicBool,
3434

35-
// Whether the future waken before it finishes polling
35+
// Whether the future was awoken during polling
3636
// It is possible for this flag to be set to true after the polling,
3737
// but it will be ignored.
3838
pub(super) woken: AtomicBool,

futures/tests/stream_futures_unordered.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ fn futures_not_moved_after_poll() {
268268
let fut = future::ready(()).pending_once().assert_unmoved();
269269
let mut stream = vec![fut; 3].into_iter().collect::<FuturesUnordered<_>>();
270270
assert_stream_pending!(stream);
271-
assert_stream_pending!(stream);
272-
assert_stream_pending!(stream);
273271
assert_stream_next!(stream, ());
274272
assert_stream_next!(stream, ());
275273
assert_stream_next!(stream, ());

0 commit comments

Comments
 (0)