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
Ad

Recommended

PDF
A Better Python for the JVM
Tobias Lindaaker
 
PDF
Python Flavors
Geison Goes
 
PDF
PyPy 1.2: snakes never crawled so fast
PyCon Italia
 
PPTX
How to integrate python into a scala stack
Fliptop
 
PPT
A Real Future for Innovation in Silicon IP Business Models Using Open Source ...
Design And Reuse
 
PDF
180 nm Tape out experience using Open POWER ISA
Ganesan Narayanasamy
 
PPTX
Hdl simulators
Haribabu Kannuri
 
PPT
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
Jerry Chou
 
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
 
PDF
Understanding PyPy - PyConEs 14
fcofdezc
 
PPTX
A deep dive into python and it's position in the programming landscape.pptx
Murugan Murugan
 

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)

PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
Edge AI and Vision Alliance
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Practical Applications of AI in Local Government
OnBoard
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
Edge AI and Vision Alliance
 

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)