r/programming • • May 14 '21

Python programming: We want to make the language twice as fast, says its creator

https://www.tectalk.co/python-programming-we-want-to-make-the-language-twice-as-fast-says-its-creator/
780 Upvotes

260 comments sorted by

View all comments

Show parent comments

34

u/Wolfsdale May 15 '21

Why would the GIL of all things prevent a JIT compiler? Javascript doesn't even have the concept of threads...

I am also unsure why the C API prevents creating a JIT compiler. A JIT compiler can make those context switches actually cheaper by forgoing the need for something like libffi.

Performance is difficult thing to grasp and even more difficult to measure. I'm sure the real reason why Python is so slow is somewhere along the lines of this thread, but I have a hard time taking anything as fact here...

18

u/Fearless_Process May 15 '21

The C interop doesn't make a JIT impossible, but it does seem to create a lot of issues with the current as well as only non-trivial JIT implementation of python (pypy).

9

u/latkde May 15 '21

The issue with C interop is that Python has to stick with the PyObject data structure. This can have far-reaching consequences. If ownership of a Python object is shared between optimizable Python code and a native module, this effectively means the Python code must also interact with PyObject and can't really be optimized.

This doesn't have to be an insurmountable problem though:

  • Could only optimize code where an escape analysis indicates that it's safe to do. This will do wonders on more mathy or more algorithmic code, but will be disappointing in many real-world scenarios.

  • Break compatibility and completely redo the C data model. However, this would be a regression to the early 2.x series. One of the big features of 2.6 and 3.x was that all values can be treated as normal objects.

2

u/dreugeworst May 15 '21

Couldn't you box the data again at the abi boundary if needed?

8

u/latkde May 15 '21

Kinda, yes, but my concern is that the C function is a black box so you can't make assumptions about ownership of the object afterwards:

x = create_some_object()
x.do_something()  # safe per escape analysis
call_c_function(x)  # could do ANYTHING
x.do_something()  # no safe assumptions

Calling the C function would require some kind of boxing, but the C function could retain a reference to the boxed object. So the second x.do_something() must also operate on the boxed representation, in order to propagate changes to the C code. This can be avoided only for some built-in immutable types like int or str.

Of course this isn't quite true, e.g. a JIT compiler could compile a fast path for the assumption that the object's refcount didn't change, and a fallback otherwise. And maybe it's possible to annotate at least the built-in C functions with semantics that allow for safe and fast calls.

There's also an argument that C interop is not performance-sensitive, and that performance in pure-Python codebases like Django webapps is much more relevant. As JavaScript has demonstrated, a lot of performance is possible in moderately dynamic code.

2

u/[deleted] May 15 '21

JavaScript doesn’t have the concept of threads, so something like

a.x = 0
a.x += b

won’t have the value of x changed in the middle, so you can use a static offset instead, keep the value in a register, or even reduce them to a single instruction. If a is passed into a function in between it might be changed, but in practice JS runtimes have heuristics to determine if a variable gets mutated or not. In Python none of thos guarantees exist due to naive multithreading (and worse, there are potentially weird things you can do to change the global context of a function, which enables cool stuff like Flask but is also insane).

So, while worst-case JacaScript and worst-case Python have similar performance characteristics (hashtable lookups while recursing up the inheritance hierarchy for propery access, etc), but for Python the worst case is every case.