SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Downloaden Sie, um offline zu lesen
Understanding PyPy 
Francisco Fernandez Castano 
Rushmore.fm 
francisco.fernandez.castano@gmail.com @fcofdezc 
November 8, 2014 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 1 / 51
Overview 
1 Introduction 
My Personal history with PyPy 
What is PyPy? 
Why is Python slow? 
Motivation 
2 RPython 
Overview 
De
nition 
Goals 
Hello World in RPython 
Translation process 
Building Flow Graphs 
Annotation phase 
RTyper 
Backend Optimizations 
Preparation for source generation 
Overview 
3 JIT 
PyPy strategy 
4 Garbage Collection Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 2 / 51
First PyPy impressions 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 3 / 51
First PyPy impressions 
Python 2.7.3 (87 aa9de10f9ca71da9ab4a3d53e0ba176b67d086 [ PyPy 2.2.1 with GCC 4.8.3 20140624 (Red Hat 4.8.3 -1)] Type " help ", " copyright ", " credits " or " license " for >>>> 1 + 1 
2 
>>>> def f(x): return x + 1 
>>>> f(1) 
2 
>>>> 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 4 / 51
First PyPy impressions 
Ok... just Python... 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 5 / 51
Second PyPy impressions 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 6 / 51
What is PyPy? 
PyPy is a fast, compliant alternative implementation of the Python 
language (2.7.8 and 3.2.5). 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 7 / 51
Why is Python slow? 
Interpretation overhead 
Boxed arithmetic and automatic over
ow handling 
Dynamic dispatch of operations 
Dynamic lookup of methods and attributes 
Everything can change on runtime 
Extreme introspective and re
ective capabilities 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 8 / 51
Why is Python slow? 
Boxed arithmetic and automatic over
ow handling 
i = 0 
while i < 10000000: 
i = i +1 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 9 / 51
Why is Python slow? 
Dynamic dispatch of operations 
# while i < 1000000 
9 LOAD_FAST 0 (i) 
12 LOAD_CONST 2 (10000000) 
15 COMPARE_OP 0 (<) 
18 POP_JUMP_IF_FALSE 34 
# i = i + 1 
21 LOAD_FAST 0 (i) 
24 LOAD_CONST 3 (1) 
27 BINARY_ADD 
28 STORE_FAST 0 (i) 
31 JUMP_ABSOLUTE 9 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 10 / 51
Why is Python slow? 
Dynamic lookup of methods and attributes 
class MyExample ( object ): 
pass 
def foo(target , flag ): 
if flag : 
target .x = 42 
obj = MyExample () 
foo(obj , True ) 
print obj.x #=> 42 
print getattr (obj , "x") #=> 42 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 11 / 51
Why is Python slow? 
Everything can change on runtime 
def fn (): 
return 42 
def hello (): 
return 'Hi!  PyConEs !' 
def change_the_world (): 
global fn 
fn = hello 
print fn () #=> 42 
change_the_world () 
print fn () => 'Hi!  PyConEs !' 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 12 / 51
Why is Python slow? 
Everything can change on runtime 
class Dog( object ): 
def __init__ ( self ): 
self . name = 'Jandemor ' 
def talk ( self ): 
print "%s:  guau !" % self . name 
class Cat( object ): 
def __init__ ( self ): 
self . name = 'CatInstance ' 
def talk ( self ): 
print "%s:  miau !" % self . name 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 13 / 51
Why is Python slow? 
Everything can change on runtime 
my_pet = Dog () 
my_pet . talk () #=> 'Jandemor : guau !' 
my_pet . __class__ = Cat 
my_pet . talk () #=> 'Jandemor : miau !' 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 14 / 51
Why is Python slow? 
Extreme introspective and re
ective capabilities 
def fill_list ( name ): 
frame = sys. _getframe (). f_back 
lst = frame . f_locals [ name ] 
lst. append (42) 
def foo (): 
things = [] 
fill_list ('things ') 
print things #=> 42 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 15 / 51
Why is Python slow? 
CPython is a clean and maintainable software. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 16 / 51
Why is Python slow? 
Show me the numbers 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 17 / 51
Why is Python slow? 
Show me the numbers 
speed.pypy.org 
Video processing example 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 18 / 51
What is PyPy? 
PyPy is a python interpreter written in Python. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 19 / 51
What is PyPy? 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 20 / 51
What is PyPy? 
PyPy is a python interpreter written in RPython. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 21 / 51
Motivation 
The Pareto princpile: the 20% of the program will account for the 
80% of the runtime. 
The Fast path principle. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 22 / 51
De
nition 
RPython is a restricted subset of Python that is amenable to static 
analysis. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 23 / 51
Goals 
Abstract as much as possible creating Interpreters for DL. 
l * o * p 
l - language to analyze. 
o - optimize and tweak depending on dierent factors. (GC ie) 
p - being able to produce interpreter for dierent platforms. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 24 / 51
Hello World 
Mandatory Hello World. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 25 / 51
Translation process 
Complete program is imported generating control 
ow graph. 
The Annotator does type inference. 
The Rtyper uses high level types to transform into low level ones. 
Some optimizations are applied. 
Next step is prepare graphs to be translated. 
The C backend generates source
les. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 26 / 51
Translation process 
RPython source code 
Flow object space 
flow graph 
Annotator 
low level helpers 
annotated flow graph 
RTyper 
annotate low level helpers 
Low Level flow graph 
handwritten backend code 
external functions 
C code 
LLVM code 
genc 
other target 
function discovered 
need low level helper 
genllvm 
High Level Low Level 
handwritten code 
genXXX 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 27 / 51
Building Flow Graphs 
Code is NOT parsed. 
Uses code objects that de
ne the behaviour. 
Flow Graph builder using an abstract interpretation producing control 

ow graph. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 28 / 51
Control 
ow graph 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 29 / 51
Building Flow Graphs 
def f(n): 
return 3*n+2 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 30 / 51
Building Flow Graphs 
Block (v1 ): # input argument 
v2 = mul ( Constant (3) , v1) 
v3 = add (v2 , Constant (2)) 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 31 / 51
Annotation phase 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 32 / 51
Annotation phase 
Each variable that appears in the 
ow grap is annotated. 
With all possible values. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 33 / 51
Annotation phase 
SomeObject 
SomeInteger 
SomeString 
SomeChar 
SomeTuple([s1, s2..., sn]) 
SomeList 
SomeDict 
SomeInstance 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 34 / 51
RTyper 
v3 = add(v1 , v2) 
v1 - SomeInteger () 
v2 - SomeInteger () 
v3 - SomeInteger () 
Result 
v3 = int_add (v1 , v2) 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 35 / 51
Backend Optimizations 
Function inlining. 
Malloc removal. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 36 / 51
Preparation for source generation 
Exception handling, since C doesn't have that concept. 
Memory Management, GC Pluggable. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 37 / 51
Overview 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 38 / 51
Jit 
De
nition 
In computing, just-in-time compilation (JIT), also known as dynamic 
translation, is compilation done during execution of a program at run time 
rather than prior to execution. Wikipedia. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 39 / 51
Overview 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 40 / 51
PyPy strategy 
RPython generates a tracing JIT. 
Instead of user code is interpreter code who launch compiled code. 
It comes mostly for free to language implementators. 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 41 / 51
Control 
ow graph 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 42 / 51
tlrjitdriver = JitDriver ( greens = ['pc ', 'bytecode '], 
reds = ['a', 'regs ']) 
def interpret ( bytecode , a): 
regs = [0] * 256 
pc = 0 
while True : 
tlrjitdriver . jit_merge_point () 
opcode = ord ( bytecode [pc ]) 
pc += 1 
if opcode == JUMP_IF_A : 
target = ord ( bytecode [pc ]) 
pc += 1 
if a: 
if target  pc: 
tlrjitdriver . can_enter_jit () 
pc = target 
elif opcode == MOV_A_R : 
Francisco Fernandez Castano.(.@f.cofde#zc)rest unmoPdyPiy fied November 8, 2014 43 / 51
PyPy strategy 
[ elem * 2 for elem in elements ] 
balance = (a / b / c) * 4 
'asdadsasd - xxx '. replace ('x', 'y'). replace ('a', 'b') 
foo.bar () 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 44 / 51
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 45 / 51
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 46 / 51

Weitere ähnliche Inhalte

Was ist angesagt?

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
 
The Benefits of Type Hints
The Benefits of Type HintsThe Benefits of Type Hints
The Benefits of Type Hintsmasahitojp
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaokeRenyuan Lyu
 
Creating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy PresentationCreating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy Presentationdmurali2
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Younggun Kim
 
R and Python, A Code Demo
R and Python, A Code DemoR and Python, A Code Demo
R and Python, A Code DemoVineet Jaiswal
 
data-microscopes
data-microscopesdata-microscopes
data-microscopesStephen Tu
 
Programming with Python - Basic
Programming with Python - BasicProgramming with Python - Basic
Programming with Python - BasicMosky Liu
 
LingPy : A Python Library for Historical Linguistics
LingPy : A Python Library for Historical LinguisticsLingPy : A Python Library for Historical Linguistics
LingPy : A Python Library for Historical LinguisticsDr. Amit Kumar Jha
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its ApplicationsAbhijeet Singh
 
First python project
First python projectFirst python project
First python projectNeetu Jain
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorialee0703
 
Python in real world.
Python in real world.Python in real world.
Python in real world.Alph@.M
 
Microsoft kafka load imbalance
Microsoft   kafka load imbalanceMicrosoft   kafka load imbalance
Microsoft kafka load imbalanceNitin Kumar
 
Python: the Project, the Language and the Style
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the StyleJuan-Manuel Gimeno
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1Elaf A.Saeed
 

Was ist angesagt? (20)

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
 
The Benefits of Type Hints
The Benefits of Type HintsThe Benefits of Type Hints
The Benefits of Type Hints
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaoke
 
Creating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy PresentationCreating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy Presentation
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015
 
R and Python, A Code Demo
R and Python, A Code DemoR and Python, A Code Demo
R and Python, A Code Demo
 
data-microscopes
data-microscopesdata-microscopes
data-microscopes
 
Programming with Python - Basic
Programming with Python - BasicProgramming with Python - Basic
Programming with Python - Basic
 
LingPy : A Python Library for Historical Linguistics
LingPy : A Python Library for Historical LinguisticsLingPy : A Python Library for Historical Linguistics
LingPy : A Python Library for Historical Linguistics
 
Code quality for Terraform
Code quality for TerraformCode quality for Terraform
Code quality for Terraform
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its Applications
 
Python
Python Python
Python
 
First python project
First python projectFirst python project
First python project
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
 
Python in real world.
Python in real world.Python in real world.
Python in real world.
 
Microsoft kafka load imbalance
Microsoft   kafka load imbalanceMicrosoft   kafka load imbalance
Microsoft kafka load imbalance
 
Python: the Project, the Language and the Style
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the Style
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1
 

Ähnlich wie Understanding PyPy - PyConEs 14

Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014fcofdezc
 
Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Codemotion
 
Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015fcofdezc
 
Why Python is better for Data Science
Why Python is better for Data ScienceWhy Python is better for Data Science
Why Python is better for Data ScienceÍcaro Medeiros
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentationCommand Prompt., Inc
 
IT talk "Python language evolution"
IT talk "Python language evolution"IT talk "Python language evolution"
IT talk "Python language evolution"DataArt
 
PyWPS Development restart
PyWPS Development restartPyWPS Development restart
PyWPS Development restartJachym Cepicky
 
PyCon JP 2011 Lightning Talk No.10
PyCon JP 2011 Lightning Talk No.10PyCon JP 2011 Lightning Talk No.10
PyCon JP 2011 Lightning Talk No.10Yoji TAKEUCHI
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in PythonAjay Ohri
 
Will iPython replace Bash?
Will iPython replace Bash?Will iPython replace Bash?
Will iPython replace Bash?Babel
 
Will iPython replace bash?
Will iPython replace bash?Will iPython replace bash?
Will iPython replace bash?Roberto Polli
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったかYuta Kashino
 
Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Daker Fernandes
 
Python for System Administrators
Python for System AdministratorsPython for System Administrators
Python for System AdministratorsRoberto Polli
 

Ähnlich wie Understanding PyPy - PyConEs 14 (20)

Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014
 
Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Extending Python, what is the best option for me?
Extending Python, what is the best option for me?
 
Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015
 
SciPy 2022 Scikit-HEP
SciPy 2022 Scikit-HEPSciPy 2022 Scikit-HEP
SciPy 2022 Scikit-HEP
 
Why Python is better for Data Science
Why Python is better for Data ScienceWhy Python is better for Data Science
Why Python is better for Data Science
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentation
 
IT talk "Python language evolution"
IT talk "Python language evolution"IT talk "Python language evolution"
IT talk "Python language evolution"
 
PyWPS Development restart
PyWPS Development restartPyWPS Development restart
PyWPS Development restart
 
Python ppt
Python pptPython ppt
Python ppt
 
PyCon JP 2011 Lightning Talk No.10
PyCon JP 2011 Lightning Talk No.10PyCon JP 2011 Lightning Talk No.10
PyCon JP 2011 Lightning Talk No.10
 
50 shades of PHP
50 shades of PHP50 shades of PHP
50 shades of PHP
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in Python
 
Will iPython replace Bash?
Will iPython replace Bash?Will iPython replace Bash?
Will iPython replace Bash?
 
Will iPython replace bash?
Will iPython replace bash?Will iPython replace bash?
Will iPython replace bash?
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか
 
Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Python for System Administrators
Python for System AdministratorsPython for System Administrators
Python for System Administrators
 
180809
180809180809
180809
 

Mehr von fcofdezc

STM on PyPy
STM on PyPySTM on PyPy
STM on PyPyfcofdezc
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014fcofdezc
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014fcofdezc
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDayfcofdezc
 
Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)fcofdezc
 
biicode, reuse and play
biicode, reuse and playbiicode, reuse and play
biicode, reuse and playfcofdezc
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tourfcofdezc
 

Mehr von fcofdezc (7)

STM on PyPy
STM on PyPySTM on PyPy
STM on PyPy
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
 
Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)
 
biicode, reuse and play
biicode, reuse and playbiicode, reuse and play
biicode, reuse and play
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tour
 

Kürzlich hochgeladen

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
🐬 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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Kürzlich hochgeladen (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Understanding PyPy - PyConEs 14

  • 1. Understanding PyPy Francisco Fernandez Castano Rushmore.fm francisco.fernandez.castano@gmail.com @fcofdezc November 8, 2014 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 1 / 51
  • 2. Overview 1 Introduction My Personal history with PyPy What is PyPy? Why is Python slow? Motivation 2 RPython Overview De
  • 3. nition Goals Hello World in RPython Translation process Building Flow Graphs Annotation phase RTyper Backend Optimizations Preparation for source generation Overview 3 JIT PyPy strategy 4 Garbage Collection Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 2 / 51
  • 4. First PyPy impressions Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 3 / 51
  • 5. First PyPy impressions Python 2.7.3 (87 aa9de10f9ca71da9ab4a3d53e0ba176b67d086 [ PyPy 2.2.1 with GCC 4.8.3 20140624 (Red Hat 4.8.3 -1)] Type " help ", " copyright ", " credits " or " license " for >>>> 1 + 1 2 >>>> def f(x): return x + 1 >>>> f(1) 2 >>>> Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 4 / 51
  • 6. First PyPy impressions Ok... just Python... Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 5 / 51
  • 7. Second PyPy impressions Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 6 / 51
  • 8. What is PyPy? PyPy is a fast, compliant alternative implementation of the Python language (2.7.8 and 3.2.5). Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 7 / 51
  • 9. Why is Python slow? Interpretation overhead Boxed arithmetic and automatic over ow handling Dynamic dispatch of operations Dynamic lookup of methods and attributes Everything can change on runtime Extreme introspective and re ective capabilities Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 8 / 51
  • 10. Why is Python slow? Boxed arithmetic and automatic over ow handling i = 0 while i < 10000000: i = i +1 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 9 / 51
  • 11. Why is Python slow? Dynamic dispatch of operations # while i < 1000000 9 LOAD_FAST 0 (i) 12 LOAD_CONST 2 (10000000) 15 COMPARE_OP 0 (<) 18 POP_JUMP_IF_FALSE 34 # i = i + 1 21 LOAD_FAST 0 (i) 24 LOAD_CONST 3 (1) 27 BINARY_ADD 28 STORE_FAST 0 (i) 31 JUMP_ABSOLUTE 9 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 10 / 51
  • 12. Why is Python slow? Dynamic lookup of methods and attributes class MyExample ( object ): pass def foo(target , flag ): if flag : target .x = 42 obj = MyExample () foo(obj , True ) print obj.x #=> 42 print getattr (obj , "x") #=> 42 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 11 / 51
  • 13. Why is Python slow? Everything can change on runtime def fn (): return 42 def hello (): return 'Hi! PyConEs !' def change_the_world (): global fn fn = hello print fn () #=> 42 change_the_world () print fn () => 'Hi! PyConEs !' Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 12 / 51
  • 14. Why is Python slow? Everything can change on runtime class Dog( object ): def __init__ ( self ): self . name = 'Jandemor ' def talk ( self ): print "%s: guau !" % self . name class Cat( object ): def __init__ ( self ): self . name = 'CatInstance ' def talk ( self ): print "%s: miau !" % self . name Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 13 / 51
  • 15. Why is Python slow? Everything can change on runtime my_pet = Dog () my_pet . talk () #=> 'Jandemor : guau !' my_pet . __class__ = Cat my_pet . talk () #=> 'Jandemor : miau !' Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 14 / 51
  • 16. Why is Python slow? Extreme introspective and re ective capabilities def fill_list ( name ): frame = sys. _getframe (). f_back lst = frame . f_locals [ name ] lst. append (42) def foo (): things = [] fill_list ('things ') print things #=> 42 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 15 / 51
  • 17. Why is Python slow? CPython is a clean and maintainable software. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 16 / 51
  • 18. Why is Python slow? Show me the numbers Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 17 / 51
  • 19. Why is Python slow? Show me the numbers speed.pypy.org Video processing example Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 18 / 51
  • 20. What is PyPy? PyPy is a python interpreter written in Python. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 19 / 51
  • 21. What is PyPy? Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 20 / 51
  • 22. What is PyPy? PyPy is a python interpreter written in RPython. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 21 / 51
  • 23. Motivation The Pareto princpile: the 20% of the program will account for the 80% of the runtime. The Fast path principle. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 22 / 51
  • 24. De
  • 25. nition RPython is a restricted subset of Python that is amenable to static analysis. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 23 / 51
  • 26. Goals Abstract as much as possible creating Interpreters for DL. l * o * p l - language to analyze. o - optimize and tweak depending on dierent factors. (GC ie) p - being able to produce interpreter for dierent platforms. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 24 / 51
  • 27. Hello World Mandatory Hello World. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 25 / 51
  • 28. Translation process Complete program is imported generating control ow graph. The Annotator does type inference. The Rtyper uses high level types to transform into low level ones. Some optimizations are applied. Next step is prepare graphs to be translated. The C backend generates source
  • 29. les. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 26 / 51
  • 30. Translation process RPython source code Flow object space flow graph Annotator low level helpers annotated flow graph RTyper annotate low level helpers Low Level flow graph handwritten backend code external functions C code LLVM code genc other target function discovered need low level helper genllvm High Level Low Level handwritten code genXXX Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 27 / 51
  • 31. Building Flow Graphs Code is NOT parsed. Uses code objects that de
  • 32. ne the behaviour. Flow Graph builder using an abstract interpretation producing control ow graph. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 28 / 51
  • 33. Control ow graph Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 29 / 51
  • 34. Building Flow Graphs def f(n): return 3*n+2 Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 30 / 51
  • 35. Building Flow Graphs Block (v1 ): # input argument v2 = mul ( Constant (3) , v1) v3 = add (v2 , Constant (2)) Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 31 / 51
  • 36. Annotation phase Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 32 / 51
  • 37. Annotation phase Each variable that appears in the ow grap is annotated. With all possible values. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 33 / 51
  • 38. Annotation phase SomeObject SomeInteger SomeString SomeChar SomeTuple([s1, s2..., sn]) SomeList SomeDict SomeInstance Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 34 / 51
  • 39. RTyper v3 = add(v1 , v2) v1 - SomeInteger () v2 - SomeInteger () v3 - SomeInteger () Result v3 = int_add (v1 , v2) Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 35 / 51
  • 40. Backend Optimizations Function inlining. Malloc removal. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 36 / 51
  • 41. Preparation for source generation Exception handling, since C doesn't have that concept. Memory Management, GC Pluggable. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 37 / 51
  • 42. Overview Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 38 / 51
  • 44. nition In computing, just-in-time compilation (JIT), also known as dynamic translation, is compilation done during execution of a program at run time rather than prior to execution. Wikipedia. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 39 / 51
  • 45. Overview Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 40 / 51
  • 46. PyPy strategy RPython generates a tracing JIT. Instead of user code is interpreter code who launch compiled code. It comes mostly for free to language implementators. Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 41 / 51
  • 47. Control ow graph Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 42 / 51
  • 48. tlrjitdriver = JitDriver ( greens = ['pc ', 'bytecode '], reds = ['a', 'regs ']) def interpret ( bytecode , a): regs = [0] * 256 pc = 0 while True : tlrjitdriver . jit_merge_point () opcode = ord ( bytecode [pc ]) pc += 1 if opcode == JUMP_IF_A : target = ord ( bytecode [pc ]) pc += 1 if a: if target pc: tlrjitdriver . can_enter_jit () pc = target elif opcode == MOV_A_R : Francisco Fernandez Castano.(.@f.cofde#zc)rest unmoPdyPiy fied November 8, 2014 43 / 51
  • 49. PyPy strategy [ elem * 2 for elem in elements ] balance = (a / b / c) * 4 'asdadsasd - xxx '. replace ('x', 'y'). replace ('a', 'b') foo.bar () Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 44 / 51
  • 50. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 45 / 51
  • 51. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 46 / 51
  • 52. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 47 / 51
  • 53. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 48 / 51
  • 54. Mark and sweep Pros: Can collect cycles. Cons: Basic implementation stops the world Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 49 / 51
  • 55. Questions? Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 50 / 51
  • 56. The End Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 51 / 51