Waker::will_wake() gets mostly defeated by executor optimizations #66281
Labels
A-async-await
Area: Async & Await
AsyncAwait-Triaged
Async-await issues that have been triaged during a working group meeting.
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
C-optimization
Category: An issue highlighting optimization opportunities or PRs implementing such
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
The Futures
Waker
API provides a Waker::will_wake() method which tries to determine whether the 2Waker
s are equivalent. It intends to allowFuture
s so determine whether they have to swap aWaker
or have to can keep an already stored one which is equivalent. That can avoid some churn on atomic refcounts on thoseWaker
s.The
will_wake
method is implemented by aPartialEq
derive onRawWaker
- which meanswill_wake()
will returntrue
only if the pointer as well as the vtable inRawWaker
are equivalent.However futures executors are moving more and more towards a design where they lazily create the "real" storeable
Waker
, and only provide aWaker
reference to tasks they poll. E.g.futures-rs
uses WakerRef tokio does the same. This avoids an atomic increment and decrement cycle for each task that an executor polls.However this means the
RawWaker
that the executor passes through theContext
parameter will now always be different to the one stored inside aFuture
andwill_wake()
will return false. Which causes theFuture
to update theWaker
reference (2 atomic ops). If the executor polls a couple of sub-tasks which now all swap out theirWaker
s the original executor optimization could now even lead to de-optimization - since the result is more atomic ops in other places.Using the
will_wake()
method makes most sense exactly inside the.poll()
method of aFuture
in order to determine whether a storedWaker
needs to get updated. This is now the exact same point where theWakerRef
would return the false negative. Therefore using.will_wake()
is not that helpful given the state of the executor ecosystem.So far for the description of the issue. The next question is whether this can be improved or solved. I don't think changing
PartialEq for RawWaker
- e.g. to conly compare the data pointer - makes sense. It will likely only lead to false positives (waker.will_wake(other) == true
). Which then leads to missing wakeups aka live-locks.The original design of
RawWaker
plusVtable
contained a vtable entry forwill_wake()
. This would have definitely helped to solve the issue, since the executors would have been able to overwrite the check and to be able to associateWaker
s and theirWakerRef
s. However this was removed for simplification purposes.I think one possible outcome could be to keep this issue as a tracking issue for the deficit. And if there is ever a change to the vtable for other reasons to also add a
will_wake
method back. Up to then it's probably not worth an update.The text was updated successfully, but these errors were encountered: