GeezerVison is showing github.com — gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly by itamaro · Pull Request #104140 · python/cpython
Conversation
itamaro
commented
May 3, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 creating tasks as part of a TaskGroup, e.g.:
asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)
async with asyncio.TaskGroup() as tg:
tg.create_task(coro1)
tg.create_task(coro2)
tg.create_task(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 of TaskGroup uses callbacks internally that end up getting scheduled to the event loop even if all the tasks were able to finish synchronously, and blocking the coroutine in which TaskGroup() was awaited, preventing the task from completing eagerly even if otherwise it could.
applications that use multiple levels of nested TaskGroups can benefit significantly from eagerly completing multiple levels without blocking, as implemented in this PR by skipping scheduling the done callback if the future is done.
Benchmarks
this makes the async pyperformance benchmarks up to 4x faster (!!), using a patch to pyperformance that adds "eager" flavors and uses TaskGroups instead of gather
3.12-base.20230503.async.3.json
===============================
Performance version: 1.0.7
Python version: 3.12.0a7+ (64-bit) revision da1980afcb
Report on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31
Number of logical CPUs: 72
Start date: 2023-05-03 22:18:14.628900
End date: 2023-05-03 22:36:35.397630
3.12-tgcb.20230503.async.3.json
===============================
Performance version: 1.0.7
Python version: 3.12.0a7+ (64-bit) revision 5397cd9f62
Report on Linux-5.15.0-1033-aws-x86_64-with-glibc2.31
Number of logical CPUs: 72
Start date: 2023-05-03 22:38:45.960219
End date: 2023-05-03 22:54:49.008951
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| Benchmark | 3.12-base.20230503.async.3.json | 3.12-tgcb.20230503.async.3.json | Change | Significance |
+===============================+=================================+=================================+==============+========================+
| async_tree_cpu_io_mixed | 843 ms | 827 ms | 1.02x faster | Not significant |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager | 383 ms | 92.9 ms | 4.12x faster | Significant (t=189.26) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_cpu_io_mixed | 708 ms | 412 ms | 1.72x faster | Significant (t=180.43) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_io | 1.40 sec | 1.39 sec | 1.01x faster | Not significant |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_eager_memoization | 515 ms | 232 ms | 2.22x faster | Significant (t=183.24) |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_io | 1.37 sec | 1.35 sec | 1.01x faster | Not significant |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_memoization | 644 ms | 634 ms | 1.01x faster | Not significant |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
| async_tree_none | 517 ms | 508 ms | 1.02x faster | Not significant |
+-------------------------------+---------------------------------+---------------------------------+--------------+------------------------+
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
itamaro
changed the title
gh-NNNN: Skip scheduling a done callback if a TaskGroup task completes eagerly
gh-104144: Skip scheduling a done callback if a TaskGroup task completes eagerly
May 3, 2023
Uh oh!
There was an error while loading. Please reload this page.
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.
How sure are you that skipping the set-name call if name is None is always correct? (It seems fine to me, but in theory some 3rd party task implementation could do other stuff there.)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
itamaro reacted with thumbs up emoji
How sure are you that skipping the set-name call if name is None is always correct? (It seems fine to me, but in theory some 3rd party task implementation could do other stuff there.)
I don't know about all possible 3p task implementations. At least looking at uvloop, it seems it uses asyncio.Task directly.
the asyncio Task.set_name returns immediately if name is NULL, so just saving the function call is a pretty minor optimization that we can probably revert with no noticeable regression (I can measure it). If this will make interop with 3p Task implementations better I think it's worth it.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I don't think it will be measurable -- it looks to be a very minor part of creating a task (even more so with gh-103767). It just is one more thing we'd have to put in what's new (and it's not covered by the PR title :-).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
agreed, let me revert that part!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
@@ -0,0 +1 @@ |
|
|
Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`. Skip scheduling done callbacks when all tasks finish without blocking.
|
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.
Hmm actually the scheduling is skipped on a per task basis rather than when all tasks finish without blocking.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|