Visitar URL original
Optimize `asyncio.get_running_loop` · Issue #100363 · python/cpython · GitHub
Skip to content

Optimize asyncio.get_running_loop #100363

Description

@kumaraditya303

asyncio.get_running_loop is one of the most performance critical functions in asyncio. With #66285 the running loop can be reset in the fork handler thereby avoiding the need for the getpid checks. The whole running loop holder object is unnecessary now and adds up unnecessary dependent memory loads.

Benchmark:

import asyncio
from pyperf import Runner

async def main():
    for i in range(10000):
        asyncio.get_running_loop()


runner = Runner()
runner.bench_async_func("main", main)

Result:

Benchmark base patch
main 5.03 ms 328 us: 15.33x faster

The numbers speak for themselves.

Linked PRs

Activity

  1. gvanrossum commented on Dec 20, 2022

    @gvanrossum
    Member

    asyncio.get_running_loop is one of the most performance critical functions in asyncio.

    Do you have data about this? Does it show up in profiles of real functions, or do you have call counts or whatever? Usually I imagine the call is followed by a much more expensive computation.

  2. kumaraditya303 commented on Dec 20, 2022

    @kumaraditya303
    ContributorAuthor

    This is based upon call counts and the fact that most functions now internally call this function as loop argument was removed from most asyncio APIs.

  3. gvanrossum commented on Dec 20, 2022

    @gvanrossum
    Member

    Okay, makes sense.

  4. added a commit that references this issue on Dec 21, 2022
  5. Repository owner moved this from Todo to Done in asyncioon Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions