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

The State of PyPy (April 2015)

The State of PyPy (April 2015)

Modern programming languages are expected to offer high-level abstractions at reasonable speed. Especially scripting languages like Ruby, PHP and Python are highly dynamic and flexible, which makes performance optimizations difficult to implement.

While Python 3 offers a lot more features compared to Python 2, these features don't always improve runtime performance. In the pystone benchmark, execution speed has even decreased over the recent years.
PyPy, a relatively new implementation of Python with a focus on performance tries to improve the situation - with obvious success. Code execution is much faster thanks to the built-in just in time compiler which optimizes code at runtime.

In this talk I give an overview of PyPy and how it works internally. Also I talk a bit about PyPy STM, a new approach for parallel code execution that can take advantage of modern multi-core hardware.

Matthias Endler

April 29, 2015
Tweet

More Decks by Matthias Endler

Other Decks in Programming

Transcript

  1. PyPy
    The state of
    2015-04 @matthiasendler

    View Slide

  2. Why PyPy?
    "Scientific American" Vol. XXXVI, No. 19, May 12, 1877 p.294 New York: Munn &Company (Publisher)

    View Slide

  3. Why PyPy?
    What keeps you from using switching to Python 3?

    View Slide

  4. Why PyPy?
    No more support for older versions
    Major bugfixes
    New language features
    Improved performance
    Incentives to switch to a new language version

    View Slide

  5. Why PyPy?
    More speed means…
    less servers
    new possibilities

    View Slide

  6. Why PyPy?
    Two ways to speed up a scripting language
    •Improve the implementation
    •Improve the runtime

    View Slide

  7. Why PyPy?
    Two ways to speed up a scripting language
    •Improve the implementation
    •Improve the runtime

    View Slide

  8. Why PyPy?
    Warning:

    Benchmarks are always error-prone.

    Always measure yourself!

    This is just a trend.

    View Slide

  9. 0
    35
    70
    105
    140
    1.8.7 1.9.3 2.0.0 2.1.0 2.2.0
    Relative Ruby performance
    Why PyPy?
    Source: isrubyfastyet.com
    speed (%)
    version
    baseline (100%)
    +12%
    +30%
    +35% +36%

    View Slide

  10. Why PyPy?
    0
    65
    130
    195
    260
    5.2 5.3 5.4 5.5 5.6 7.0
    Relative PHP performance
    Source: PHPNG a new core for PHP7 by Dmitry Stogov
    baseline (100%)
    speed (%)
    version
    +13%
    +36% +39%
    +50%
    +149%

    View Slide

  11. Why PyPy?
    25
    50
    75
    100
    2.6 2.7 3.4
    Relative Python performance
    Source: pystone benchmark on Core i7, 16 GB RAM
    baseline (100%)
    speed (%)
    version
    -10%
    -3%

    View Slide

  12. Why PyPy?
    0
    38
    75
    113
    150
    2007 2009 2011 2013 2015
    PHP Python Ruby
    baseline (100%)
    speed (%)
    time
    Relative performance improvements per language over time

    View Slide

  13. Why PyPy?
    The net result of the 3.0 generalizations is that 

    Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5.
    Most likely the biggest cause is the removal of special-casing for small
    integers. There’s room for improvement, but it will happen after 3.0 is
    released!
    docs.python.org/3/whatsnew/3.0.html

    View Slide

  14. Why PyPy?
    Two ways to speed up a scripting language
    •Improve the implementation
    •Improve the runtime

    View Slide

  15. Why PyPy?
    Ruby Rubinius
    PyPy
    HHVM
    Python
    PHP

    View Slide

  16. About PyPy
    Cottrell & Babcock's air spring printing press
    Source: Scientific American, June 22, 1872

    View Slide

  17. What is PyPy?
    Followup of Psyco runtime
    Version 1.0 in 2007
    Python written in Python
    Just-In-Time compiler
    Compatible with Python 2.7 and 3.4

    View Slide

  18. What is PyPy?
    Followup of Psyco runtime
    Version 1.0 in 2007
    Python written in Python RPython
    Just-In-Time compiler
    Compatible with Python 2.7 and 3.4

    View Slide

  19. PyPy implementation
    • Variables in any given control flow path must have only a single type
    • No multiple inheritance
    • No generators
    • Most dunder-methods can't be used.
    RPython is a subset of Python
    http:/
    /www.quora.com/PyPy/How-annoying-is-writing-RPython-compared-to-Python

    View Slide

  20. PyPy implementation
    Tracing JIT compiler
    http:/
    /www.aosabook.org/en/pypy.html
    Your Python program

    View Slide

  21. PyPy implementation
    Tracing JIT compiler
    http:/
    /www.aosabook.org/en/pypy.html
    „Hot“ loop

    View Slide

  22. PyPy implementation
    Tracing JIT compiler
    http:/
    /www.aosabook.org/en/pypy.html
    can_enter_jit()
    jit_merge_point()

    View Slide

  23. PyPy implementation
    Tracing JIT compiler
    http:/
    /www.aosabook.org/en/pypy.html
    Optimized machine code

    View Slide

  24. PyPy Speed
    speed.pypy.org

    View Slide

  25. Upright boring machine, 1872
    Source: Scientific American, Nov 23,1872
    Demo

    View Slide

  26. The future of PyPy
    Berryman's steam trap, 1872
    Source: Scientific American, Sept 14,1872

    View Slide

  27. Project milestones
    2008 Django support
    2010 First JIT-compiler
    2011 Compatibility with CPython 2.7
    2014 Basic ARM support
    CPython 3 support
    Improve compatibility with C extensions
    NumPyPy
    Multi-threading support

    View Slide

  28. Project milestones
    2008 Django support
    2010 First JIT-compiler
    2011 Compatibility with CPython 2.7
    2014 Basic ARM support
    CPython 3 support
    Improve compatibility with C extensions
    NumPyPy
    Multi-threading support

    View Slide

  29. PyPy STM

    View Slide

  30. PyPy STM
    http://dabeaz.com/GIL/gilvis/
    GIL locking

    View Slide

  31. PyPy STM
    10 loops, best of 3: 1.2 sec per loop
    10 loops, best of 3: 822 msec per loop
    from threading import Thread
    def count(n):
    while n > 0:
    n -= 1
    def run():
    t1 = Thread(target=count, args=(10000000,))
    t1.start()
    t2 = Thread(target=count, args=(10000000,))
    t2.start()
    t1.join(); t2.join()
    def count(n):
    while n > 0:
    n -= 1
    def run():
    count(10000000)
    count(10000000)
    Inside the Python GIL - David Beazley

    View Slide

  32. PyPy STM
    T1
    T2
    T3
    x
    y
    z
    Thread 1
    Shared memory T1
    T2
    T3
    Thread 2

    View Slide

  33. PyPy STM
    T1
    T2
    T3
    x = 3
    x
    y
    z
    Thread 1
    Shared memory T1
    T2
    T3
    y=5
    Thread 2

    View Slide

  34. PyPy STM
    T1
    T2
    T3
    3
    5
    x
    y
    z
    Thread 1
    Shared memory T1
    T2
    T3
    Thread 2

    View Slide

  35. PyPy STM
    T1
    T2
    T3
    x = 18

    y= 12
    3
    5
    x
    y
    z
    Thread 1
    Shared memory T1
    T2
    T3
    x = 2
    Thread 2

    View Slide

  36. PyPy STM
    T1
    T2
    T3
    2
    5
    x
    y
    z
    Thread 1
    Shared memory T1
    T2
    T3
    Thread 2

    View Slide

  37. Lathrop's sewing machine, 1872
    Source: Scientific American, Oct 26,1872
    Demo

    View Slide

  38. Conclusion
    …already awesome for sequential tasks
    …gradually improving parallel performance
    …a very important Python project
    PyPy is…

    View Slide

  39. Further reading
    Python 3 Performance
    https:/
    /speakerdeck.com/pyconslides/python-3-dot-3-trust-me-its-
    better-than-python-2-dot-7-by-dr-brett-cannon
    https:/
    /www.youtube.com/watch?v=UJPdhx5zTaw

    View Slide

  40. Further reading
    RPython
    http:/
    /tratt.net/laurie/blog/entries/
    fast_enough_vms_in_fast_enough_time

    View Slide

  41. Further reading
    PyPy STM
    http:/
    /pypy.readthedocs.org/en/latest/stm.html
    http:/
    /morepypy.blogspot.de/
    Demo code from
    http:/
    /www.ripton.net/blog/?p=104

    View Slide

  42. Blueprints provided by Marcel Douwe Dekker under Creative Commons.
    https://www.flickr.com/photos/marceldouwedekker/7564782310/in/
    photostream/

    Blueprint by Joy Oil Co Ltd (http://commons.wikimedia.org/wiki/
    File:Joy_Oil_gas_station_blueprints.jpg)
    Credits

    View Slide

  43. by Simon Child
    and by Luis Prado
    by Océan Bussard
    by Luis Rodrigues
    by Mister Pixel
    by Alexander Wiefel
    by Darin S
    by spotted paint

    The icons used in this presentation were downloaded from The Noun Project.

    Thanks to the following artists:

    View Slide