Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyPy hands-on

PyPy hands-on

EuroPython 2011, Firenze, Italy

Antonio Cuni

June 20, 2011
Tweet

More Decks by Antonio Cuni

Other Decks in Programming

Transcript

  1. PyPy training session Antonio Cuni Armin Rigo EuroPython 2011 June

    20 2011 antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 1 / 1
  2. 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
  3. Part 1 Run your application under PyPy antocuni, arigo (EuroPython

    2011) PyPy training session June 20 2011 2 / 1
  4. How to run PyPy pypy program.py That’s it! (modulo details)

    antocuni, arigo (EuroPython 2011) PyPy training session June 20 2011 3 / 1
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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