SlideShare a Scribd company logo
Pyjion
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
 Clone https://github.com/quantifiedcode/python-ast-visualizer
 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
once compiled
 Contrast Jython to Cpython, Jython compiles the AST to JVM bytecode,
Cpython to Cpython bytecode (pyc files)
cPython bytecode
 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
def helloworld():
print("Hello world!")
dis.dis(helloworld)
2 0 LOAD_GLOBAL 0 (print)
3 LOAD_CONST 1 ('Hello world!')
6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
Using the standard library ‘dis’
module, you can disassemble a
Python object into byte-code in
the Python CLI.
https://github.com/python/cpython/blob/ab8b3ff250a1
3d506f572abd66c37bce0759ab7d/Lib/dis.py#L31-L65
Python threads and the Global
Interpreter Lock
 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)
 See: http://www.dabeaz.com/python/GIL.pdf
How a Python thread executes
Thread created
Python creates
data structure
(PyThreadState)
for thread
The pthread is
launched
The thread calls
PyEval_CallObject
(run the callable)
Just-in-time compilation
 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
flow
.NET core
 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
application development.
 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.
Python CodeObjects
 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
ILGenerator
 The ILGenerator generates DotNetCore IL instructions for each Python code
End to end workflow
Python creates
PyCodeObject
from code
file/input
PythonCompiler
takes
PyCodeObject
and converts
Python OpCodes
into
intermediate
instructions
(Pyjion IL?)
ILGenerator
converts
abstract IL
operations to
send to the
ICorJitCompiler
ICorJitCompiler
compiles stacks
to machine code
ICorJitInfo is the main
interface that the JIT uses
to call back to the
execution engine (EE) and
get information.
DotNetCorePyjionPython
print(‘hello world!’)
010101110011
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 =
(JitInitFunction)GetProcAddress(pyjit,
"JitInit"); 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?
 No.
 .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
execution engine.
Building the project
 You will need
 Git, SVN
 Cmake
 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
(pyjit.dll)
About me
@anthonypjshaw
@tonybaloney
anthonyshaw@apache.org
Head of Innovation at Dimension Data

More Related Content

What's hot (20)

PPTX
Open POWER Cores and ISA
Ganesan Narayanasamy
 
PPT
Py Con 2009 Pumping Iron Into Python
Sarah Dutkiewicz
 
PPT
Mixing Python and Java
Andreas Schreiber
 
PDF
Kotlin
Nodirbek Usmanov
 
ODP
At Last an OCL Debugger
Edward Willink
 
PPTX
Introduction to Python Programing
sameer patil
 
PPT
a quick Introduction to PyPy
Kai Aras
 
PDF
Orion RESTful git API
Tomasz Zarna
 
ZIP
An Introduction to PyPy
Michael Hudson-Doyle
 
PPTX
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
Jeff Squyres
 
PPTX
Python Programming
sameer patil
 
PDF
Introduction to Kotlin for Java developer
Shuhei Shogen
 
PDF
PyPy
ESUG
 
PDF
OpenPOWER ADG key note
Ganesan Narayanasamy
 
PDF
Python 3.5: An agile, general-purpose development language.
Carlos Miguel Ferreira
 
PDF
Jython: Integrating Python and Java
Charles Anderson
 
PPT
Porting To Symbian
Mark Wilcox
 
ODP
Eclipse OCL Summary
Edward Willink
 
PDF
PyPy's approach to construct domain-specific language runtime
National Cheng Kung University
 
Open POWER Cores and ISA
Ganesan Narayanasamy
 
Py Con 2009 Pumping Iron Into Python
Sarah Dutkiewicz
 
Mixing Python and Java
Andreas Schreiber
 
At Last an OCL Debugger
Edward Willink
 
Introduction to Python Programing
sameer patil
 
a quick Introduction to PyPy
Kai Aras
 
Orion RESTful git API
Tomasz Zarna
 
An Introduction to PyPy
Michael Hudson-Doyle
 
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
Jeff Squyres
 
Python Programming
sameer patil
 
Introduction to Kotlin for Java developer
Shuhei Shogen
 
PyPy
ESUG
 
OpenPOWER ADG key note
Ganesan Narayanasamy
 
Python 3.5: An agile, general-purpose development language.
Carlos Miguel Ferreira
 
Jython: Integrating Python and Java
Charles Anderson
 
Porting To Symbian
Mark Wilcox
 
Eclipse OCL Summary
Edward Willink
 
PyPy's approach to construct domain-specific language runtime
National Cheng Kung University
 

Similar to Pyjion - a JIT extension system for CPython (20)

PDF
Understanding PyPy - PyConEs 14
fcofdezc
 
PPTX
A deep dive into python and it's position in the programming landscape.pptx
Murugan Murugan
 
DOCX
Python introduction
Learnbay Datascience
 
PPTX
Introducing-the-Python-Interpreter. What is Python Interpreter
SHASHIKANT346021
 
PPTX
python unit2.pptx
GEETHAS668001
 
PDF
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
PDF
Python Introduction
Learnbay Datascience
 
PPTX
Introduction to Python.Net
Stefan Schukat
 
PPTX
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
PPTX
IPT 2.pptx
CHRISPay4
 
PPTX
What is python
faizrashid1995
 
PPTX
Python programming
Megha V
 
PPTX
Introduction to Python Programming
Akhil Kaushik
 
PDF
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 
PPT
A-overview.pptbb cpp introduction how cpp
compengwaelalahmar
 
PPT
Python Introduction
Mohammad Javad Beheshtian
 
PPTX
Python for dummies
Roberto Stefanetti
 
PDF
session5-Getting stated with Python.pdf
AyushDutta32
 
PPT
Python_basics_tuples_sets_lists_control_loops.ppt
VGaneshKarthikeyan
 
PPTX
Introduction to the Python
BMS Institute of Technology and Management
 
Understanding PyPy - PyConEs 14
fcofdezc
 
A deep dive into python and it's position in the programming landscape.pptx
Murugan Murugan
 
Python introduction
Learnbay Datascience
 
Introducing-the-Python-Interpreter. What is Python Interpreter
SHASHIKANT346021
 
python unit2.pptx
GEETHAS668001
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
Python Introduction
Learnbay Datascience
 
Introduction to Python.Net
Stefan Schukat
 
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
IPT 2.pptx
CHRISPay4
 
What is python
faizrashid1995
 
Python programming
Megha V
 
Introduction to Python Programming
Akhil Kaushik
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 
A-overview.pptbb cpp introduction how cpp
compengwaelalahmar
 
Python Introduction
Mohammad Javad Beheshtian
 
Python for dummies
Roberto Stefanetti
 
session5-Getting stated with Python.pdf
AyushDutta32
 
Python_basics_tuples_sets_lists_control_loops.ppt
VGaneshKarthikeyan
 
Introduction to the Python
BMS Institute of Technology and Management
 
Ad

More from Anthony Shaw (6)

PPTX
I can count to 1023
Anthony Shaw
 
PPTX
What's new in Python 3.11
Anthony Shaw
 
PPTX
Python 3.8 and Beyond
Anthony Shaw
 
PPTX
Docker and Containers in the Cloud
Anthony Shaw
 
PPTX
Introducing container as-a-service support to apache libcloud
Anthony Shaw
 
PPTX
Apache LibCloud - Keeping up with the cloud market in 2016
Anthony Shaw
 
I can count to 1023
Anthony Shaw
 
What's new in Python 3.11
Anthony Shaw
 
Python 3.8 and Beyond
Anthony Shaw
 
Docker and Containers in the Cloud
Anthony Shaw
 
Introducing container as-a-service support to apache libcloud
Anthony Shaw
 
Apache LibCloud - Keeping up with the cloud market in 2016
Anthony Shaw
 
Ad

Recently uploaded (20)

PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Python basic programing language for automation
DanialHabibi2
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 

Pyjion - a JIT extension system for CPython

  • 2. 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
  • 3. Abstract Syntax Trees  Clone https://github.com/quantifiedcode/python-ast-visualizer  pip install –r requirements.txt  cat example.py | python astvisualizer.py  Uses built in module ast and function compile
  • 4. 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 once compiled  Contrast Jython to Cpython, Jython compiles the AST to JVM bytecode, Cpython to Cpython bytecode (pyc files)
  • 5. cPython bytecode  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.
  • 6. Using DIS to inspect byte-code def helloworld(): print("Hello world!") dis.dis(helloworld) 2 0 LOAD_GLOBAL 0 (print) 3 LOAD_CONST 1 ('Hello world!') 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 9 POP_TOP 10 LOAD_CONST 0 (None) 13 RETURN_VALUE Using the standard library ‘dis’ module, you can disassemble a Python object into byte-code in the Python CLI. https://github.com/python/cpython/blob/ab8b3ff250a1 3d506f572abd66c37bce0759ab7d/Lib/dis.py#L31-L65
  • 7. Python threads and the Global Interpreter Lock  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)  See: http://www.dabeaz.com/python/GIL.pdf
  • 8. How a Python thread executes Thread created Python creates data structure (PyThreadState) for thread The pthread is launched The thread calls PyEval_CallObject (run the callable)
  • 9. Just-in-time compilation  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 flow
  • 10. .NET core  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 application development.  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.
  • 11. Python CodeObjects  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 ILGenerator  The ILGenerator generates DotNetCore IL instructions for each Python code
  • 12. End to end workflow Python creates PyCodeObject from code file/input PythonCompiler takes PyCodeObject and converts Python OpCodes into intermediate instructions (Pyjion IL?) ILGenerator converts abstract IL operations to send to the ICorJitCompiler ICorJitCompiler compiles stacks to machine code ICorJitInfo is the main interface that the JIT uses to call back to the execution engine (EE) and get information. DotNetCorePyjionPython print(‘hello world!’) 010101110011
  • 13. 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 = (JitInitFunction)GetProcAddress(pyjit, "JitInit"); jitinit();  Ceval (the code that evaluates the .pyc opcode cache) can now ask the JIT module to evaluate frames
  • 14. Does this help me run .NET from Python or vice versa?  No.  .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 execution engine.
  • 15. Building the project  You will need  Git, SVN  Cmake  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 (pyjit.dll)