Changing task::block_on to park after a single poll (#684)

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.
pull/688/head
Katharina Fey 5 years ago committed by Stjepan Glavina
parent d283352a9a
commit 81aa6d152a

@ -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;
@ -125,7 +124,6 @@ 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`.
@ -133,14 +131,7 @@ where
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;
}
}
})
}

Loading…
Cancel
Save