Skip to content

Commit

Permalink
remove double store of deadline, saving 128 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Feb 6, 2023
1 parent 88b1eb5 commit 73733a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
21 changes: 15 additions & 6 deletions tokio/src/runtime/time/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -504,7 +506,8 @@ impl TimerEntry {
Self {
driver,
inner: StdUnsafeCell::new(TimerShared::new()),
initial_deadline: Some(deadline),
deadline,
registered: false,
_m: std::marker::PhantomPinned,
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down
9 changes: 3 additions & 6 deletions tokio/src/time/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,13 @@ pin_project! {
cfg_trace! {
#[derive(Debug)]
struct Inner {
deadline: Instant,
ctx: trace::AsyncOpTracingCtx,
}
}

cfg_not_trace! {
#[derive(Debug)]
struct Inner {
deadline: Instant,
}
}

Expand Down Expand Up @@ -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 }
}
Expand All @@ -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.
Expand Down Expand Up @@ -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"))]
{
Expand Down

0 comments on commit 73733a0

Please sign in to comment.