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

PyPy: current status and the GIL-less future

PyPy: current status and the GIL-less future

EuroPython 2012, Firenze, Italy

Antonio Cuni

July 02, 2012
Tweet

More Decks by Antonio Cuni

Other Decks in Programming

Transcript

  1. PyPy: current status and GIL-less future Antonio Cuni, Maciej Fijałkowski,

    Armin Rigo EuroPython 2012 July 2 2012 antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 1 / 35
  2. PyPy at EuroPython fijal:~/extradoc/talk$ cd ep20 ep2004-pypy/ ep2006/ ep2008/ ep2010/

    ep2005/ ep2007/ ep2009/ ep2011/ ep2012/ for those who missed previous EPs, PyPy is a Python interpreter with a JIT. antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 1 / 35
  3. PyPy at EuroPython fijal:~/extradoc/talk$ cd ep20 ep2004-pypy/ ep2006/ ep2008/ ep2010/

    ep2005/ ep2007/ ep2009/ ep2011/ ep2012/ for those who missed previous EPs, PyPy is a Python interpreter with a JIT. antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 1 / 35
  4. Software archeology “single functions doing integer arithmetic get great speed-ups;

    about anything else will be a bit slower with the JIT than without. We are working on this - you can even expect quick progress, because it is mostly a matter of adding a few careful hints in the source code of the Python interpreter of PyPy.” (status of the JIT of PyPy as of March 2007) antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 2 / 35
  5. Software archeology Around since 2003 (advertised as) production ready since

    December 2010 release 1.4 Funding EU FP6 programme Eurostars programme donations ... antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 3 / 35
  6. PyPy 1.9: current status Faster 1.7x than 1.5 (a year

    ago) 2.2x than 1.4 5.5x than CPython Implements Python 2.7.2 Many more “PyPy-friendly” programs Packaging Debian, Ubuntu, Fedora, Homebrew, Gentoo, ArchLinux, ... Windows (32bit only), OS X C extension compatibility runs (big part of) PyOpenSSL and lxml antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 4 / 35
  7. PyPy organization Part of Software Freedom Conservancy Bradley successfully fighting

    U.S. bureaucracy we are happy about it Funding model py3k, numpy, STM more than 100’000$ in donations from individuals, large companies and the PSF thank to all antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 5 / 35
  8. Let’s talk about Python Rapid prototyping run your web server

    in 3 seconds run your script in 0.1s Glue language integrating with C is “easy” antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 6 / 35
  9. Let’s talk about PyPy JIT warmup time significant rapid prototyping

    is harder no good way to call C from PyPy (yet) antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 7 / 35
  10. JIT warmup times JIT-ted code: very fast Everything else: slow

    JIT-ting one piece at a time “takes a while” Cannot cache JIT-ted code between runs We did not spend much time on this PyPy JIT Under the hood July 4 2012 antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 8 / 35
  11. JIT warmup times JIT-ted code: very fast Everything else: slow

    JIT-ting one piece at a time “takes a while” Cannot cache JIT-ted code between runs We did not spend much time on this PyPy JIT Under the hood July 4 2012 antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 8 / 35
  12. Py3k py3k branch in mercurial developed in parallel Focus on

    correctness Dropped some interpreter optimizations for now Work in progress antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 9 / 35
  13. Py3k status Directly from the “What’s new in Python 3.x”:

    string vs unicode, int/long unification syntactic changes (print(), except, ...) set, oct, binary, bytes literals view and iterators instead of lists function annotations, keyword only arguments nonlocal extended iterable unpacking dictionary comprehensions raise ... from ..., lexical exception handling __pycache__ Most features are already there major exception: unicode identifiers antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 10 / 35
  14. Py3k: what’s left? First 90% done, remaining 90% not done

    Tons of small issues Extension modules / stdlib In January: PyPy “own” tests: 1621 failures CPython tests: N/A (did not compile) Now: PyPy “own” tests: 83 failures CPython tests: “lots” Most are shallow failures antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 11 / 35
  15. NumPy progress going slowly multi dimensional arrays, broadcasting, fancy indexing

    all dtypes, except complex, strings and objects tons of functions missing you can help! antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 12 / 35
  16. Calling C landscape CPython C extensions SWIG, SIP, wrapper generators

    ctypes Cython CFFI (our new thing) antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 14 / 35
  17. CFFI Example >>> from cffi import FFI >>> ffi =

    FFI() >>> ffi.cdef(""" ... int printf(const char *format, ...); ... """) >>> C = ffi.dlopen(None) >>> arg = ffi.new("char[]", "world") >>> C.printf("hi there, %s!\n", arg) hi there, world! antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 15 / 35
  18. CFFI Many more examples Including macro calls and most subtleties

    of C http://cffi.readthedocs.org antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 16 / 35
  19. STM Software Transactional Memory “Remove the GIL” antocuni, fijal, arigo

    (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 17 / 35
  20. Problem One Python program == one core Even with threads

    antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 18 / 35
  21. Does it matter? “My script runs anyway in 0.1 seconds”

    Python getting exponentially slower? antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 19 / 35
  22. Does it matter? “My script runs anyway in 0.1 seconds”

    Python getting exponentially slower? antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 19 / 35
  23. Does it matter? “I can have several processes exchanging data”

    A special-case solution only antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 20 / 35
  24. Does it matter? “I can have several processes exchanging data”

    A special-case solution only antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 20 / 35
  25. pypy-stm A Python without the GIL not the first one:

    Python 1.4 patch (Greg Stein, 1996) Jython IronPython Demo antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 21 / 35
  26. STM Transactions, similar to database transactions GIL STM antocuni, fijal,

    arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 22 / 35
  27. HTM Hardware support: Intel Haswell, 2013 “CPython-htm”? Removing the GIL:

    suddenly around the corner antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 24 / 35
  28. The catch You have to use threads antocuni, fijal, arigo

    (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 25 / 35
  29. The catch You have to use threads antocuni, fijal, arigo

    (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 25 / 35
  30. Threads Messy Hard to debug, non-reproductible Parallel with Explicit Memory

    Management: messy, hard to debug rare leaks or corruptions automatic GC solves it (like in Python) antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 26 / 35
  31. This talk is really about... Multicore usage without using threads

    Demo with the “transaction” module antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 27 / 35
  32. How? Longer, controlled transactions GIL STM antocuni, fijal, arigo (EuroPython

    2012) PyPy: current status and GIL-less future July 2 2012 28 / 35
  33. Results Same results in both cases i.e. can pretend it

    is one-core antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 29 / 35
  34. The opposite catch Always gives correct results... But maybe too

    many conflicts up to: systematic conflicts This still approaches the issue from “the right side” antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 30 / 35
  35. The opposite catch Always gives correct results... But maybe too

    many conflicts up to: systematic conflicts This still approaches the issue from “the right side” antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 30 / 35
  36. About CPython Long transactions: HTM too limited At least for

    the next 10-15 years On CPython we are stuck with threads for the next 10-15 years antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 31 / 35
  37. Summary STM fine with PyPy, but HTM required for CPython

    HTM too limited for long transactions Long transactions give a better programming model For years to come, only in PyPy Unless major effort from CPython devs antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 32 / 35
  38. Conclusion The GIL will be removed soon But for the

    foreseeable future, Python programmers stuck with using threads antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 33 / 35
  39. Conclusion ...while other langs get the better programming model My

    own point of view only Or maybe everybody will switch to PyPy antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 34 / 35
  40. Conclusion ...while other langs get the better programming model My

    own point of view only Or maybe everybody will switch to PyPy antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 34 / 35
  41. Thank you http://pypy.org/ You can hire Antonio (http://antocuni.eu) Questions? PyPy

    help desk on Thursday morning antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 35 / 35