Hacker News new | ask | show | jobs
by sumitgt 3541 days ago
I am new to the python ecosystem. I use python for solving problems and hackerrank and noticed that switching from python3 to pypy3 can mean the difference betwwen timing out on some problems v/s getting it to work.

Can someone eli5 how pypy achieves such difference and why that improvement cannot be contributed back to python3?

2 comments

PyPy uses a JIT (just-in-time) compiler, as opposed to the static interpreter of CPython (the default/canonical implementation of Python from python.org). What this means is that the PyPy interpreter uses runtime information about your program to compile your Python code into very efficient machine code on the fly. Because this compilation is done at runtime, PyPy has lots of data about things like variable types, how often each function gets called, etc, and it can use that data to generate really fast code in many cases.

PyPy is a completely different implementation of Python, so it's impossible to simply contribute improvements from it to CPython. To answer the question I think you're asking, though, the main reason we can't just have PyPy replace CPython as the default implementation isn't strictly technical: The Python community (Guido in particular) has decided to keep the reference implementation (CPython) as simple and consistent as possible, so that it's easy to contribute to and doesn't have any surprising behavior. PyPy, by comparison, is an incredibly complex piece of software. Even if you ignore the fact that it would make contributions harder, its performance is, in many scenarios, actually significantly worse than CPython's, because its optimizations rely on specific patterns within your code. I think it's mostly a good thing that PyPy is its own project and not the default implementation of Python, because it allows Python (the language) to grow much faster than it would otherwise.

I hope that answers your question. I apologize to anyone who knows more about the details than I do if I over-simplified things. :)

The complexity of the JIT isn't in pypy's implementation of python. This is probably the most important part of the pypy project: it's a python implementation written in a simple subset of python plus some standard hints, and an independent piece that generates a JIT for things written in that subset.
PyPy is a different implementation of a python interpreter, it includes a JIT to rewrite hot code into assembler. See www.pypy.org for more info. The JIT cannot be tacked on to CPython (what you call python3) since it is part of the interpreter.
The jit is _generated_ from the interpreter; you can actually run pypy's interpreter directly from cpython, it's just slower.