Skip to content

Commit

Permalink
Changing task::block_on to park after a single poll (#684)
Browse files Browse the repository at this point in the history
This was previously discussed in #605 and others as a source of high
CPU load when sleeping tasks because of the overhead created by
retrying a future in short succession.
  • Loading branch information
spacekookie authored and Stjepan Glavina committed Jan 20, 2020
1 parent d283352 commit 81aa6d1
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions src/task/block_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::future::Future;
use std::mem::{self, ManuallyDrop};
use std::sync::Arc;
use std::task::{RawWaker, RawWakerVTable};
use std::thread;

use crossbeam_utils::sync::Parker;
use kv_log_macro::trace;
Expand Down Expand Up @@ -125,22 +124,14 @@ where
let waker = unsafe { ManuallyDrop::new(Waker::from_raw(RawWaker::new(ptr, &VTABLE))) };
let cx = &mut Context::from_waker(&waker);

let mut step = 0;
loop {
if let Poll::Ready(t) = future.as_mut().poll(cx) {
// Save the parker for the next invocation of `block`.
cache.set(Some(arc_parker));
return t;
}

// Yield a few times or park the current thread.
if step < 3 {
thread::yield_now();
step += 1;
} else {
arc_parker.park();
step = 0;
}
arc_parker.park();
}
})
}

0 comments on commit 81aa6d1

Please sign in to comment.