From 73733a095c5f4038e921c7b9dce8afa04171e739 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sun, 29 Jan 2023 23:05:56 +0000 Subject: [PATCH] remove double store of deadline, saving 128 bytes --- tokio/src/runtime/time/entry.rs | 21 +++++++++++++++------ tokio/src/time/sleep.rs | 9 +++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/tokio/src/runtime/time/entry.rs b/tokio/src/runtime/time/entry.rs index f0d613a3bb4..ad55a2e8290 100644 --- a/tokio/src/runtime/time/entry.rs +++ b/tokio/src/runtime/time/entry.rs @@ -298,9 +298,11 @@ pub(crate) struct TimerEntry { /// This is manipulated only under the inner mutex. TODO: Can we use loom /// cells for this? inner: StdUnsafeCell, - /// Initial deadline for the timer. This is used to register on the first + /// Deadline for the timer. This is used to register on the first /// poll, as we can't register prior to being pinned. - initial_deadline: Option, + deadline: Instant, + /// Whether the deadline has been registered. + registered: bool, /// Ensure the type is !Unpin _m: std::marker::PhantomPinned, } @@ -504,7 +506,8 @@ impl TimerEntry { Self { driver, inner: StdUnsafeCell::new(TimerShared::new()), - initial_deadline: Some(deadline), + deadline, + registered: false, _m: std::marker::PhantomPinned, } } @@ -513,8 +516,12 @@ impl TimerEntry { unsafe { &*self.inner.get() } } + pub(crate) fn deadline(&self) -> Instant { + self.deadline + } + pub(crate) fn is_elapsed(&self) -> bool { - !self.inner().state.might_be_registered() && self.initial_deadline.is_none() + !self.inner().state.might_be_registered() && self.registered } /// Cancels and deregisters the timer. This operation is irreversible. @@ -545,7 +552,8 @@ impl TimerEntry { } pub(crate) fn reset(mut self: Pin<&mut Self>, new_time: Instant) { - unsafe { self.as_mut().get_unchecked_mut() }.initial_deadline = None; + unsafe { self.as_mut().get_unchecked_mut() }.deadline = new_time; + unsafe { self.as_mut().get_unchecked_mut() }.registered = true; let tick = self.driver().time_source().deadline_to_tick(new_time); @@ -567,7 +575,8 @@ impl TimerEntry { panic!("{}", crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR); } - if let Some(deadline) = self.initial_deadline { + if !self.registered { + let deadline = self.deadline; self.as_mut().reset(deadline); } diff --git a/tokio/src/time/sleep.rs b/tokio/src/time/sleep.rs index 0a012e25015..fc2ed1ae338 100644 --- a/tokio/src/time/sleep.rs +++ b/tokio/src/time/sleep.rs @@ -235,7 +235,6 @@ pin_project! { cfg_trace! { #[derive(Debug)] struct Inner { - deadline: Instant, ctx: trace::AsyncOpTracingCtx, } } @@ -243,7 +242,6 @@ cfg_trace! { cfg_not_trace! { #[derive(Debug)] struct Inner { - deadline: Instant, } } @@ -296,11 +294,11 @@ impl Sleep { resource_span, }; - Inner { deadline, ctx } + Inner { ctx } }; #[cfg(not(all(tokio_unstable, feature = "tracing")))] - let inner = Inner { deadline }; + let inner = Inner {}; Sleep { inner, entry } } @@ -311,7 +309,7 @@ impl Sleep { /// Returns the instant at which the future will complete. pub fn deadline(&self) -> Instant { - self.inner.deadline + self.entry.deadline() } /// Returns `true` if `Sleep` has elapsed. @@ -357,7 +355,6 @@ impl Sleep { fn reset_inner(self: Pin<&mut Self>, deadline: Instant) { let mut me = self.project(); me.entry.as_mut().reset(deadline); - (me.inner).deadline = deadline; #[cfg(all(tokio_unstable, feature = "tracing"))] {