What are we going to discuss
When you develop a Python app, what happens when you execute it
Why does that happen and how does it work?
How can it be improved?
What is a JIT
What is Pyjion
Abstract Syntax Trees
pip install –r requirements.txt
cat example.py | python astvisualizer.py
Uses built in module ast and function compile
What is ByteCode
Machine code is the lowest level but not portable between processors
Assembly is short-hand for machine code
C is compiled and linked into machine-code which is efficient but not portable
Contrast Jython to Cpython, Jython compiles the AST to JVM bytecode,
Cpython to Cpython bytecode (pyc files)
The cPython compiler reads the text (command, file etc)
Prepares Abstract Syntax Tree
The cPython compiler then converts the AST into Bytecode
Bytecode operations are low level operations that translate to likely
OS/machine level instructions.
Using DIS to inspect byte-code
2 0 LOAD_GLOBAL 0 (print)
3 LOAD_CONST 1 ('Hello world!')
6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
10 LOAD_CONST 0 (None)
Using the standard library ‘dis’
module, you can disassemble a
Python object into byte-code in
the Python CLI.
Python threads and the Global
Python has native support for threading
Python threads are real system threads (either POSIX or Windows)
Those threads are managed by the host operating system
Python threads are simple callable objects
The GIL ensures that sure each thread gets exclusive access to the interpreter
internals when it's running (and that call-outs to C extensions play nice)
How a Python thread executes
The pthread is
The thread calls
(run the callable)
Just-in-time compilation requires an intermediate
language (IL) to allow the code to be split into
chunks (or frames)
Ahead of time (AOT) compilers are designed to
ensure that, the CPU can understand every line in
the code before any interaction takes place.
In the case of Python, the execution is converted to
byte-code when called. Those byte-code statements
are cached in .pyc files.
This diagram shows the .NET MSIL to JIT compiler
Microsoft.NET is a JIT framework, with a series of languages supported by
Microsoft and interopability with the common MSIL intermediate language.
The .NET runtime is the MSIL JIT compilation module, but the executable and
installer includes 100’s of modules specific for Windows and Windows
Microsoft have redeveloped .NET into a new lightweight framework focusing
on the core elements (JIT and compilation) as well as the core data structures
and common classes. This is developed in C++ and is cross-platform.
The JIT module itself in .NET core can be used without the MSIL purely for
executing byte-code operators on the local CPU.
This is what Pyjion does.
The goal of the JIT is to convert Python Code Objects (either loaded via
imports, on the CLI or in a thread) to machine instructions.
The low level data structure is called PyCodeObject, declared in Cpython.
Pyjion introduces a new abstract type, IPythonCompiler, which takes a pointer
to a PyCodeObject and executes (compiles) the code against a target
The ILGenerator generates DotNetCore IL instructions for each Python code
End to end workflow
send to the
to machine code
ICorJitInfo is the main
interface that the JIT uses
to call back to the
execution engine (EE) and
Proposed extensions to CPython
In order to support Pyjion (or another JIT engine) some
changes are proposed to Cpython
Load pyjit.dll and run ‘jitInit’ (pylifecycle.cpp)
JitInitFunction jitinit =
Ceval (the code that evaluates the .pyc opcode cache)
can now ask the JIT module to evaluate frames
Does this help me run .NET from Python
or vice versa?
.NET is not only an intermediate-language and execution
engine, but also a huge collection of standard libraries
(similar to Python).
Pyjion bridges the gap between the low-level function
that CPython performs executing byte-code operations on
the local CPU and replaces the last step with a 3rd party
Building the project
You will need
Visual Studio (well MSBuild)
1. Download Pyjion (github.com/Microsoft/Pyjion)
2. Install submodules
3. Apply patches to CoreCLR and Python
4. Compile Pyjion
5. Copy build output to the CPython distribution output
Head of Innovation at Dimension Data