Download - Understanding PyPy - PyConEs 14
![Page 1: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/1.jpg)
Understanding PyPy
Francisco Fernandez Castano
Rushmore.fm
[email protected] @fcofdezc
November 8, 2014
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 1 / 51
![Page 2: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/2.jpg)
Overview1 Introduction
My Personal history with PyPyWhat is PyPy?
Why is Python slow?
Motivation2 RPython
OverviewDefinitionGoals
Hello World in RPythonTranslation process
Building Flow GraphsAnnotation phaseRTyperBackend OptimizationsPreparation for source generation
Overview3 JIT
PyPy strategy4 Garbage Collection
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 2 / 51
![Page 3: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/3.jpg)
First PyPy impressions
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 3 / 51
![Page 4: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/4.jpg)
First PyPy impressions
Python 2.7.3 (87 aa9de10f9ca71da9ab4a3d53e0ba176b67d086 , Sep 03 2014, 08:45:22)
[PyPy 2.2.1 with GCC 4.8.3 20140624 (Red Hat 4.8.3 -1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> 1 + 1
2
>>>> def f(x): return x + 1
>>>> f(1)
2
>>>>
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 4 / 51
![Page 5: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/5.jpg)
First PyPy impressions
Ok... just Python...
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 5 / 51
![Page 6: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/6.jpg)
Second PyPy impressions
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 6 / 51
![Page 7: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/7.jpg)
What is PyPy?
PyPy is a fast, compliant alternative implementation of the Pythonlanguage (2.7.8 and 3.2.5).
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 7 / 51
![Page 8: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/8.jpg)
Why is Python slow?
Interpretation overhead
Boxed arithmetic and automatic overflow handling
Dynamic dispatch of operations
Dynamic lookup of methods and attributes
Everything can change on runtime
Extreme introspective and reflective capabilities
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 8 / 51
![Page 9: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/9.jpg)
Why is Python slow?Boxed arithmetic and automatic overflow handling
i = 0
while i < 10000000:
i = i +1
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 9 / 51
![Page 10: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/10.jpg)
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
![Page 11: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/11.jpg)
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
![Page 12: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/12.jpg)
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
![Page 13: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/13.jpg)
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
![Page 14: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/14.jpg)
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
![Page 15: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/15.jpg)
Why is Python slow?Extreme introspective and reflective 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
![Page 16: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/16.jpg)
Why is Python slow?
CPython is a clean and maintainable software.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 16 / 51
![Page 17: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/17.jpg)
Why is Python slow?Show me the numbers
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 17 / 51
![Page 18: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/18.jpg)
Why is Python slow?Show me the numbers
speed.pypy.org
Video processing example
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 18 / 51
![Page 19: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/19.jpg)
What is PyPy?
PyPy is a python interpreter written in Python.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 19 / 51
![Page 20: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/20.jpg)
What is PyPy?
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 20 / 51
![Page 21: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/21.jpg)
What is PyPy?
PyPy is a python interpreter written in RPython.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 21 / 51
![Page 22: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/22.jpg)
Motivation
The Pareto princpile: the 20% of the program will account for the80% of the runtime.
The Fast path principle.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 22 / 51
![Page 23: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/23.jpg)
Definition
RPython is a restricted subset of Python that is amenable to staticanalysis.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 23 / 51
![Page 24: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/24.jpg)
Goals
Abstract as much as possible creating Interpreters for DL.
l * o * p
l - language to analyze.
o - optimize and tweak depending on different factors. (GC ie)
p - being able to produce interpreter for different platforms.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 24 / 51
![Page 25: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/25.jpg)
Hello World
Mandatory Hello World.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 25 / 51
![Page 26: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/26.jpg)
Translation process
Complete program is imported generating control flow 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 files.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 26 / 51
![Page 27: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/27.jpg)
Translation process
RPython source code
Flow object space
High Level Low Level
flow graph
annotated flow graph
Annotator
Low Level flow graph
RTyper
C code
LLVM code
genc
other target
PyPy:Translation Process
low level helpers
an
no
tate
lo
w l
evel
help
ers
handwritten backend code
external functions
function discovered
need low level helper
produced code
intermediate representation
handwritten code
genXXX
genllvm
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 27 / 51
![Page 28: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/28.jpg)
Building Flow Graphs
Code is NOT parsed.
Uses code objects that define the behaviour.
Flow Graph builder using an abstract interpretation producing controlflow graph.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 28 / 51
![Page 29: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/29.jpg)
Control flow graph
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 29 / 51
![Page 30: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/30.jpg)
Building Flow Graphs
def f(n):
return 3*n+2
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 30 / 51
![Page 31: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/31.jpg)
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
![Page 32: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/32.jpg)
Annotation phase
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 32 / 51
![Page 33: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/33.jpg)
Annotation phase
Each variable that appears in the flow grap is annotated.
With all possible values.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 33 / 51
![Page 34: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/34.jpg)
Annotation phase
SomeObject
SomeInteger
SomeString
SomeChar
SomeTuple([s1, s2..., sn])
SomeList
SomeDict
SomeInstance
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 34 / 51
![Page 35: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/35.jpg)
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
![Page 36: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/36.jpg)
Backend Optimizations
Function inlining.
Malloc removal.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 36 / 51
![Page 37: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/37.jpg)
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
![Page 38: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/38.jpg)
Overview
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 38 / 51
![Page 39: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/39.jpg)
JitDefinition
In computing, just-in-time compilation (JIT), also known as dynamictranslation, is compilation done during execution of a program at run timerather than prior to execution. Wikipedia.
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 39 / 51
![Page 40: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/40.jpg)
Overview
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 40 / 51
![Page 41: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/41.jpg)
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
![Page 42: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/42.jpg)
Control flow graph
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 42 / 51
![Page 43: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/43.jpg)
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:
... #rest unmodifiedFrancisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 43 / 51
![Page 44: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/44.jpg)
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
![Page 45: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/45.jpg)
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 45 / 51
![Page 46: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/46.jpg)
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 46 / 51
![Page 47: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/47.jpg)
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 47 / 51
![Page 48: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/48.jpg)
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 48 / 51
![Page 49: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/49.jpg)
Mark and sweep
Pros: Can collect cycles.
Cons: Basic implementation stops the world
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 49 / 51
![Page 50: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/50.jpg)
Questions?
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 50 / 51
![Page 51: Understanding PyPy - PyConEs 14](https://reader034.vdocuments.mx/reader034/viewer/2022042614/55a2bdcf1a28ab54268b4615/html5/thumbnails/51.jpg)
The End
Francisco Fernandez Castano (@fcofdezc) PyPy November 8, 2014 51 / 51