Skip to content

Speed up calling an async function that doesn't suspend #161170

Open

Description

@asvetlov

Feature or enhancement

Now on await f() Python creates a coroutine object on heap and then calls coro.send(None).

If an async function doesn't suspend (directly or indirectly awaits for non-ready future in asyncio world), the coroutine is still created and send() is still called.
Very many async functions have a happy path that, for example, returns data from a memory buffer and runs I/O only if the buffer is empty. Or get an item from a queue without suspending if the queue is not empty.

The interpreter could emulate this first send(None) on the stack and lazily materialize the coroutine only if an asyc function suspends, or other conditions are met: a coroutine object is explicitly requested with immediate awaiting, C extension calls async function, an instrumentation is applied, etc.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

#91121 propose an alternative approach by introducing a new vectorcall flag.

Linked PRs

Activity

  1. added
    type-feature
    on Nov 2, 2026
  2. added 2 commits that reference this issue on Nov 2, 2026
  3. added
    performance
    interpreter-core
    on Nov 2, 2026
  4. changed the title [-]Speed up calling am async function that doesn't suspend[/-] [+]Speed up calling an async function that doesn't suspend[/+] on Nov 2, 2026
  5. added a commit that references this issue on Nov 20, 2026
  6. markshannon commented on Nov 23, 2026

    @markshannon
    Member

    I'm seeing a lot of PRs appearing linked to this issue.
    Why is speeding up calling an async function that doesn't suspend worth improving over, say, improving the speed of coroutine creation in general, or reducing the time from call to the coroutine function to entering the coroutine frame in the first send?

    Can you show what is slow and why, before attempting to improve it?

    This improvement seems worthwhile: https://github.com/python/cpython/pull/161171/changes#diff-729a985b0cb8b431cb291f1edb561bbbfea22e3f8c262451cd83328a0936a342R1656
    can we start with that?

    Also, for any changes you'll need to do wider benchmarking. Speeding up await is not worth it, if it slows down everything else.

  7. asvetlov commented on Nov 24, 2026

    @asvetlov
    ContributorAuthor

    @markshannon I'm trying slightly different approaches for solving the issue; that's why I've created many draft PRs that do the same, but the implementation diverges a little.
    Please give me a few days more to come up with the final version that is ready for the review. Or maybe two alternative PRs, each with their own benefits and downsides.

    Why is speeding up calling an async function that doesn't suspend worth improving over, say, improving the speed of coroutine creation in general, or reducing the time from call to the coroutine function to entering the coroutine frame in the first send?

    I would be glad to discuss all suggestions. The lazy coroutine initialization is what comes to my mind, but maybe I miss something.

    Can you show what is slow and why, before attempting to improve it?

    When a regular python function is called, its _PyInterpreterFrame lives on a per-thread data stack, a chain of large _PyStackChunk chunks. Thus, amortized cost for frame allocation is zero.

    When an async function is called, it first creates a coroutine object that is allocated on the heap. The object embeds its own frame as cr_iframe (and exception state as cr_exc_state). The initialization is cheap, but memory allocation is not.

    In turn, every asyncio (and async in general, the same is true for tornado, trio, etc.) program has a lot of awaited calls that don't await in runtime but return a result immediately.

    For example, await queue.put(val) doesn't suspend if the queue is not full. async with lock: ... doesn't suspend if the lock is not held at the moment of the call. await transport.read_msg() often has a message in the internal queue or it constructs a message from a binary data stream that was pre-fetched already and sits in an internal buffer. await transport.write(data) doesn't suspend if the write buffer is not overflown. It is a common fact that very many async functions have a happy path when they don't suspend but return fast.

    A simple ping-pong script that operates over sockets shows ~8% speedup with the lazy coroutine materialization. The script is written in pipelined mode; it sends 256 ping messages, 16 bytes each before starting to read pongs back. Producer-consumer scenario that sends over unbounded asyncio.Queue is 14% faster.
    Numbers for the real web server are not so impressive, sure, but I think that the change is worth it.

    Also, for any changes you'll need to do wider benchmarking.

    Fair point. I understand that we cannot sacrifice the performance of regular functions and generators.

  8. markshannon commented on Nov 24, 2026

    @markshannon
    Member

    A few more comments/suggestions

    • Let's not spend too much effort avoiding allocation. We will always need to allocate objects, and I'd rather spend that effort on speeding up allocation in general
    • The sequence of instructions from the CALL to the first POP_TOP instruction in the coroutine is quite long and predictable. It would be worth improving it two ways:
      • in the compiler/bytecode merging the pushing of the NULL None pair with getting the awaitable (as you did in an earlier PR)
      • specializing CALL for creating a generator/coroutine, or for creating it and immediately sending to it.
        The CALL specialization could skip a lot of dispatching and data movement.

    Overall, I think we can get a decent speedup with fairly localized changes.

  9. asvetlov commented on Nov 24, 2026

    @asvetlov
    ContributorAuthor

    @markshannon thanks for the feedback.

    I've already merged push_NULL and push(None) into GET_AWAITABLE opcode in #161875
    I'm very sorry for not waiting for your review. The change is pretty simple and straightforward; I hope you have no objections.

    CALL specialization for creating a coroutine (and maybe immediately starting it) sounds interesting; let me experiment with it.

    In your talk about allocation speed up, the main action point is replacing object type based freelist with size-based and applying freelists for allocating all small (<512 bytes) objects. Do I understand you right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coreperformancetype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions