◐ Shell
clean mode source ↗

Optimize `asyncio.get_running_loop`

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