GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Provide C implementation for asyncio.current_task · Issue #100344 · python/cpython

Provide C implementation for asyncio.current_task #100344

New issue
Copy link
New issue
Copy link
Closed
Closed
Provide C implementation for asyncio.current_task#100344
Copy link
Labels
3.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asyncio

Description

@itamaro
itamaro
opened on Dec 19, 2022
Issue body actions

Feature or enhancement

By providing a C implementation for asyncio.current_task, its performance can be improved.

Pitch

Performance improvement.

From Instagram profiling data, we've found that this function is called frequently, and a C implementation (in Cinder 3.8) showed more than 4x speedup in a microbenchmark.

Previous discussion

N/A

Linked PRs

  • gh-100344: Provide C implementation for asyncio.current_task #100345
Reacted by J. Nick Koston and Kamil Monicz

Activity

  1. itamaro
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Dec 19, 2022
  2. itamaro
    added a commit that references this issue on Dec 19, 2022

    pythongh-100344: Provide C implementation for asyncio.current_task

    76cbbc6
  3. kumaraditya303 commented on Dec 19, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 19, 2022
    Contributor
    More actions

    Can you post benchmarks with pyperf?

  4. kumaraditya303
    added
    performancePerformance or resource usagePerformance or resource usage
    topic-asyncio
    3.12only security fixesonly security fixes
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Dec 19, 2022
  5. ezio-melotti
    added this to asyncioon Dec 19, 2022
  6. ezio-melotti
    moved this to Todo in asyncioon Dec 19, 2022
  7. itamaro commented on Dec 19, 2022

    @itamaro
    itamaro
    on Dec 19, 2022
    ContributorAuthor
    More actions

    Can you post benchmarks with pyperf?

    You mean pyperformance suite with and without the C acceleration?

  8. kumaraditya303 commented on Dec 19, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 19, 2022
    Contributor
    More actions

    You mean pyperformance suite with and without the C acceleration?

    No this microbenchmark with pyperf.

  9. bluetech commented on Dec 19, 2022

    @bluetech
    bluetech
    on Dec 19, 2022
    Contributor
    More actions

    As a note of support for making this faster, current_task is used in the asgiref.local implementation, which is used a lot by Django, and it shows up in profiles.

    Reacted by Itamar Oren
  10. itamaro commented on Dec 19, 2022

    @itamaro
    itamaro
    on Dec 19, 2022
    ContributorAuthor
    More actions

    No this microbenchmark with pyperf.

    thanks for the clarification :)

    I don't know how to isolate the overhead of the event loop when using pyperf, so hope this is helpful:

    C implementation:

    $ python -m pyperf timeit -s 'from asyncio.tasks import _c_current_task as current_task' -s 'from asyncio import run' -s '
    async def main():
      for _ in range(10**6):
        current_task()
    ' 'run(main())'
    .....................
    Mean +- std dev: 33.4 ms +- 1.0 ms
    

    Python implementation:

    $ python -m pyperf timeit -s 'from asyncio.tasks import _py_current_task as current_task' -s 'from asyncio import run' -s '
    async def main():
      for _ in range(10**6):
        current_task()
    ' 'run(main())'
    .....................
    Mean +- std dev: 133 ms +- 8 ms
    
    Reacted by Guido van Rossum
  11. kumaraditya303 commented on Dec 20, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 20, 2022
    Contributor
    More actions

    The numbers looks interesting, it seems to be because there is no fastpath for dict.get like there is for list.append in ceval.

    @markshannon Do you have plans to optimize this?

    Reacted by Itamar Oren
  12. markshannon commented on Dec 20, 2022

    @markshannon
    markshannon
    on Dec 20, 2022
    Member
    More actions

    @kumaraditya303 No plans at the moment.

    I'd be interested to see how this compared:

    def current_task(loop=None):
        """Return a currently executed task."""
        if loop is None:
            loop = events.get_running_loop()
        try:
            return _current_tasks[loop]
        except:
            return None

    I assume that current_task() is expected to return a task, not None, most of the time.

    Reacted by Itamar Oren
  13. itamaro commented on Dec 20, 2022

    @itamaro
    itamaro
    on Dec 20, 2022
    ContributorAuthor
    More actions

    I assume that current_task() is expected to return a task, not None, most of the time.

    makes sense!

    this optimization make the python impl about 40% faster!

    $ python -m pyperf timeit -s 'from asyncio.tasks import _py_current_task as current_task' -s 'from asyncio import run' -s '
    async def main():
      for _ in range(10**6):
        current_task()
    ' 'run(main())'
    .....................
    Mean +- std dev: 81.1 ms +- 4.9 ms
    

    the C impl is still more than 2x faster than this, so maybe do both?

  14. gvanrossum commented on Dec 21, 2022

    @gvanrossum
    gvanrossum
    on Dec 21, 2022
    Member
    More actions

    Why bother speeding up the Python version if we have the C version? There really aren't any interesting situations where the C accelerator is unavailable (that I know of).

  15. itamaro commented on Dec 21, 2022

    @itamaro
    itamaro
    on Dec 21, 2022
    ContributorAuthor
    More actions

    Why bother speeding up the Python version if we have the C version? There really aren't any interesting situations where the C accelerator is unavailable (that I know of).

    In my mind it's "why not speed up the Python version?"
    I'm not aware of situations where it matters for cpython users, but maybe alternative implementations that use cpython's stdlib it would be valuable?
    anyway, I don't feel strongly about it. happy to revert to the existing python implementation if you'd prefer!

  16. gvanrossum commented on Dec 21, 2022

    @gvanrossum
    gvanrossum
    on Dec 21, 2022
    Member
    More actions

    In my mind it's "why not speed up the Python version?"

    Because you're replacing one line of code with a well-known idiom with four lines of code that require the reader to follow carefully what's going on and why. For me, reading the version with .get() is much quicker than the try/except version.

    If we wrote hyper-optimized code like that everywhere, even when speed doesn't matter, we'd end up with considerably less readable code. So, in my mind the question very much needs to be "why speed it up".

    Reacted by Itamar Oren, Paweł Rubin and AlenPaulVarghese
  17. carljm commented on Dec 22, 2022

    @carljm
    carljm
    on Dec 22, 2022
    Member
    More actions

    The optimization in the Python version is also very much tuned for the current performance characteristics of the adaptive interpreter (i.e. that dict subscripts are much better optimized than dict method calls.) If alternate Python implementations are the only likely users of the Python implementation, this code change won't necessarily give similar speedups for them.

    Reacted by Itamar Oren and Asif Saif Uddin {"Auvi":"অভি"}
  18. kumaraditya303
    added a commit that references this issue on Dec 22, 2022

    gh-100344: Add C implementation for `asyncio.current_task` (#100345)

    4cc63e0
  19. kumaraditya303
    closed this as completedon Dec 22, 2022
  20. Repository owner moved this from Todo to Done in asyncioon Dec 22, 2022
  21. facebook-github-bot
    added a commit that references this issue on Dec 27, 2022

    Backport gh-100345 / Port D39555934: Provide C implementation for asy…

    27df1dc
  22. iritkatriel
    added a commit that references this issue on Dec 28, 2022

    Merged main into regmachine branch (#50)

    fdc73cb
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

    3.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asyncio

    Projects

    • asyncio
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.