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

Investigate Cython support #56

Closed
maxfischer2781 opened this issue Aug 1, 2021 · 7 comments
Closed

Investigate Cython support #56

maxfischer2781 opened this issue Aug 1, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@maxfischer2781
Copy link
Owner

It looks like Cython is compatible with asyncio, python-trio/trio#550 and dabeaz/curio#326, as well as bulk of Python level functions (see cython/cython#2273). Since asyncstdlib currently does some heavy lifting using pure Python, optimising this seems prudent.

Rough checkpoints to using Cython:

  • Check whether Cython 0.29/0.3 has sufficient support for async
    • Check that current code can be significantly optimised
    • Check whether PyPy can benefit; PyPy support will not be dropped.
  • Add unittests using asyncio, trio, curio to ensure compatibility
  • Add benchmarks to verify added complexity is worth it
@maxfischer2781 maxfischer2781 added the enhancement New feature or request label Aug 1, 2021
@maxfischer2781
Copy link
Owner Author

maxfischer2781 commented Aug 1, 2021

Quick check shows that Cython can handle all of asyncstdlib, with the exception of two one "clever" comprehensions. These can be refactored to work with Cython.

@maxfischer2781
Copy link
Owner Author

Packaging is a lot less straightforward. Basic template would be the Cython guide. Possible example projects are hdbscan and a third-party cython-package-example.

My main concern is that the package still has to work "out of the box". That means at least building wheels for all major platforms; separate GitHub Actions could help here.

@maxfischer2781
Copy link
Owner Author

A simple approach to the packaging issue would be to provide a separate package for fast utilities. Similar to the stdlib, asyncstdlib would optionally import it for speedup if available.

@maxfischer2781
Copy link
Owner Author

Major showstopper in my tests so far is that Cython's PyTypeObject does not (yet) define the tp_as_async slot.

@maxfischer2781
Copy link
Owner Author

Turns out the missing type fields are simple enough to define.

cdef extern from "Python.h":
    ctypedef struct PyAsyncMethods:
        unaryfunc am_await
        unaryfunc am_aiter
        unaryfunc am_anext

    ctypedef struct PyTypeObject:
        ...
        PyAsyncMethods* tp_as_async

    ...

Got it working for a basic aiter out of the box.

@maxfischer2781
Copy link
Owner Author

Experimental code is in a separate repo: https://github.com/maxfischer2781/asyncstdlib_cy

@maxfischer2781
Copy link
Owner Author

I do have a clear roadmap on how this could be done, but it does not seem worth the effort at this point. There would have to be a separately maintained Cython codebase, yet the optimisations that Cython can perform statically on coroutines (especially the async-neutral ones) are minimal.


If anyone needs a push of performance but cannot switch to PyPy, it looks like nuitka can compile the entire library for CPython.

python -m nuitka --module asyncstdlib --include-package=asyncstdlib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant