Skip to content

Commit 3dc2790

Browse files
committed
Changing task::block_on to park after a single poll
This was previously discussed in async-rs#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.
1 parent 133e30e commit 3dc2790

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

Diff for: src/task/block_on.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::future::Future;
33
use std::mem::{self, ManuallyDrop};
44
use std::sync::Arc;
55
use std::task::{RawWaker, RawWakerVTable};
6-
use std::thread;
76

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

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

136-
// Yield a few times or park the current thread.
137-
if step < 3 {
138-
thread::yield_now();
139-
step += 1;
140-
} else {
141-
arc_parker.park();
142-
step = 0;
143-
}
134+
arc_parker.park();
144135
}
145136
})
146137
}

0 commit comments

Comments
 (0)