Skip to content

Commit 134d248

Browse files
authored
Rollup merge of rust-lang#66398 - sfackler:no-async-nesting, r=Centril
Remove some stack frames from `.async` calls The `Context` argument is currently smuggled through TLS for async-generated futures. The current infrastructure is closure-based, and results in an extra 6 stack frames when .awaiting an async-generated future! ``` 12: foo::async_b::{{closure}} at src/main.rs:10 13: <std::future::GenFuture<T> as core::future::future::Future>::poll::{{closure}} at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:43 14: std::future::set_task_context at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:79 15: <std::future::GenFuture<T> as core::future::future::Future>::poll at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:43 16: std::future::poll_with_tls_context::{{closure}} at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:121 17: std::future::get_task_context at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:111 18: std::future::poll_with_tls_context at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:121 19: foo::async_a::{{closure}} at src/main.rs:6 ``` While the long (medium?) term solution is to remove the use of TLS entirely, we can improve things a bit in the meantime. In particular, this commit does 2 things: 1. `get_task_context` has been inlined into `poll_with_tls_context`, removing 2 frames (16 and 17 above). 2. `set_task_context` now returns a guard type that resets the TLS rather than taking a closure, removing 2 frames (13 and 14 above). We can also remove frame 18 by removing `poll_with_tls_context` in favor of a `get_task_context` function which returns a guard, but that requires adjusting the code generated for .await, so I've left that off for now.
2 parents 4081586 + 3fe7cfc commit 134d248

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

src/libstd/future.rs

+12-33
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
4040
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
4141
// Safe because we're !Unpin + !Drop mapping to a ?Unpin value
4242
let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
43-
set_task_context(cx, || match gen.resume() {
43+
let _guard = unsafe { set_task_context(cx) };
44+
match gen.resume() {
4445
GeneratorState::Yielded(()) => Poll::Pending,
4546
GeneratorState::Complete(x) => Poll::Ready(x),
46-
})
47+
}
4748
}
4849
}
4950

@@ -61,35 +62,23 @@ impl Drop for SetOnDrop {
6162
}
6263
}
6364

64-
#[doc(hidden)]
65-
#[unstable(feature = "gen_future", issue = "50547")]
66-
/// Sets the thread-local task context used by async/await futures.
67-
pub fn set_task_context<F, R>(cx: &mut Context<'_>, f: F) -> R
68-
where
69-
F: FnOnce() -> R
70-
{
65+
// Safety: the returned guard must drop before `cx` is dropped and before
66+
// any previous guard is dropped.
67+
unsafe fn set_task_context(cx: &mut Context<'_>) -> SetOnDrop {
7168
// transmute the context's lifetime to 'static so we can store it.
72-
let cx = unsafe {
73-
core::mem::transmute::<&mut Context<'_>, &mut Context<'static>>(cx)
74-
};
69+
let cx = core::mem::transmute::<&mut Context<'_>, &mut Context<'static>>(cx);
7570
let old_cx = TLS_CX.with(|tls_cx| {
7671
tls_cx.replace(Some(NonNull::from(cx)))
7772
});
78-
let _reset = SetOnDrop(old_cx);
79-
f()
73+
SetOnDrop(old_cx)
8074
}
8175

8276
#[doc(hidden)]
8377
#[unstable(feature = "gen_future", issue = "50547")]
84-
/// Retrieves the thread-local task context used by async/await futures.
85-
///
86-
/// This function acquires exclusive access to the task context.
87-
///
88-
/// Panics if no context has been set or if the context has already been
89-
/// retrieved by a surrounding call to get_task_context.
90-
pub fn get_task_context<F, R>(f: F) -> R
78+
/// Polls a future in the current thread-local task waker.
79+
pub fn poll_with_tls_context<F>(f: Pin<&mut F>) -> Poll<F::Output>
9180
where
92-
F: FnOnce(&mut Context<'_>) -> R
81+
F: Future
9382
{
9483
let cx_ptr = TLS_CX.with(|tls_cx| {
9584
// Clear the entry so that nested `get_task_waker` calls
@@ -108,15 +97,5 @@ where
10897
//
10998
// The pointer that was inserted came from an `&mut Context<'_>`,
11099
// so it is safe to treat as mutable.
111-
unsafe { f(cx_ptr.as_mut()) }
112-
}
113-
114-
#[doc(hidden)]
115-
#[unstable(feature = "gen_future", issue = "50547")]
116-
/// Polls a future in the current thread-local task waker.
117-
pub fn poll_with_tls_context<F>(f: Pin<&mut F>) -> Poll<F::Output>
118-
where
119-
F: Future
120-
{
121-
get_task_context(|cx| F::poll(f, cx))
100+
unsafe { F::poll(f, cx_ptr.as_mut()) }
122101
}

0 commit comments

Comments
 (0)