Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-104144: Optimize gather to finish eagerly when all futures complete eagerly#104138

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@itamaro
Copy link
Contributor

@itamaroitamaro commentedMay 3, 2023
edited
Loading

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.:

asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)await asyncio.gather(coro1, coro2, coro3)

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 ofeager 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 apatch to pyperformance that adds "eager" flavors

3.12-base.20230503.async.4.json===============================Performance version: 1.0.7Python version: 3.12.0a7+ (64-bit) revision da1980afcbReport on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31Number of logical CPUs: 72Start date: 2023-05-03 23:27:23.329046End date: 2023-05-03 23:46:37.7063263.12-nogf.20230503.async.2.json===============================Performance version: 1.0.7Python version: 3.12.0a7+ (64-bit) revision 5397cd9f62Report on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31Number of logical CPUs: 72Start date: 2023-05-03 23:05:45.011427End date: 2023-05-03 23:22:44.908094+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| Benchmark                     | 3.12-base.20230503.async.4.json | 3.12-nogf.20230503.async.2.json | Change       | Significance           |+===============================+=================================+=================================+==============+========================+| async_tree_cpu_io_mixed       | 868 ms                          | 859 ms                          | 1.01x faster | Not significant        |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_eager              | 391 ms                          | 129 ms                          | 3.03x faster | Significant (t=209.74) |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_eager_cpu_io_mixed | 756 ms                          | 490 ms                          | 1.54x faster | Significant (t=167.41) |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_eager_io           | 1.51 sec                        | 1.50 sec                        | 1.00x faster | Not significant        |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_eager_memoization  | 595 ms                          | 314 ms                          | 1.89x faster | Significant (t=70.25)  |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_io                 | 1.39 sec                        | 1.40 sec                        | 1.00x slower | Not significant        |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_memoization        | 677 ms                          | 683 ms                          | 1.01x slower | Not significant        |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+| async_tree_none               | 575 ms                          | 574 ms                          | 1.00x faster | Not significant        |+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+

@itamaroitamaroforce-pushed theasyncio-skip-gathering-future branch from4a5d42c tob3e479aCompareMay 3, 2023 20:57
@itamaroitamaro changed the titlegh-NNNN: Skip creating GatheringFuture if all futures finished eagerlygh-104144: Skip creating GatheringFuture if all futures finished eagerlyMay 3, 2023
@itamaroitamaro marked this pull request as ready for reviewMay 3, 2023 23:52
Copy link
Member

@carljmcarljm left a 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.

itamaro reacted with thumbs up emoji
Co-authored-by: Carl Meyer <carl@oddbird.net>
Copy link
Contributor

@jbower-fbjbower-fb left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If you look atgather._done_callback() you'll see it has a bunch of logic which gets executed at the moment all the futures have finished (starting fromif 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 pointouter 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, Ithink 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 inbpo-46672, but it has a test so we'll see.

itamaro reacted with thumbs up emoji
outer=futures.Future(loop=loop)
outer.set_result([c.resultforcinchildren])
else:
outer=_GatheringFuture(children,loop=loop)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
outer=_GatheringFuture(children,loop=loop)
outer.__self_log_traceback=False
outer=_GatheringFuture(children,loop=loop)

@carljm
Copy link
Member

The title no longer accurately describes the updated PR, since the_GatheringFuture is always created.

itamaro reacted with thumbs up emoji

@itamaro
Copy link
ContributorAuthor

thanks for the review@jbower-fb !

I pushed a new version of the PR based on your suggestions, but not identical.
summary of my changes:

  • instead of immediately calling the done callbacks for eagerly completed futures in the loop, I add them to a list and call their callbacks only at the end, after creating the outer future
  • this helps because now outer is defined the same way, and the children list is fully populated, which is also important because the done callback uses the children list
  • I always create a GatheringFuture so I get the same cancellation treatment, but it will still finish eagerly if all futures completed eagerly thanks to the last done callback right before returning
  • there shouldn't be any issues withbpo-46672 since outer looks the same as before
jbower-fb reacted with thumbs up emoji

@itamaroitamaro changed the titlegh-104144: Skip creating GatheringFuture if all futures finished eagerlygh-104144: Optimize gather to finish eagerly when all futures complete eagerlyMay 5, 2023
@itamaro
Copy link
ContributorAuthor

The title no longer accurately describes the updated PR, since the_GatheringFuture is always created.

thanks, I updated the title!

carljm reacted with thumbs up emoji

Copy link
Member

@gvanrossumgvanrossum left a 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!

@AlexWaygoodAlexWaygood added the performancePerformance or resource usage labelMay 6, 2023
@gvanrossum
Copy link
Member

PS. Ingeneral 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).

@kumaraditya303kumaraditya303enabled auto-merge (squash)May 6, 2023 14:55
@kumaraditya303kumaraditya303 merged commit263abd3 intopython:mainMay 6, 2023
@itamaroitamaro deleted the asyncio-skip-gathering-future branchMay 7, 2023 22:16
jbower-fb pushed a commit to jbower-fb/cpython that referenced this pull requestMay 8, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@carljmcarljmcarljm left review comments

@gvanrossumgvanrossumgvanrossum approved these changes

@kumaraditya303kumaraditya303kumaraditya303 approved these changes

@1st11st1Awaiting requested review from 1st11st1 is a code owner

@asvetlovasvetlovAwaiting requested review from asvetlovasvetlov is a code owner

@willingcwillingcAwaiting requested review from willingcwillingc is a code owner

+1 more reviewer

@jbower-fbjbower-fbjbower-fb requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

performancePerformance or resource usagetopic-asyncio

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

7 participants

@itamaro@carljm@gvanrossum@jbower-fb@kumaraditya303@bedevere-bot@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp