Hacker News new | ask | show | jobs
by framecowbird 1618 days ago
> CPython doesn't have a just in time compiler right now, instead the interpreter is running the bytecode instructions directly.

Isn't compiling and then immediately running the code exactly what a just in time compiler is? Or do I have a misunderstanding of the term?

2 comments

CPython compiles Python source code to bytecode, but it never compiles the bytecode to machine code. Instead it interprets the bytecode, reading one instruction at a time, and basically calling a giant switch statement that handles every possible opcode.

A JIT would compile the bytecode to machine code then run it directly (at least for frequently executed code paths). There is no "switch" anymore. Each bytecode instruction has already been replaced by the corresponding machine code.

So PyPy has JIT, but not CPython, that's weird.

Is there any reason why official python doesn't have any JIT option? Would that be too fastidious to develop?

> Is there any reason why official python doesn't have any JIT option?

Desires to keep the implementation simple and approachable (relatively), as well as avoid issues of performance cliffs and such.

Also the C API has historically been extremely broad and provided large access to what amount to implementation details, making this keep working properly with a jit is difficult (at least for anything but a simplistic macro-ish JIT).

PyPy is not fully compatible with CPython. You won't have the same behaviour and CPython C API is not guaranteed to be fully compatible. So, I'm not sure that having a JIT that is fully compatible is easy.
pypy was started as an effort to make a JIT for python...when viewed in that light, it's not a weird situation at all.

as for why cpython doesn't use a jit? most likely to prevent any breakage with c modules

Most terms in language implementation are fuzzy. But just in time compilation most often refers to switching from interpreting bytecode to (generating machine code and running that) generated machine code in specific spots after having analyzed the currently running bytecode for a while.

"Classical" (again, every term is fuzzy) JIT compilers either do this machine code compilation after seeing a good candidate _entire function_ or a good candidate _section of code within a function_. Good candidates are often areas of code that are executed a large number of times and with consistent internals (e.g. iterating from 0 to 10000 with variables inside that have provably fixed types).

But there are infinite variations of JIT compilation.

In any case, CPython doesn't do that switching from bytecode to generated machine code. Pypy does do that. As does V8 and the JVM and so on.