Skip to content
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

Optimize asyncio.get_running_loop #100363

Closed
kumaraditya303 opened this issue Dec 20, 2022 · 3 comments
Closed

Optimize asyncio.get_running_loop #100363

kumaraditya303 opened this issue Dec 20, 2022 · 3 comments
Assignees
Labels
3.12 expert-asyncio performance Performance or resource usage

Comments

@kumaraditya303
Copy link
Contributor

kumaraditya303 commented Dec 20, 2022

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

@gvanrossum
Copy link
Member

gvanrossum commented Dec 20, 2022

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.

@kumaraditya303
Copy link
Contributor Author

kumaraditya303 commented Dec 20, 2022

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.

@gvanrossum
Copy link
Member

gvanrossum commented Dec 20, 2022

Okay, makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 expert-asyncio performance Performance or resource usage
Projects
Status: Done
Development

No branches or pull requests

2 participants