SlideShare a Scribd company logo
1 of 16
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

Py Con 2009 Pumping Iron Into Python
Py Con 2009   Pumping Iron Into PythonPy Con 2009   Pumping Iron Into Python
Py Con 2009 Pumping Iron Into PythonSarah Dutkiewicz
 
At Last an OCL Debugger
At Last an OCL DebuggerAt Last an OCL Debugger
At Last an OCL DebuggerEdward Willink
 
Introduction to Python Programing
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programingsameer patil
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPyKai Aras
 
Orion RESTful git API
Orion RESTful git APIOrion RESTful git API
Orion RESTful git APITomasz Zarna
 
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZEJeff Squyres
 
Python Programming
Python ProgrammingPython Programming
Python Programmingsameer patil
 
Introduction to Kotlin for Java developer
Introduction to Kotlin for Java developerIntroduction to Kotlin for Java developer
Introduction to Kotlin for Java developerShuhei Shogen
 
PyPy
PyPyPyPy
PyPyESUG
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Carlos Miguel Ferreira
 
Jython: Integrating Python and Java
Jython: Integrating Python and JavaJython: Integrating Python and Java
Jython: Integrating Python and JavaCharles Anderson
 
Porting To Symbian
Porting To SymbianPorting To Symbian
Porting To SymbianMark Wilcox
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimeNational Cheng Kung University
 

What's hot (20)

Open POWER Cores and ISA
Open POWER Cores and ISAOpen POWER Cores and ISA
Open POWER Cores and ISA
 
Py Con 2009 Pumping Iron Into Python
Py Con 2009   Pumping Iron Into PythonPy Con 2009   Pumping Iron Into Python
Py Con 2009 Pumping Iron Into Python
 
Mixing Python and Java
Mixing Python and JavaMixing Python and Java
Mixing Python and Java
 
Kotlin
KotlinKotlin
Kotlin
 
At Last an OCL Debugger
At Last an OCL DebuggerAt Last an OCL Debugger
At Last an OCL Debugger
 
Introduction to Python Programing
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programing
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPy
 
Orion RESTful git API
Orion RESTful git APIOrion RESTful git API
Orion RESTful git API
 
An Introduction to PyPy
An Introduction to PyPyAn Introduction to PyPy
An Introduction to PyPy
 
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
(Very) Loose proposal to revamp MPI_INIT and MPI_FINALIZE
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Introduction to Kotlin for Java developer
Introduction to Kotlin for Java developerIntroduction to Kotlin for Java developer
Introduction to Kotlin for Java developer
 
PyPy
PyPyPyPy
PyPy
 
OpenPOWER ADG key note
OpenPOWER ADG key note OpenPOWER ADG key note
OpenPOWER ADG key note
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
Jython: Integrating Python and Java
Jython: Integrating Python and JavaJython: Integrating Python and Java
Jython: Integrating Python and Java
 
Kotlin
KotlinKotlin
Kotlin
 
Porting To Symbian
Porting To SymbianPorting To Symbian
Porting To Symbian
 
Eclipse OCL Summary
Eclipse OCL SummaryEclipse OCL Summary
Eclipse OCL Summary
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtime
 

Similar to Pyjion - a JIT extension system for CPython

Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineYusuke Izawa
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter NotebooksIntroduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter NotebooksEueung Mulyana
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthingtonoscon2007
 
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bugWang Hao Lee
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfVisionAcademyProfSac
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfbhagyashri686896
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfrupaliakhute
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfsannykhopade
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes rajaniraut
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsHenry Schreiner
 
PyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanPyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanAyan Pahwa
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSCisco Russia
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughgabriellekuruvilla
 
Find your own iOS kernel bug
Find your own iOS kernel bugFind your own iOS kernel bug
Find your own iOS kernel bugGustavo Martinez
 

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

Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter NotebooksIntroduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter Notebooks
 
Python Interpreter.pdf
Python Interpreter.pdfPython Interpreter.pdf
Python Interpreter.pdf
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthington
 
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
 
Python PPT1.pdf
Python PPT1.pdfPython PPT1.pdf
Python PPT1.pdf
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
Chapter - 1.pptx
Chapter - 1.pptxChapter - 1.pptx
Chapter - 1.pptx
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
PyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanPyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_Ayan
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
Pi Is For Python
Pi Is For PythonPi Is For Python
Pi Is For Python
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Find your own iOS kernel bug
Find your own iOS kernel bugFind your own iOS kernel bug
Find your own iOS kernel bug
 

More from Anthony Shaw

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

More from Anthony Shaw (6)

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

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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)