Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Calling C antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 13 / 35

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

STM Software Transactional Memory “Remove the GIL” antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 17 / 35

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Conflicts Occasional conflict: antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 23 / 35

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

How? Longer, controlled transactions GIL STM antocuni, fijal, arigo (EuroPython 2012) PyPy: current status and GIL-less future July 2 2012 28 / 35

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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