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 2 commits that reference this issue on Nov 2, 2026
  2. 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
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-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions