Slide 1

Slide 1 text

PyPy training session Antonio Cuni Armin Rigo EuroPython 2011 June 20 2011 antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 1 / 1

Slide 2

Slide 2 text

PyPy training session Part 1: Run your application under PyPy Part 2: Write your own interpreter with PyPy antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 1 / 1

Slide 3

Slide 3 text

Part 1 Run your application under PyPy antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 2 / 1

Slide 4

Slide 4 text

How to run PyPy pypy program.py That’s it! (modulo details) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 3 / 1

Slide 5

Slide 5 text

Challenge html_fibo.py HTML list of fibonacci numbers (the most complicate ever) run it on CPython run it on PyPy fix it! antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 4 / 1

Slide 6

Slide 6 text

Refcounting vs generational GC (1) gc0.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) foo() print file(’/tmp/bar.txt’).read() gc1.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) f.close() # <------- gc2.py def foo(): with file(’/tmp/bar.txt’, ’w’) as f: f.write(’hello world’) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 5 / 1

Slide 7

Slide 7 text

Refcounting vs generational GC (1) gc0.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) foo() print file(’/tmp/bar.txt’).read() gc1.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) f.close() # <------- gc2.py def foo(): with file(’/tmp/bar.txt’, ’w’) as f: f.write(’hello world’) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 5 / 1

Slide 8

Slide 8 text

Refcounting vs generational GC (1) gc0.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) foo() print file(’/tmp/bar.txt’).read() gc1.py def foo(): f = file(’/tmp/bar.txt’, ’w’) f.write(’hello world’) f.close() # <------- gc2.py def foo(): with file(’/tmp/bar.txt’, ’w’) as f: f.write(’hello world’) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 5 / 1

Slide 9

Slide 9 text

Refcounting vs generational GC (2) __del__ especially files or sockets don’t leak file descriptors! weakrefs finally inside generators antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 6 / 1

Slide 10

Slide 10 text

Just-in-Time Compilation Tracing JIT, like TraceMonkey Complete by construction Supports Intel x86, amd64, and soon ARM antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 7 / 1

Slide 11

Slide 11 text

Short introduction to JITting run code with the interpreter observe what it does generate optimized machine code for commonly executed paths using runtime knowledge (types, paths taken) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 8 / 1

Slide 12

Slide 12 text

Tracing JIT compiles one loop at a time generates linear code paths, recording what the interpreter did for each possible branch, generate a guard, that exits assembler on triggering if guard fails often enough, start tracing from the failure antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 9 / 1

Slide 13

Slide 13 text

Meta-Tracing in PyPy The explanation above assumes a tracing JIT for the full Python language Would need to be maintained whenever we change the Python version we support Instead, we have a “meta-tracing JIT” A very important point for us since we don’t have a huge team to implement all Python semantics for the JIT We trace the python interpreter’s main loop (running N times) interpreting a python loop (running once) antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 10 / 1

Slide 14

Slide 14 text

PYPYLOG PYPYLOG=categories:logfile pypy program.py categories: gc-minor, gc-major jit-log-noopt, jit-log-opt jit-backend jit-backend-counts antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 11 / 1

Slide 15

Slide 15 text

Inspecting the JIT log count.py def count_mult_of_5(N): mult = 0 not_mult = 0 for i in range(N): if i % 5 == 0: mult += 1 else: not_mult += 1 return mult, not_mult PYPYLOG=jit-log-opt:mylog pypy count.py 2000 PYPYLOG=jit-log-opt:mylog pypy count.py 10000 antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 12 / 1

Slide 16

Slide 16 text

The jitviewer PYPYLOG=jit-log-opt,jit-backend-counts:mylog pypy count.py 2000 PYPYLOG=jit-log-opt,jit-backend-counts:mylog pypy count.py 10000 jitviewer.py log.pypylog Look at the (missing) bridge! antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 13 / 1