diff options
author | Carl Lerche <me@carllerche.com> | 2020-11-11 09:28:21 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-11 09:28:21 -0800 |
commit | ce891a4df17e632f7557dd0cd1f1e8da89bd6ae4 (patch) | |
tree | fa5478c0b3bacacfc65bfbadbe1cdb92234d5b5f /tokio/src/io/driver/scheduled_io.rs | |
parent | d869e16990c5fc2cbda48b036708efa4b450e807 (diff) |
io: driver internal cleanup (#3124)
* Removes duplicated code by moving it to `Registration`.
* impl `Deref` for `PollEvented` to avoid `get_ref()`.
* Avoid extra waker clones in I/O driver.
* Add `Interest` wrapper around `mio::Interest`.
Diffstat (limited to 'tokio/src/io/driver/scheduled_io.rs')
-rw-r--r-- | tokio/src/io/driver/scheduled_io.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/tokio/src/io/driver/scheduled_io.rs b/tokio/src/io/driver/scheduled_io.rs index 3aefb376..ed3adc39 100644 --- a/tokio/src/io/driver/scheduled_io.rs +++ b/tokio/src/io/driver/scheduled_io.rs @@ -49,6 +49,8 @@ struct Waiters { } cfg_io_readiness! { + use crate::io::Interest; + #[derive(Debug)] struct Waiter { pointers: linked_list::Pointers<Waiter>, @@ -57,7 +59,7 @@ cfg_io_readiness! { waker: Option<Waker>, /// The interest this waiter is waiting on - interest: mio::Interest, + interest: Interest, is_ready: bool, @@ -283,7 +285,7 @@ impl ScheduledIo { /// These are to support `AsyncRead` and `AsyncWrite` polling methods, /// which cannot use the `async fn` version. This uses reserved reader /// and writer slots. - pub(in crate::io) fn poll_readiness( + pub(super) fn poll_readiness( &self, cx: &mut Context<'_>, direction: Direction, @@ -299,7 +301,19 @@ impl ScheduledIo { Direction::Read => &mut waiters.reader, Direction::Write => &mut waiters.writer, }; - *slot = Some(cx.waker().clone()); + + // Avoid cloning the waker if one is already stored that matches the + // current task. + match slot { + Some(existing) => { + if !existing.will_wake(cx.waker()) { + *existing = cx.waker().clone(); + } + } + None => { + *slot = Some(cx.waker().clone()); + } + } // Try again, in case the readiness was changed while we were // taking the waiters lock @@ -348,7 +362,7 @@ unsafe impl Sync for ScheduledIo {} cfg_io_readiness! { impl ScheduledIo { /// An async version of `poll_readiness` which uses a linked list of wakers - pub(crate) async fn readiness(&self, interest: mio::Interest) -> ReadyEvent { + pub(crate) async fn readiness(&self, interest: Interest) -> ReadyEvent { self.readiness_fut(interest).await } @@ -356,7 +370,7 @@ cfg_io_readiness! { // we are borrowing the `UnsafeCell` possibly over await boundaries. // // Go figure. - fn readiness_fut(&self, interest: mio::Interest) -> Readiness<'_> { + fn readiness_fut(&self, interest: Interest) -> Readiness<'_> { Readiness { scheduled_io: self, state: State::Init, |