Search code examples
pythoncompilationjitpypyoptimization

Guidelines to write fast code for PyPy's JIT


PyPy's JIT can make Python code execute much faster than CPython. Are there a set of guidelines for writing code that can be optimised better by the JIT compiler? For example, Cython can compile some static code into C++, and it has guidelines to write efficient code. Are there a set of good practices for PyPy? I know that the PyPy project has guidelines for including hints while writing your own JIT-enabled interpreters for other dynamic languages, but that is not relevant to most end users of the framework, who are simply using the interpreter. Questions I am wondering about include:

  1. Packaging a script into functions
  2. Explicitly deleting variables
  3. Possible ways of giving, or hinting variable types
  4. Writing loops a certain way

Solution

  • PyPy wiki's at BitBucket has a section on JIT Friendliness. Some blog posts offer further advice on making code run fast in PyPy, but AFAIK the idea is that idiomatic code that doesn't force interpreting/realizing frames should be fast and is a bug if it isn't.

    I know that for 3, some "assert x > 0" or similar statements can be useful, but I don't remember where I saw that. I also believe I've seen some suggestion about refactoring conditional-paths-in-loops related to 4 (edit: this seems to be outdated now).

    Here's a thread with some related discussion. You can check how well the JIT is working with your code with jitviewer, but it's somewhat advanced. Joining #pypy on Freenode will get you help with jitviewer and your particular code.

    2020+

    Since Pypy moved to Heptapod in 2020, the JIT Friendliness has moved here: https://foss.heptapod.net/pypy/pypy/-/wikis/JitFriendliness

    Additional performance info is available here: https://www.pypy.org/performance.html