How much Cython will benefit for optimization in next Python reaeases?

69 views
Skip to first unread message

Adri

unread,
Jan 28, 2023, 9:09:55 AM1/28/23
to cython-users
Hi, I'm using Cython to develop a library which makes a lot of conversions from and to Python objects (i.e. I receive a list in input, create a typed memory view from it, do some calculations, and reconvert back to a list). Sometimes I get good speed-up even with the conversions overhead, but other times I get "only" something like 1.5x (in respect to 3.10). My understanding of Cython and Python internals is quite limited, but from what I understand:
- in 3.11 Python specialized the bytecode, and this specializations should go forward in 3.12;
-  Cython doesn't use the bytecode, but talks directly to the python/C api, so it shouldn't benefit from bytecode optimizations;

I don't know if it is possible to do a prediction about this but I'd like to ask if the Python/C API will be targeted as something to optimize in next releases? (will the JIT compiler that maybe will be introduced improve also the Python/C api?)

This is relevant to me because the conversions costs a lot in my case, so if they ended-up slower in next releases in comparison to use Python directly, maybe I should change my optimization plan. Can something be said on this topic?



------------------------

Indirizzo istituzionale di posta elettronica degli studenti e dei laureati dell'UniversitĂ  di Torino
Official University of Turin email address for students and graduates 

D Woods

unread,
Jan 28, 2023, 2:24:24 PM1/28/23
to cython-users
Your understanding is right - most of the changes to 3.11 are improvements to bytecode and this doesn't benefit Cython. I'd expect the changes in Python 3.12 to be similar and there to not be much benefit to the Python C API (but you'd probably get a better answer on discuss.python.org).

> [...] which makes a lot of conversions from and to Python objects (i.e. I receive a list in input, create a typed memory view from it, do some calculations, and reconvert back to a list)

This suggests to me that you're using the wrong types in your Python interface - you should be inputing buffer types (e.g. array.array or numpy array). Getting a typed memoryview from those is very cheap. Copying lists into buffers in Cython sounds like a huge waste of time.

Adri

unread,
Jan 29, 2023, 8:24:07 AM1/29/23
to cython-users
thanks for the reply D Woods,  will try to ask the question in the Python forum, thanks for the suggestion!
Regarding the types in the interface: I don't think that in my case I can pass buffer types because the user interface wants to be a pure python library (e.g. we have list of 2d tuples to manage, making it a ndarray would be more limited in some respects to work with)

Prakhar Goel

unread,
Jan 29, 2023, 8:24:34 AM1/29/23
to cython...@googlegroups.com
You might have better luck avoiding conversions in the first place? It depends very much on your specific use case. For example, you may be able to keep the data in an efficient low-level format and use cython to provide a Python friendly interface to it.

Hard to say more without more details about the problem you're trying to solve.

-- PG

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/d987427f-2ac2-47ec-9290-884e47f2bcc0n%40googlegroups.com.

Julien Jerphanion

unread,
Jan 29, 2023, 8:24:48 AM1/29/23
to cython-users
Hi Adri,

I think speed-ups that one can observe are different to the kind of Python objects you are manipulating.
For instance, coercing numpy arrays to memoryviews has no particular cost, and so does accessing them back.

Your understanding of Cython with respect to CPython internals is right: Cython does not use bytecode, but Cython
does use CPython C API directly. In the best cases, Cython also allows writing code whose executing is entirely
independent of CPython itself (hence making it runs like C).

I do not know what you are referring to with the JIT compiler. Are you talking about CPython's interpreter?

If you can provide a few links to your work and Cython use-case, I think this will better help people better understand
and concretely improve your usage of Cython. 🙂

Julien.

Adri

unread,
Jan 30, 2023, 3:04:27 AM1/30/23
to cython-users
thanks for all your help, really appreciated! the repo of the project is here: https://github.com/rht/mesa-perf, the library when completed should become part of the back-end of the pure python library https://github.com/projectmesa/mesa which is an agent based modeling library in pure python and we are constrained to keep the interface as it is, so many times the user wants lists in output or gives lists in input. We have two aims: 1. Improve the performance of the main library 2. Create some sort of interface for the Cython back-end so that the user could improve the performance even more if he wants to do that. The main aim is 1. but the conversions when we have small inputs list are gone in the creation of the memory views (I posted a question on stack overflow on the matter:https://stackoverflow.com/questions/75269696/fast-creation-of-2d-memory-views-in-cython) so that maybe going fully with lists (I see big speed-up sometimes even with them) and drop 2. can be an option. If you have any suggestion, I will be really grateful!
Reply all
Reply to author
Forward
0 new messages