logoalt Hacker News

rich_sasha • today at 1:39 PM • 8 replies • view on HN

Here’s an idea instead (maybe it already exists). Allow a decorator for writing inline Cython. It solves, I think, 90% of the problem. On first run, or eagerly, compile the decorated functions. Basically, Cython is the JIT, and user enables it explicitly.

This way I can easily write inlined functions with proper C speed, in a Python esque language.

Reasons why I rarely use Cython without this:

- unmodified Python code running in Cython is often faster, but not that much. 50% or so is my rule of thumb.

- but the development style is very different for Cython and Python, adding a lot of impedance. Eg reloading doesn’t quite work, profiling is different, you need a build directory and a build step at deployment etc.

- Cython proper is different enough and I’d argue unintuitive enough that if I have to have all these things in place, I might as well wrap my function in Rust.

Such a decorator would give you a low friction way to make things faster when you need it, with scope for making it much faster via all the funny Cython, while keeping Python-esque semantics, in particular "development semantics".

I think it would require some language modification since Cython was definitely a superset of Python; I think some of that syntax has been streamlined, but maybe still a strict superset.


Replies

ponco • today at 3:06 PM

The main reason I don't see it in production code is you significantly increase your chance to write seg faults that kills the runtime / behaves unexpectedly to surrounding systems

lcnmrn • today at 4:35 PM

We just need Mojo to have 99% Python friendly syntax and we can forget about CPython.

somethingsome • today at 2:52 PM

I like Julia approach, compile everything on first launch, then reuse without speed penality, if you change a function, recompile that.

On another note, I'm working on a (big) PEP for python, and looking for maintainers and sponsors (as per python's rules). Do someone know where I could find such amazing persons? (I already have the code, works in cython,graalVM and pypy, and it's kind of a big change but I believe it will help a lot the language)

➕ show 5 replies
O5vYtytb • today at 2:32 PM

This is fine for your own application code, but it doesn't do anything for the libraries you pull in for your project. I don't think that sprinkling Cython code in all library code is going to be a popular solution, especially for those not performance focused. The idea behind a JIT is to be a general performance solution not requiring changes in existing code.

adsharma • today at 2:06 PM

This is largely the idea behind py2many + fquery, but we want to get rid of C-API as well.

  https://github.com/py2many/py2many
  https://github.com/adsharma/fquery/blob/main/tests/django_example_model.py
ttul • today at 1:54 PM

I built this in 2001 (https://pyinline.sourceforge.net/). Obviously, that ancient thing doesn't work anymore, but the idea worked then and it ought to work today. That being said, I think there are significantly superior options in existence now!

guipsp • today at 2:12 PM

This is numba?

➕ show 1 reply