Slide 1

Slide 1 text

Let's Talk About David Beazley @dabeaz

Slide 2

Slide 2 text

PyPy Project • Perhaps you've heard about PyPy • Python implemented in Python • It is apparently quite a bit faster • How is that possible? Magic???

Slide 3

Slide 3 text

It's Not Slow Draw the Mandelbrot set Credit: Jeff Preshing CPython 2.7: 502s _ = ( 255, lambda V ,B,c :c and Y(V*V+B,B, c -1)if(abs(V)<6)else ( 2+c-4*abs(V)**-0.4)/i ) ;v, x=1500,1000;C=range(v*x );import struct;P=struct.pack;M,\ j ='

Slide 4

Slide 4 text

It's Not Slow Draw the Mandelbrot set Credit: Jeff Preshing CPython 2.7: 502s _ = ( 255, lambda V ,B,c :c and Y(V*V+B,B, c -1)if(abs(V)<6)else ( 2+c-4*abs(V)**-0.4)/i ) ;v, x=1500,1000;C=range(v*x );import struct;P=struct.pack;M,\ j ='

Slide 5

Slide 5 text

It's Not Slow Draw the Mandelbrot set (non-obfuscated) Python 2.7.2 : 14.5s Python 2.7.2+ctypes : 0.95s PyPy-1.8 : 0.42s Yow! That's 34x faster!

Slide 6

Slide 6 text

I LIKE IT! "Laziness is the first great virtue of a programmer" -- Larry Wall

Slide 7

Slide 7 text

CPython PyPy One is clearly faster than the other...

Slide 8

Slide 8 text

CPython PyPy Just in time compilation Translation to C Optimization One is clearly faster than the other...

Slide 9

Slide 9 text

CPython PyPy Just in time compilation Translation to C Optimization ... but performance is not what this talk is about. One is clearly faster than the other...

Slide 10

Slide 10 text

CPython PyPy • Which one can you adjust with a pocketknife?

Slide 11

Slide 11 text

CPython PyPy • Which one can you adjust with a pocketknife? ... in the dark

Slide 12

Slide 12 text

CPython PyPy • Which one can you adjust with a pocketknife? ... in the dark ... under a pressing deadline

Slide 13

Slide 13 text

CPython PyPy • Which one can you adjust with a pocketknife? ... in the dark ... under a pressing deadline I speak from some experience...

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Thinking about Tinkering (with PyPy)

Slide 16

Slide 16 text

Tinkering Matters! CPython

Slide 17

Slide 17 text

Tinkering Matters! CPython Patches

Slide 18

Slide 18 text

Tinkering Matters! CPython Patches PEPs

Slide 19

Slide 19 text

Tinkering Matters! CPython Patches PEPs Extensions python-ideas

Slide 20

Slide 20 text

"Oh, Interesting..."

Slide 21

Slide 21 text

Exploring New Ideas ported to An "afternoon hack," with a big impact parallel Python

Slide 22

Slide 22 text

Exploring New Ideas ported to An "afternoon hack," with a big impact parallel Python ... we didn't choose Python for performance.

Slide 23

Slide 23 text

Tinkering Creates Cool Stuff

Slide 24

Slide 24 text

CPython PyPy An honest question • Is PyPy something that YOU can tinker with? • As in YOU... sitting in this room. • Or is it for "evil genuises only?"

Slide 25

Slide 25 text

Armin Maciej Alex You?

Slide 26

Slide 26 text

A Confession • PyPy scares me • It's fast. I get that. • A lot of moving parts • A lot of advanced computer science inside

Slide 27

Slide 27 text

Tinker Away! I Worry About Complexity ... Abandon all hope

Slide 28

Slide 28 text

CPython PyPy An honest question • Is PyPy something that YOU can tinker with? Honest answer: I don't know

Slide 29

Slide 29 text

• See if I could teach myself to tinker with PyPy • From scratch (I'm not a PyPy developer) • Use nothing but the source, online docs, etc. An Experiment:

Slide 30

Slide 30 text

Constraints A part-time project

Slide 31

Slide 31 text

Tinkering with PyPy != Using PyPy • If you want to use it, just run it • It's Python. • Not so interesting (not as much as tinkering) bash % pypy gofaster.py

Slide 32

Slide 32 text

Tinkering with PyPy != Creating PyPy • submit a useful bug report (or patch) • Make extensions • Study parts of the implementation (GIL, etc.) • Post messages on [email protected]

Slide 33

Slide 33 text

Where To Start? • Tinkerers use source • You build it yourself! • You read instructions http://pypy.org Go Download it. Now!

Slide 34

Slide 34 text

Running py.py • PyPy is written in "Python"... you can run it bash % python pypy-1.8/pypy/bin/py.py [platform:execute] gcc-4.0 -c -arch x86_64 -O frame-pointer - \ ... PyPy 1.8.0 in StdObjSpace on top of Python 2. (startuptime: 23.23 secs) >>>> • Performance is terrible! • You wouldn't do it except for debugging

Slide 35

Slide 35 text

Translating PyPy • To get the "real" version, you translate it • Huh? No makefile? No setup.py? • Already, I'm getting nervous. bash % cd pypy/translator/goal bash % python translate.py -Ojit

Slide 36

Slide 36 text

Demo

Slide 37

Slide 37 text

Building PyPy Some Facts: • Movie is @ 64x speed • Takes a few hours Contrast: Configure and build CPython-3.2.2 • ./configure; make -j8 • Takes about 90 seconds Immediate Problem: • Finding enough RAM • It takes >4GB 4 cores, 8 GB RAM EC2, m2.xlarge (17GB) What's Actually Happening • Translation of PyPy to C • Creates ~800 C files • ~10.4 million lines! • 350 Mbytes It might kill the C compiler (or your machine) • Example: gcc-4.2 This is Amazing! • Dare I say "diabolical" • If not intimidating One of the most daunting parts of PyPy • Must redo the process if you make any tweak • An obvious barrier to casual tinkering

Slide 38

Slide 38 text

RPython • PyPy is actually implemented in "RPython" • RPython is not an "interpreter", but a restricted subset of the Python language Python rpython • It can run as valid Python code, but that's about the only similarity

Slide 39

Slide 39 text

RPython • Formal specification (in their own words): "RPython is everything that our translation toolchain can accept"

Slide 40

Slide 40 text

RPython • Formal specification (in their own words): "RPython is everything that our translation toolchain can accept" • An analogy "Python is everything that runs without generating a traceback."

Slide 41

Slide 41 text

RPython • Formal specification (in their own words): "RPython is everything that our translation toolchain can accept" • An analogy "Python is everything that runs without generating a traceback." • Okay, let's go reading...

Slide 42

Slide 42 text

Documentation

Slide 43

Slide 43 text

High-level Docs

Slide 44

Slide 44 text

Detailed Tech Reports

Slide 45

Slide 45 text

Detailed Tech Reports To be fair, it was a funded academic project in PL. (They had to write like this)

Slide 46

Slide 46 text

Source Code • 454 directories • 5534 files (4513 .py source files) • ~1.25 million non-blank source lines (.py) By The Numbers: It's not so easy to just jump in and make sense of it

Slide 47

Slide 47 text

Reading Blogs • Recommend start: Andrew Brown • Laurence Tratt "Fast Enough VMs in Fast Enough Time" "Tutorial: Writing an Interpreter with PyPy" http://bit.ly/fmV2wx http://bit.ly/y8GLqf

Slide 48

Slide 48 text

Just Do It (Live RPython Coding Demo)

Slide 49

Slide 49 text

RPython in a Nutshell • RPython is a completely different language • Python syntax, yes. • Must be compiled (like C, C++, etc.) • Static typing via type inference • Limited set of libraries • If you love Python, you will hate RPython

Slide 50

Slide 50 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0

Slide 51

Slide 51 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0 int

Slide 52

Slide 52 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0 int int

Slide 53

Slide 53 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0 int int int

Slide 54

Slide 54 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0 int int int int

Slide 55

Slide 55 text

Type Inference Illustrated def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) def main(argv): print fib(int(argv[1])) return 0 int int int int int Key Point: Think static typing (like C)

Slide 56

Slide 56 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Now think about the whole program Type inference + restrictions

Slide 57

Slide 57 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement There is a single spark... Entry point

Slide 58

Slide 58 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Entry point Exploration Begins

Slide 59

Slide 59 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Exploration Begins Entry point

Slide 60

Slide 60 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Entry point Exploration Begins Whole program type annotation!

Slide 61

Slide 61 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement All reachable control- flow paths are followed Entry point Whole program type annotation!

Slide 62

Slide 62 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Entry point Imagine this diagram, but with tens of thousands of functions Whole program type annotation!

Slide 63

Slide 63 text

# file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement Key Insight: Entry point All of the reachable code is RPython

Slide 64

Slide 64 text

RPython def name1(args): statement statement statement # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement def name3(args): statement statement statement statement # file2.py def name1(args): statement statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement PyPy Source def name1(args): statement statement # file3.py def name2(args): statement statement def name3(args): statement statement def name4(args): statement statement class B(object): def method1(self,args): statement statement statement def method2(self,args): statement statement def name1(args): statement statement def name2(args): statement statement def name4(args): statement statement Translation C Compile pypy-c Entry point

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Understanding Translation • The translation process will blow your mind • Full understanding by mortals is probably futile • Snakes + Souls of Ph.D. students inside? • Let's look at a small taste...

Slide 67

Slide 67 text

A Function def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) Obvious question: How does it translate to C?

Slide 68

Slide 68 text

Traditional Compiler def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) Lexer Parser IR C You might think it's like a traditional compiler.

Slide 69

Slide 69 text

Traditional Compiler def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) Lexer Parser IR C You might think it's like a traditional compiler. (and you would be wrong)

Slide 70

Slide 70 text

Traditional Compiler IR def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) Lexer Parser C Insight: Python already parsed the code! ... so don't do it again. ?????

Slide 71

Slide 71 text

RPython Translation IR C Translation occurs directly from Python code objects >>> fib.__code__.co_code '|\x00\x00d\x01\x00k\x00\x00r\x10\x00 d\x02\x00St\x00\x00|\x00\x00d\x02\x00 \x18\x83\x01\x00t\x00\x00|\x00\x00d \x01\x00\x18\x83\x01\x00\x17Sd\x00\x00S'

Slide 72

Slide 72 text

Bytecode Interpretation CPython • Python has a bytecode interpreter • Core of the eval loop (written in C). bytecode interpreter runtime

Slide 73

Slide 73 text

Bytecode Interpretation CPython • It executes the bytecode bytecode interpreter runtime >>> fib.__code__.co_code '|\x00\x00d\x01\x00k\x00\x00r\x1 d\x02\x00St\x00\x00|\x00\x00d\x \x18\x83\x01\x00t\x00\x00|\x00\ \x01\x00\x18\x83\x01\x00\x17Sd\

Slide 74

Slide 74 text

Bytecode Interpretation PyPy • PyPy has a bytecode interpreter too • Written in pure Python (that's the whole idea) bytecode interpreter runtime >>> fib.__code__.co_code '|\x00\x00d\x01\x00k\x00\x00r\x1 d\x02\x00St\x00\x00|\x00\x00d\x \x18\x83\x01\x00t\x00\x00|\x00\ \x01\x00\x18\x83\x01\x00\x17Sd\

Slide 75

Slide 75 text

Bytecode Interpretation PyPy runtime bytecode interpreter • Bytecode interpreter is modular • Also used by the translate.py program translate.py bytecode interpreter abstract runtime

Slide 76

Slide 76 text

Just to be clear... PyPy translates itself using its own bytecode interpreter

Slide 77

Slide 77 text

Abstract Interpretation translate.py bytecode interpreter abstract runtime 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE Translator runs the code "in the abstract"

Slide 78

Slide 78 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE Translator runs the code "in the abstract" translate.py bytecode interpreter abstract runtime

Slide 79

Slide 79 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 80

Slide 80 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 81

Slide 81 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 82

Slide 82 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 83

Slide 83 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 84

Slide 84 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 85

Slide 85 text

Abstract Interpretation Translator runs the code "in the abstract" 2 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (2) 6 COMPARE_OP 0 (<) 9 POP_JUMP_IF_FALSE 16 3 12 LOAD_CONST 2 (1) 15 RETURN_VALUE 5 >> 16 LOAD_GLOBAL 0 (fib) 19 LOAD_FAST 0 (n) 22 LOAD_CONST 2 (1) 25 BINARY_SUBTRACT 26 CALL_FUNCTION 1 29 LOAD_GLOBAL 0 (fib) 32 LOAD_FAST 0 (n) 35 LOAD_CONST 1 (2) 38 BINARY_SUBTRACT 39 CALL_FUNCTION 1 42 BINARY_ADD 43 RETURN_VALUE 44 LOAD_CONST 0 (None) 47 RETURN_VALUE translate.py bytecode interpreter abstract runtime

Slide 86

Slide 86 text

Abstract Interpretation A Control flow graph is constructed translate.py bytecode interpreter abstract runtime

Slide 87

Slide 87 text

Abstract Interpretation C Code The full details are "hairy"

Slide 88

Slide 88 text

"You are in a maze of twisty little passages, all alike." (and a huge green fierce snake bars the way)

Slide 89

Slide 89 text

Understanding the Source • Two different languages co-exist (same syntax) # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement Full Python???? RPython???? Which is it?

Slide 90

Slide 90 text

Understanding the Source • Two different languages co-exist (same syntax) # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement Full Python???? RPython???? Which is it? (You can't look in isolation)

Slide 91

Slide 91 text

Understanding the Source def cast_object_to_ptr(PTR, object): """NOT_RPYTHON: hack. The object may Limited to casting a given object to """ if isinstance(PTR, lltype.Ptr): TO = PTR.TO else: TO = PTR ... • PyPy uses doc strings to help you sort it out • It is enforced by the translator (an assertion)

Slide 92

Slide 92 text

Understanding the Source • Deeper question: Why would you have mixed code? # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement RPython Python • Head throbbing...

Slide 93

Slide 93 text

Execution Contexts # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement Translation (Python) Executable (C)

Slide 94

Slide 94 text

Execution Contexts # file1.py def name2(args): statement statement def name3(args): statement statement statement statement def name1(args): statement statement statement Translation (Python) Executable (C) • At translation, the code separates Metaprogramming Implementation • decorators • metaclasses • exec()

Slide 95

Slide 95 text

Example def decorator(func): statements ... def wrapper(*args,**kwargs): statements ... return func(*args,**kwargs) return wrapper @decorator def func(args): statements ...

Slide 96

Slide 96 text

Example def decorator(func): statements ... def wrapper(*args,**kwargs): statements ... return func(*args,**kwargs) return wrapper @decorator def func(args): statements ... Python RPython Python RPython

Slide 97

Slide 97 text

Rules of Thumb • Code that executes at import time can make use of all Python features • Code reachable through the entry point (target) is RPython • Keeping it straight is hard (for me anyways)

Slide 98

Slide 98 text

But Wait, There's More!

Slide 99

Slide 99 text

Foreign Code • PyPy is written in "Python", but can access external C code and libraries • os, math, time, threads, etc. • There is a highly developed FFI mechanism • Plus a configuration system (think autoconf)

Slide 100

Slide 100 text

Example (Accessing Foreign Functions)

Slide 101

Slide 101 text

A Quandary • How do I end this talk? • I've only talked about RPython • When do we get to the PyPy?

Slide 102

Slide 102 text

A Realization I still don't know know how PyPy works! Score: PyPy: 1 Dave: 0

Slide 103

Slide 103 text

A Deeper Realization I don't even know how CPython works!

Slide 104

Slide 104 text

WAT!?! WAT!?!

Slide 105

Slide 105 text

A Clarification I do know how to use the tools that make CPython

Slide 106

Slide 106 text

A Clarification I do know how to use the tools that make CPython • ANSI C • Makefiles • Algorithms • Data Structures

Slide 107

Slide 107 text

The Challenge PyPy has a different set of tools • RPython • translate.py • Metaprogramming • Foreign Functions

Slide 108

Slide 108 text

So how to end this talk?

Slide 109

Slide 109 text

Wait! I used to be an evil professor!

Slide 110

Slide 110 text

Figuring out how PyPy works is left as an exercise! (You'll learn a lot)

Slide 111

Slide 111 text

Postscript

Slide 112

Slide 112 text

Postscript Let's talk about Ruby! threads.each { |aThread| aThread.join }

Slide 113

Slide 113 text

Breaking GILs • As you know, I like breaking GILs • You know, global interpreter locks • As in threads and stuff... • I love it!

Slide 114

Slide 114 text

A Benchmark • Message-passing with a CPU-bound thread C : 1.11s Python 2.7 : 1.60s Ruby 1.9 : 5839.4s • Don't concern yourself with the details • Ruby 3600x slower than Python? • What's that all about? Let's go tinker!

Slide 115

Slide 115 text

Tinkering with Ruby • It was pretty straightforward • Finding the GIL didn't take long • An afternoon of fiddling around (Search for my talk at RuPy 2011) • Caused by a more extreme case of the thread priority inversion that's in Python 3.3

Slide 116

Slide 116 text

Just to be clear... I couldn't write a real Ruby program to save my life right now.

Slide 117

Slide 117 text

A PyPy Benchmark • A similar message-passing benchmark Python 2.7 : 15.6s PyPy-1.6 : 6689.2s (428x slower) • Huh? What's that all about? • No idea! Or even how to look. • That is the whole reason for this talk

Slide 118

Slide 118 text

Parting Words • Can you tinker with PyPy? • Honest answer: I still don't know • Should you try to go tinker with it anyways? • YES! • You will find interesting things inside

Slide 119

Slide 119 text

"My God, it's full of stars!"

Slide 120

Slide 120 text

"My God, it's full of stars!" (or VMs?)

Slide 121

Slide 121 text

Thanks! • Hope you learned at least one new thing • Special thanks: • Alex Gaynor • Maciej Fijalkowski • Chipy • Twitter: @dabeaz