time: reduce the generated code size of Timeout<T>::poll (#7535)

This commit is contained in:
Alex Bakon 2025-08-19 07:42:55 -04:00 committed by GitHub
parent dd74c7c1bf
commit 925c614c89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -203,26 +203,32 @@ where
return Poll::Ready(Ok(v));
}
let has_budget_now = coop::has_budget_remaining();
let delay = me.delay;
let poll_delay = || -> Poll<Self::Output> {
match delay.poll(cx) {
Poll::Ready(()) => Poll::Ready(Err(Elapsed::new())),
Poll::Pending => Poll::Pending,
}
};
if let (true, false) = (had_budget_before, has_budget_now) {
// if it is the underlying future that exhausted the budget, we poll
// the `delay` with an unconstrained one. This prevents pathological
// cases where the underlying future always exhausts the budget and
// we never get a chance to evaluate whether the timeout was hit or
// not.
coop::with_unconstrained(poll_delay)
} else {
poll_delay()
}
poll_delay(had_budget_before, me.delay, cx).map(Err)
}
}
// The T-invariant portion of Timeout::<T>::poll. Pulling this out reduces the
// amount of code that gets duplicated during monomorphization.
fn poll_delay(
had_budget_before: bool,
delay: Pin<&mut Sleep>,
cx: &mut task::Context<'_>,
) -> Poll<Elapsed> {
let delay_poll = || match delay.poll(cx) {
Poll::Ready(()) => Poll::Ready(Elapsed::new()),
Poll::Pending => Poll::Pending,
};
let has_budget_now = coop::has_budget_remaining();
if let (true, false) = (had_budget_before, has_budget_now) {
// if it is the underlying future that exhausted the budget, we poll
// the `delay` with an unconstrained one. This prevents pathological
// cases where the underlying future always exhausts the budget and
// we never get a chance to evaluate whether the timeout was hit or
// not.
coop::with_unconstrained(delay_poll)
} else {
delay_poll()
}
}