time: don't store deadline twice in sleep entries (#5410)

This commit is contained in:
Conrad Ludgate 2023-02-09 10:19:02 +00:00 committed by GitHub
parent 09b2653e71
commit d96bbf0465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -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<TimerShared>,
/// 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<Instant>,
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);
}

View File

@ -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,
}
}
@ -297,11 +295,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 }
}
@ -312,7 +310,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.
@ -358,7 +356,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"))]
{