pypy 1.2: snakes never crawled so fast

47

Upload: pycon-italia

Post on 14-May-2015

2.382 views

Category:

Technology


0 download

DESCRIPTION

Antonio Cuni, Armin Rigo

TRANSCRIPT

Page 1: PyPy 1.2: snakes never crawled so fast

PyPy 1.2: snakes never crawled so fast

Antonio CuniArmin Rigo

Pycon Italia Qu4ttro

May 8 2010

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 1 / 35

Page 2: PyPy 1.2: snakes never crawled so fast

Outline

PyPy 1.2: what’s new and status update

Overview of the JIT

Demo: how to use PyPy right now

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 1 / 35

Page 3: PyPy 1.2: snakes never crawled so fast

Part 0: What is PyPy? :-)

Python interpreter written in Python

Framework for developing dynamic languages

etc. etc.

From the user point of view

An alternative to CPython

with more features!

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 2 / 35

Page 4: PyPy 1.2: snakes never crawled so fast

Part 0: What is PyPy? :-)

Python interpreter written in Python

Framework for developing dynamic languages

etc. etc.

From the user point of view

An alternative to CPython

with more features!

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 2 / 35

Page 5: PyPy 1.2: snakes never crawled so fast

Part 1

What’s new and status update

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 3 / 35

Page 6: PyPy 1.2: snakes never crawled so fast

What’s new in PyPy 1.2

Released on March 12th, 2010

Main theme: speed

JIT compiler

speed.pypy.org

Ubuntu packages

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 4 / 35

Page 7: PyPy 1.2: snakes never crawled so fast

Speed: PyPy vs CPython

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 5 / 35

Page 8: PyPy 1.2: snakes never crawled so fast

Speed: PyPy vs Psyco

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 6 / 35

Page 9: PyPy 1.2: snakes never crawled so fast

Speed: Demo

Django applicationMandelbrot fractal

I fished randomly on the net :-)

Run both on CPython and PyPyI django trunk!

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 7 / 35

Page 10: PyPy 1.2: snakes never crawled so fast

What works on PyPyPure Python modules should Just Work (TM)

I django trunkI twisted, nevowI pylonsI bittorrentI ...

lot of standard modules

__builtin__ __pypy__ _codecs _lsprof _minimal_curses_random _rawffi _socket _sre _weakref bz2 cStringIO crypt errnoexceptions fcntl gc itertools marshal math md5 mmap operator parserposix pyexpat select sha signal struct symbol sys termios thread timetoken unicodedata zipimport zlibarray binascii cPickle cmath collections ctypes datetime functools grpmd5 pwd pyexpat sha sqlite3 syslog

ctypes

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 8 / 35

Page 11: PyPy 1.2: snakes never crawled so fast

What works on PyPyPure Python modules should Just Work (TM)

I django trunkI twisted, nevowI pylonsI bittorrentI ...

lot of standard modules

__builtin__ __pypy__ _codecs _lsprof _minimal_curses_random _rawffi _socket _sre _weakref bz2 cStringIO crypt errnoexceptions fcntl gc itertools marshal math md5 mmap operator parserposix pyexpat select sha signal struct symbol sys termios thread timetoken unicodedata zipimport zlibarray binascii cPickle cmath collections ctypes datetime functools grpmd5 pwd pyexpat sha sqlite3 syslog

ctypes

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 8 / 35

Page 12: PyPy 1.2: snakes never crawled so fast

What does not work on PyPy

Pure Python modules should Just Work (TM)I ... unless they don’t :-)

Programs that rely on CPython-specific behaviorI refcounting: open(’xxx’, ’w’).write(’stuff’)I non-string keys in dict of types (try it!)I exact naming of a list comprehension variableI exact message matching in exception catching codeI ...

Extension modulesI really?I drum roll...

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 9 / 35

Page 13: PyPy 1.2: snakes never crawled so fast

What does not work on PyPy

Pure Python modules should Just Work (TM)I ... unless they don’t :-)

Programs that rely on CPython-specific behaviorI refcounting: open(’xxx’, ’w’).write(’stuff’)I non-string keys in dict of types (try it!)I exact naming of a list comprehension variableI exact message matching in exception catching codeI ...

Extension modulesI really?I drum roll...

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 9 / 35

Page 14: PyPy 1.2: snakes never crawled so fast

What does not work on PyPy

Pure Python modules should Just Work (TM)I ... unless they don’t :-)

Programs that rely on CPython-specific behaviorI refcounting: open(’xxx’, ’w’).write(’stuff’)I non-string keys in dict of types (try it!)I exact naming of a list comprehension variableI exact message matching in exception catching codeI ...

Extension modulesI really?I drum roll...

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 9 / 35

Page 15: PyPy 1.2: snakes never crawled so fast

What does not work on PyPy

Pure Python modules should Just Work (TM)I ... unless they don’t :-)

Programs that rely on CPython-specific behaviorI refcounting: open(’xxx’, ’w’).write(’stuff’)I non-string keys in dict of types (try it!)I exact naming of a list comprehension variableI exact message matching in exception catching codeI ...

Extension modulesI really?I drum roll...

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 9 / 35

Page 16: PyPy 1.2: snakes never crawled so fast

What does not work on PyPy

Pure Python modules should Just Work (TM)I ... unless they don’t :-)

Programs that rely on CPython-specific behaviorI refcounting: open(’xxx’, ’w’).write(’stuff’)I non-string keys in dict of types (try it!)I exact naming of a list comprehension variableI exact message matching in exception catching codeI ...

Extension modulesI really?I drum roll...

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 9 / 35

Page 17: PyPy 1.2: snakes never crawled so fast

cpyext

CPython extension modules in PyPy

pypy-c setup.py build

still beta

not 100% of CPython API is supported

not included in PyPy 1.2Known to work:

I wxPython (after a patch)I _sreI PyCryptoI PIL

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 10 / 35

Page 18: PyPy 1.2: snakes never crawled so fast

wxPython on PyPy (1)

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 11 / 35

Page 19: PyPy 1.2: snakes never crawled so fast

wxPython on PyPy (2)

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 12 / 35

Page 20: PyPy 1.2: snakes never crawled so fast

PyPy 1.2.1

Coming soonMany bugfixes

I 27 issues reported after release of 1.2

beta version of cpyext

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 13 / 35

Page 21: PyPy 1.2: snakes never crawled so fast

Part 2: Just-in-Time compilation

Snakes never crawled so fast

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 14 / 35

Page 22: PyPy 1.2: snakes never crawled so fast

Overview of implementations

CPython

Stackless

Psyco

Jython

IronPython

PyPy (without and with JIT)

Unladen Swallow

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 15 / 35

Page 23: PyPy 1.2: snakes never crawled so fast

Demo

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 16 / 35

Page 24: PyPy 1.2: snakes never crawled so fast

Features

it just works

it may give good speed-ups (better than Psyco)

it may have a few bugs left (Psyco too)

it is not a hack (unlike Psyco)

PyPy also has excellent memory usageI half that of CPython for a program using several

hunderds MBs

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 17 / 35

Page 25: PyPy 1.2: snakes never crawled so fast

Features

it just works

it may give good speed-ups (better than Psyco)

it may have a few bugs left (Psyco too)

it is not a hack (unlike Psyco)

PyPy also has excellent memory usageI half that of CPython for a program using several

hunderds MBs

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 17 / 35

Page 26: PyPy 1.2: snakes never crawled so fast

Features

it just works

it may give good speed-ups (better than Psyco)

it may have a few bugs left (Psyco too)

it is not a hack (unlike Psyco)

PyPy also has excellent memory usageI half that of CPython for a program using several

hunderds MBs

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 17 / 35

Page 27: PyPy 1.2: snakes never crawled so fast

What is a JIT

CPython compiles the program source into bytecodes

without a JIT, the bytecodes are then interpreted

with a JIT, the bytecodes are further translated tomachine code (assembler)

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 18 / 35

Page 28: PyPy 1.2: snakes never crawled so fast

What is a JIT (2)

The translation can be:syntactic: translate the whole functions into machinecode

I “the obvious way”I e.g. Pyrex/Cython, Unladen SwallowI not good performance, or needs tricks

semantic: translate bits of the function just-in-timeI only used partsI exploit runtime information (e.g. types)I Psyco, PyPy

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 19 / 35

Page 29: PyPy 1.2: snakes never crawled so fast

What is a JIT (2)

The translation can be:syntactic: translate the whole functions into machinecode

I “the obvious way”I e.g. Pyrex/Cython, Unladen SwallowI not good performance, or needs tricks

semantic: translate bits of the function just-in-timeI only used partsI exploit runtime information (e.g. types)I Psyco, PyPy

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 19 / 35

Page 30: PyPy 1.2: snakes never crawled so fast

What is a tracing JIT

start by interpreting normally

find loops as they are executed

turn them into machine code

80% of the time is spent in 20% of the code

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 20 / 35

Page 31: PyPy 1.2: snakes never crawled so fast

What is a tracing JIT (history)

tracing assembler (Dynamo, ~2000)

tracing Java (~2005)

tracing JavaScript (~2008)

PyPy is a “tracing JIT generator”

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 21 / 35

Page 32: PyPy 1.2: snakes never crawled so fast

What is a tracing JIT (history)

tracing assembler (Dynamo, ~2000)

tracing Java (~2005)

tracing JavaScript (~2008)

PyPy is a “tracing JIT generator”

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 21 / 35

Page 33: PyPy 1.2: snakes never crawled so fast

The architecture of PyPy

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 22 / 35

Page 34: PyPy 1.2: snakes never crawled so fast

Speed of the PyPy JIT

Python programs that are, or are not, nicely handled bythe JIT:

loops, even across many calls, are nicely handledloops with very many taken paths are not

I e.g. Python programs that look like interpretersI typical in tracing JITs

bad support so far for generators and recursion

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 23 / 35

Page 35: PyPy 1.2: snakes never crawled so fast

The optimizations we get

!= optimizations we wrote :-)removed frame handling

I local variables are in CPU registers or on the C stackI but sys._getframe() works correctly

“virtuals”: temporary objects are not constructedI e = a + b + c + dI and much more complex examples

attribute and method lookups, etc.

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 24 / 35

Page 36: PyPy 1.2: snakes never crawled so fast

Example

def g(a, b):

if a < 5: # 2

return -1

return a - b # 3

def f(x):

total = 0

# 1

for i in range(x):

d = g(i, x)

total += d # 4

ADD EAX, 1

CMP EAX, EBX

JNL <guard 1>

CMP EAX, 0

JL <guard 2>

MOV ECX, EAX

SUB ECX, EBX

JO <guard 3>

ADD EDX, ECX

JO <guard 4>

JMP

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 25 / 35

Page 37: PyPy 1.2: snakes never crawled so fast

Example

def g(a, b):

if a < 5: # 2

return -1

return a - b # 3

def f(x):

total = 0

# 1

for i in range(x):

d = g(i, x)

total += d # 4

ADD EAX, 1

CMP EAX, EBX

JNL <guard 1>

CMP EAX, 0

JL <guard 2>

MOV ECX, EAX

SUB ECX, EBX

JO <guard 3>

ADD EDX, ECX

JO <guard 4>

JMP

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 25 / 35

Page 38: PyPy 1.2: snakes never crawled so fast

Practical results

fast :-)

so far, x86-32 only

relatively easy to maintain (or port to x86-64, etc.)reminder: works transparently for any Python code

I or any language (Prolog JIT :-) at PPDP 2010)

viable alternative to CPython

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 26 / 35

Page 39: PyPy 1.2: snakes never crawled so fast

Part 3

How to use PyPy right now

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 27 / 35

Page 40: PyPy 1.2: snakes never crawled so fast

Mandelbrot demo

Works purely on PyPyNot always the case

I missing extension modules (cpyext mitigates theproblem)

I libraries that rely on CPython detailsI ...

clear performance-critical part

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 28 / 35

Page 41: PyPy 1.2: snakes never crawled so fast

CPython and PyPy side by side

CPython: runs the main application

PyPy: subprocess, runs only the hotspots

How do they communicate?execnet

I The Ring of Python, Holger Krekel, 9:00I oups, too late :-)

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 29 / 35

Page 42: PyPy 1.2: snakes never crawled so fast

Rendering (1)

Mandelbrotdef render(request):w = int(request.GET.get(’w’, 320))h = int(request.GET.get(’h’, 240))

from py_mandel import mandelbrotimg = mandelbrot(w, h)

return HttpResponse(img, content_type="image/bmp")

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 30 / 35

Page 43: PyPy 1.2: snakes never crawled so fast

Rendering (2)Mandelbrot on PyPy

def pypy_render(request):w = int(request.GET.get(’w’, 320))h = int(request.GET.get(’h’, 240))

channel = pypy.remote_exec("""from py_mandel import mandelbrotw, h = channel.receive()img = mandelbrot(w, h)channel.send(img)""")channel.send((w, h))img = channel.receive()

return HttpResponse(img, content_type="image/bmp")

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 31 / 35

Page 44: PyPy 1.2: snakes never crawled so fast

execnet setup

At startup

import execnetmygroup = execnet.Group()pypy = mygroup.makegateway("popen//python=pypy-c")pypy.remote_exec("""import sysimport osos.chdir("mandelbrot")sys.path.insert(0, ’’)""")

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 32 / 35

Page 45: PyPy 1.2: snakes never crawled so fast

Demo

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 33 / 35

Page 46: PyPy 1.2: snakes never crawled so fast

Benchmarks

0 2 4 6 8 10 12

0

200

400

600

800

1000

1200

1400

pypypypy+pypycpythoncpython+pypy

requests/s

resp

on

se ti

me

(m

s)

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 34 / 35

Page 47: PyPy 1.2: snakes never crawled so fast

Contact / Q&A

Antonio Cuni: at http://merlinux.eu

Armin Rigo: arigo (at) tunes.orgLinks:

I PyPy: http://pypy.org/I PyPy speed center: http://speed.pypy.org/I Blog: http://morepypy.blogspot.com

antocuni, arigato (Pycon Italia Qu4ttro) PyPy 1.2 May 8 2010 35 / 35