Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing code for optimizing JIT compiler [duplicate]

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
like image 877
highBandWidth Avatar asked Dec 31 '25 14:12

highBandWidth


1 Answers

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

like image 120
TryPyPy Avatar answered Jan 03 '26 04:01

TryPyPy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!