开发者

Guidelines to write fast code for PyPy's JIT

开发者 https://www.devze.com 2023-02-17 05:09 出处:网络
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

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 g开发者_运维问答uidelines 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消