-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-104144: Optimize gather to finish eagerly when all futures complete eagerly #104138
gh-104144: Optimize gather to finish eagerly when all futures complete eagerly #104138
Conversation
4a5d42c
to
b3e479a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty straightforward and reasonable to me, but would prefer for asyncio experts to take a look.
Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Carl Meyer <carl@oddbird.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at gather._done_callback()
you'll see it has a bunch of logic which gets executed at the moment all the futures have finished (starting from if nfinished == nfuts:
on line 781). If all args can complete eagerly then this will be executed eagerly too.
This might have a few issues:
- At this point
outer
will beNone
and this will cause trouble on line 803. (Maybe I missed something because I'm surprised this hasn't come up yet). - Handling for futures that were cancelled during eager execution is processed but the results discarded.
- We inefficiently create a result list which we discard and then repeat when creating the eagerly completed future result.
Fortunately, I think an easy fix is to move creation of the eager result future to before the argument processing loop. See my in-line comments for specifics.
I'm not 100% sure how this will affect the issue described in bpo-46672, but it has a test so we'll see.
Lib/asyncio/tasks.py
Outdated
outer = futures.Future(loop=loop) | ||
outer.set_result([c.result for c in children]) | ||
else: | ||
outer = _GatheringFuture(children, loop=loop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outer = _GatheringFuture(children, loop=loop) | |
outer.__self_log_traceback = False | |
outer = _GatheringFuture(children, loop=loop) |
…e loop, after the GatheringFuture (outer) is created
The title no longer accurately describes the updated PR, since the |
thanks for the review @jbower-fb ! I pushed a new version of the PR based on your suggestions, but not identical.
|
thanks, I updated the title! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, will fix the typo. Maybe @carljm can merge when you all are agreed on this. Nice fix!
Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
Outdated
Show resolved
Hide resolved
PS. In general there's no need to click the "Update branch" button (or otherwise merge main back into the PR) unless there are fixes/changes that might affect the PR (e.g. if touching the same file). |
gh-97696 introduced eager tasks factory, which speeds up some async-heavy workloads by up to 50% when opted in.
installing the eager tasks factory applies out-of-the-box when gathering futures (
asyncio.gather(...)
), e.g.:coro{1,2,3}
will eagerly execute the first step, and potentially complete without scheduling to the event loop if the coros don't block.the implementation of
eager
uses callbacks internally that end up getting scheduled to the event loop even if all the futures were able to finish synchronously, and blocking the coroutine in whichgather()
was awaited, preventing the task from completing eagerly even if otherwise it could.applications that use multiple levels of nested gathers can benefit significantly from eagerly completing multiple levels without blocking, as implemented in this PR by skipping scheduling done callbacks for futures that are already done (e.g. finished eagerly).
Benchmarks
this makes the async pyperformance benchmarks up to 3x faster (!!), using a patch to pyperformance that adds "eager" flavors