Skip to content

Commit 925c614

Browse files
time: reduce the generated code size of Timeout<T>::poll (#7535)
1 parent dd74c7c commit 925c614

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

tokio/src/time/timeout.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,32 @@ where
203203
return Poll::Ready(Ok(v));
204204
}
205205

206-
let has_budget_now = coop::has_budget_remaining();
207-
208-
let delay = me.delay;
209-
210-
let poll_delay = || -> Poll<Self::Output> {
211-
match delay.poll(cx) {
212-
Poll::Ready(()) => Poll::Ready(Err(Elapsed::new())),
213-
Poll::Pending => Poll::Pending,
214-
}
215-
};
216-
217-
if let (true, false) = (had_budget_before, has_budget_now) {
218-
// if it is the underlying future that exhausted the budget, we poll
219-
// the `delay` with an unconstrained one. This prevents pathological
220-
// cases where the underlying future always exhausts the budget and
221-
// we never get a chance to evaluate whether the timeout was hit or
222-
// not.
223-
coop::with_unconstrained(poll_delay)
224-
} else {
225-
poll_delay()
226-
}
206+
poll_delay(had_budget_before, me.delay, cx).map(Err)
207+
}
208+
}
209+
210+
// The T-invariant portion of Timeout::<T>::poll. Pulling this out reduces the
211+
// amount of code that gets duplicated during monomorphization.
212+
fn poll_delay(
213+
had_budget_before: bool,
214+
delay: Pin<&mut Sleep>,
215+
cx: &mut task::Context<'_>,
216+
) -> Poll<Elapsed> {
217+
let delay_poll = || match delay.poll(cx) {
218+
Poll::Ready(()) => Poll::Ready(Elapsed::new()),
219+
Poll::Pending => Poll::Pending,
220+
};
221+
222+
let has_budget_now = coop::has_budget_remaining();
223+
224+
if let (true, false) = (had_budget_before, has_budget_now) {
225+
// if it is the underlying future that exhausted the budget, we poll
226+
// the `delay` with an unconstrained one. This prevents pathological
227+
// cases where the underlying future always exhausts the budget and
228+
// we never get a chance to evaluate whether the timeout was hit or
229+
// not.
230+
coop::with_unconstrained(delay_poll)
231+
} else {
232+
delay_poll()
227233
}
228234
}

0 commit comments

Comments
 (0)