Skip to main content

A JIT compiler wrapper for CPython

Project description

Pyjion, a JIT extension for CPython that compiles your Python code into native CIL and executes it using the .NET 5 CLR.

Documentation Status PyPI

Installing

$ pip install pyjion

Compiling from source

Prerequisites:

  • CPython 3.9.0
  • CMake 3.2 +
  • .NET 5
 $ git clone git@github.com:tonybaloney/pyjion --recurse-submodules
 $ cd pyjion
 $ python -m pip install .

Using Pyjion

To get started, you need to have .NET 5 installed, with Python 3.9 and the Pyjion package (I also recommend using a virtual environment).

After importing pyjion, enable it by calling pyjion.enable() which sets a compilation threshold to 0 (the code only needs to be run once to be compiled by the JIT):

>>> import pyjion
>>> pyjion.enable()

Any Python code you define or import after enabling pyjion will be JIT compiled. You don't need to execute functions in any special API, its completely transparent:

>>> def half(x):
...    return x/2
>>> half(2)
1.0

Pyjion will have compiled the half function into machine code on-the-fly and stored a cached version of that compiled function inside the function object. You can see some basic stats by running pyjion.info(f), where f is the function object:

>>> pyjion.info(half)
{'failed': False, 'compiled': True, 'run_count': 1}

You can see the machine code for the compiled function by disassembling it in the Python REPL. Pyjion has essentially compiled your small Python function into a small, standalone application. Install distorm3 first to disassemble x86-64 assembly and run pyjion.dis.dis_native(f):

>>> import pyjion.dis
>>> pyjion.dis.dis_native(half)
00000000: PUSH RBP
00000001: MOV RBP, RSP
00000004: PUSH R14
00000006: PUSH RBX
00000007: MOV RBX, RSI
0000000a: MOV R14, [RDI+0x40]
0000000e: CALL 0x1b34
00000013: CMP DWORD [RAX+0x30], 0x0
00000017: JZ 0x31
00000019: CMP QWORD [RAX+0x40], 0x0
0000001e: JZ 0x31
00000020: MOV RDI, RAX
00000023: MOV RSI, RBX
00000026: XOR EDX, EDX
00000028: POP RBX
00000029: POP R14
...

The complex logic of converting a portable instruction set into low-level machine instructions is done by .NET's CLR JIT compiler.

All Python code executed after the JIT is enabled will be compiled into native machine code at runtime and cached on disk. For example, to enable the JIT on a simple app.py for a Flask web app:

from src import pyjion
pyjion.enable()

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

app.run()

FAQ

How do you pronounce "Pyjion"?

Like the word "pigeon". @DinoV wanted a name that had something with "Python" -- the "Py" part -- and something with "JIT" -- the "JI" part -- and have it be pronounceable.

How do this compare to ...

PyPy?

PyPy is an implementation of Python with its own JIT. The biggest difference compared to Pyjion is that PyPy doesn't support all C extension modules without modification unless they use CFFI or work with the select subset of CPython's C API that PyPy does support. Pyjion also aims to support many JIT compilers while PyPy only supports their custom JIT compiler.

Pyston?

Pyston is an implementation of Python using LLVM as a JIT compiler. Compared to Pyjion, Pyston has partial CPython C API support but not complete support. Pyston also only supports LLVM as a JIT compiler.

Numba?

Numba is a JIT compiler for "array-oriented and math-heavy Python code". This means that Numba is focused on scientific computing while Pyjion tries to optimize all Python code. Numba also only supports LLVM.

IronPython?

IronPython is an implementation of Python that is implemented using .NET. While IronPython tries to be usable from within .NET, Pyjion does not have a compatibility story with .NET. This also means IronPython cannot use C extension modules while Pyjion can.

Psyco?

Psyco was a module that monkeypatched CPython to add a custom JIT compiler. Pyjion wants to introduce a proper C API for adding a JIT compiler to CPython instead of monkeypatching it. It should be noted the creator of Psyco went on to be one of the co-founders of PyPy.

Unladen Swallow?

Unladen Swallow was an attempt to make LLVM be a JIT compiler for CPython. Unfortunately the project lost funding before finishing their work after having to spend a large amount of time fixing issues in LLVM's JIT compiler (which has greatly improved over the subsequent years).

Nuitka and Shedskin?

Both Nuitka and Shedskin are Python-to-C++ transpilers, which means they translate Python code into equivalent C++ code. Being a JIT, Pyjion is not a transpiler.

Will this ever ship with CPython?

Goal #1 is explicitly to add a C API to CPython to support JIT compilers. There is no expectation, though, to ship a JIT compiler with CPython. This is because CPython compiles with nothing more than a C89 compiler, which allows it to run on many platforms. But adding a JIT compiler to CPython would immediately limit it to only the platforms that the JIT supports.

Does this help with using CPython w/ .NET or UWP?

No.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Release notes

0.8.0

  • Enhanced the process stage of the compiler with new abstract types, iterable, bytearray, codeobject, frozenset, enumerator, file, type and module
  • Process stage will assert the abstract return type of any call to a builtin function (e.g. list(), tuple()), which will kick in the optimizations for a broader set of scenarios
  • Added OPT-8 (OPTIMIZE_BINARY_FUNCTIONS) to combine 2 sequential binary operations into a single operation. Adds about 15-20% performance gain on PyFloat operations.
  • Added OPT-9 (OPTIMIZE_ITERATORS) to inline the FOR_ITER opcode of a listiter (List iterator) into native assembly instructions.
  • Added OPT-10 (OPTIMIZE_HASHED_NAMES) to precompute the hashes for LOAD_NAME and LOAD_GLOBAL dictionary lookups
  • Fixed a bug where looking up a known hash for a dictionary object (optimized BINARY_SUBSCR) wouldn't raise a KeyError. Seen in #157

0.7.0

  • Fixed a bug in JUMP_IF_FALSE_OR_POP/JUMP_IF_TRUE_OR_POP opcodes emitting a stack growth, which would cause a stack underflow on subsequent branch checks. JIT will compile a broader range of functions now
  • Implemented PEP590 vector calls for methods with 10+ arguments (thanks @tetsuo-cpp)
  • Implemented PEP590 vector calls for functions with 10+ arguments
  • Fixed a reference leak on method calls with large number of arguments
  • Support for tracing of function calls with 10+ arguments
  • Disabled OPT-4 as it is causing reference leaks

0.6.0

  • Added OPT-6 optimization. Frame constants are now used to speed up assignments to lists and dictionaries. STORE_SUBSCR will assert if something is a list, or dict and shortcut the assignment logic.
  • Added OPT-7 optimization. The binary subscript operator is compiled to faster path under a set of circumstances, especially if the index/key is a frame constant. Hashes are precomputed and indexes for integer constants are converted to native numbers at compile-time.
  • The native machine-code disassembler will show the actual position of the JITed code in memory, instead of starting the offset at 0
  • The pyjion.dump_native() function returns a tuple with bytes, length and position
  • Type inferencing has been improved for all inplace and binary operations
  • Windows builds from source are fixed for when the user wants to compile against a checkout of .NET
  • Implemented FAST_DISPATCH for additional opcodes
  • Added a test runner for the CPython regression suite that tests the JIT in isolation
  • Fixed a reference leak of (self) for the LOAD_METHOD opcode
  • Fixed a reference leak of non C functions being called via Call (CALL_FUNCTION)
  • Fixed a bug where (very) large tuples being created via the BUILD_TUPLE opcode would cause an overflow error
  • Fixed a bug on BUILD_MAP being called with very large dictionaries caused a fatal error

0.5.0

  • Added OPT-4 optimization. Frame locals (named variables known at compilation) using the LOAD_FAST, STORE_FAST and DELETE_FAST opcodes will use native .NET locals instead of using the frame's f_localsplus array.
  • Improved performance in LOAD_FAST and STORE_FAST through OPT-4
  • Added OPT-5 optimization. Frame push/pop on entry/exit are now inline CIL instructions.
  • LOAD_FAST skips unbound local checks when proceeded by a STORE_FAST (i.e. slot is definitely assigned)

0.4.0

  • Fixed a crash bug where CPython checks recursion depth from ceval state, which may not be set
  • Implemented a faster check for recursion depth
  • Fixed a bug on LOAD_CLOSURE operator not being set
  • Fixed OPT-2 on Windows and Linux
  • Fixed a bug where the wrong CIL opcode was being used to subtract values, would throw an overflow error and fail back into EFD.
  • Implemented the .NET EE exception handlers for guard stack canaries, overflow errors, and null reference exceptions
  • Implemented a more efficient case of ld_i(1)
  • Corrected cases of ob_refcnt to use 64-bit signed integers
  • No longer print error messages on release code for unimplemented .NET EE methods
  • Fixed a bug on the incorrect vtable relative field being set
  • Fixed a bug where tracing and profiling would be emitted even when not explicitly enabled
  • .NET Exceptions are transferred into Python exceptions at runtime

0.3.0

  • Added an optimization (OPT-1/OPTIMIZE_IS) to inline the "is"/ "is not" statement into a simple pointer comparison with jump statement. Compiles to inline machine code instead of a method call
  • Added an optimization (OPT-2/OPTIMIZE_DECREF) to decrement the refcount without a method call, when the object refcount is >1 and then call _Py_dealloc if the ref count becomes 0. Replaces the previous method call
  • Windows now uses the system page size instead of the default value of 1MB

0.2.1

  • Added support for .NET 5.0.1
  • Implemented a CIL modulus emitter

0.2.0

  • Added support for profiling compiled functions by enabling profiling (pyjion.enable_profiling())
  • Added support for profiling C function calls, returns and exceptions
  • Implemented a faster call path for functions and methods for 5-10 arguments
  • Fixed a bug where the page size defaulted to 0 in the .NET EE, which caused a failed assertion (and fails to compile the function), would fix a large % of functions that previously failed to compile

0.1.0

  • Added support for debugging compiled functions and modules by enabling tracing (pyjion.enable_tracing())
  • Added support for debugging to catch unhandled/handled exceptions at runtime when tracing is enabled
  • Added support for opcode-level tracing
  • Fixed a bug on executing Pyjion with pydevd (VScode/PyCharm debugger) would cause the Python process to crash because of a doubly-freed code object (#7)

0.0.7

  • Added a WSGI middleware function to enable Pyjion for Flask and Django (#67)
  • Fix a bug on dictionary merging for mapping types incorrectly raising a type error (#66)

0.0.6

  • Implemented supported for disassembling "large" methods into CIL (#27)
  • Added type stubs for the pyjion C extension
  • Fix a bug where merging or updating a subclassed dictionary would fail with a type error. (#28)

0.0.5

  • Fixed a critical bug where method calls with large numbers of arguments, and the argument was a tuple could cause a segmentation fault on GC collection.
  • Tested support for IPython REPL
  • Fixed a bug where importing pyjion.dis after enabling the JIT would cause a stack overflow
  • Has around 50% chance of working and not causing your computer to explode, or worse, segmentation fault

0.0.4

  • Added a stack probe helper for Linux (will use JIT in more scenarios)
  • Enabled support for running unit tests in Linux
  • Fixed a bug where JIT would crash when a method call failed because of a bad-lookup
  • Implemented helper method redirection for Linux to support PIC compiled symbols
  • Has around 35% chance of working and not causing your computer to explode, or worse, segmentation fault
  • Improved discovery of .NET libraries on Linux
  • Fixed a bug where a garble-named log file would be generated (should be JIT timings log)

0.0.3

  • Installable bdist_wheel for Ubuntu, Debian, macOS 10.15, 11 (10.16) and Windows x64
  • Installable manylinux2014 wheel with clrjit.so bundled in
  • Added multithreading/multiprocessing support
  • Fixed a bug where the wheel would be broken if there are two distributions of Python 3.9 on the system
  • Has around 30% chance of working and not causing your computer to explode, or worse, segmentation fault.

0.0.2

  • Installable source distribution support for macOS, Windows and (barely) Linux.

0.0.1

  • It compiles on my machine

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjion-0.8.0.tar.gz (122.0 kB view details)

Uploaded Source

Built Distributions

pyjion-0.8.0-cp39-cp39-win_amd64.whl (271.4 kB view details)

Uploaded CPython 3.9Windows x86-64

pyjion-0.8.0-cp39-cp39-manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9

pyjion-0.8.0-cp39-cp39-macosx_11_1_x86_64.whl (284.7 kB view details)

Uploaded CPython 3.9macOS 11.1+ x86-64

pyjion-0.8.0-cp39-cp39-macosx_10_16_x86_64.whl (284.7 kB view details)

Uploaded CPython 3.9macOS 10.16+ x86-64

pyjion-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl (284.7 kB view details)

Uploaded CPython 3.9macOS 10.15+ x86-64

File details

Details for the file pyjion-0.8.0.tar.gz.

File metadata

  • Download URL: pyjion-0.8.0.tar.gz
  • Upload date:
  • Size: 122.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0.tar.gz
Algorithm Hash digest
SHA256 babc17430c0da77d964b06d776a6933f260eca56e0549293b12461cea0d5b4e1
MD5 ef65b996b61dc62a8b7324930992163e
BLAKE2b-256 f090a78ce32f1833240cdab1fb0c714e62f1adcc641e5dbfc3c4cf8a69190f48

See more details on using hashes here.

File details

Details for the file pyjion-0.8.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pyjion-0.8.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 271.4 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 aadad4e2c4fd006563e5fa3765a12d76331a4359a4bdd1d4a6408084438290cc
MD5 e64a13afd1be2ab2bcca1f1093265d03
BLAKE2b-256 a9375ae92c298c7e5171088917d5d2b5a5b4928cba6a6872892eb25bdf0f0e56

See more details on using hashes here.

File details

Details for the file pyjion-0.8.0-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: pyjion-0.8.0-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 79bb6e6ddcf59386378a383ad8f4bea875e8dafb41237f3c8fb32f55fc453f88
MD5 f1c8358d2111bef5d50babef87306d67
BLAKE2b-256 c56f089f8572fce99c560ad8a4e5eee577b4ff2ff81685db9f6b450e902551f5

See more details on using hashes here.

File details

Details for the file pyjion-0.8.0-cp39-cp39-macosx_11_1_x86_64.whl.

File metadata

  • Download URL: pyjion-0.8.0-cp39-cp39-macosx_11_1_x86_64.whl
  • Upload date:
  • Size: 284.7 kB
  • Tags: CPython 3.9, macOS 11.1+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0-cp39-cp39-macosx_11_1_x86_64.whl
Algorithm Hash digest
SHA256 91a25344913f4c2d6660ef0123a4b2e4dd91355c6f32e7a4a29d4d6aaf12fd80
MD5 870b74059dbf689b6a18a70ee2ee7f3e
BLAKE2b-256 52507c97275d5bf3359e528c93af7c259c521a4ee7f3c400ae4c782b1a8fe8c7

See more details on using hashes here.

File details

Details for the file pyjion-0.8.0-cp39-cp39-macosx_10_16_x86_64.whl.

File metadata

  • Download URL: pyjion-0.8.0-cp39-cp39-macosx_10_16_x86_64.whl
  • Upload date:
  • Size: 284.7 kB
  • Tags: CPython 3.9, macOS 10.16+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm Hash digest
SHA256 3d7bf342b7ea220f43981bf0c04cc43cf18973b8cb58c37645ddacfdcfcd924a
MD5 19e2101a5360294588c43926a671d9f4
BLAKE2b-256 b07cb25bb6dfc039b2851039eedcabc651404c2ed60ea1fa30f4192f02115864

See more details on using hashes here.

File details

Details for the file pyjion-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: pyjion-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 284.7 kB
  • Tags: CPython 3.9, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.1

File hashes

Hashes for pyjion-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 c02300a61bdf9c28d0d24b112e85c4df658b692af4ae3e28a6e2f8e05fbb25ef
MD5 c275f1267de2205acc00941ad1ac1895
BLAKE2b-256 057d2574f597505da791f7be49c8b5d38ff64765b49b5fa064d89cd3c4131d6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page