PyPy hands on
PyPy hands on
- What is PyPy (briefly)?
- How to use it?
- How does it work?
- How to analyse performance?
What is PyPy
- A python interpreter
- Features speed
- Together with a fully transparent JIT compiler
How to run PyPy
- pypy program.py
- That's it!
- (modulo details)
- HTML list of fibonacci numbers
- (the most complicate ever)
- run it on CPython
- run it on PyPy
- fix it!
Refcounting vs generational GC (1)
def foo(): f = file('/tmp/bar.txt', 'w') f.write('hello world') foo() print file('/tmp/bar.txt').read()
def foo(): f = file('/tmp/bar.txt', 'w') f.write('hello world') f.close() # <-------
def foo(): with file('/tmp/bar.txt', 'w') as f: f.write('hello world')
Refcounting vs generational GC (2)
- especially files or sockets
- don't leak file descriptors!
- finally inside generators
How the python interpreter works
- compiles down to bytecode
- executes it one intruction at a time
- (PyPy, Psyco) compiles it further down to assembler
- Tracing JIT, like TraceMonkey
- Complete by construction
- Supports Intel x86, amd64, and soon ARM
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)
- 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
- PYPYLOG=categories:logfile pypy program.py
- gc-minor, gc-major
- jit-log-noopt, jit-log-opt
Inspecting the JIT log
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