27
PyPy and Unladen Swallow - Making Python Fast Friday, July 30, 2010

Making Python Fast - PyPy and Unladen Swallow

Embed Size (px)

DESCRIPTION

A talk given at PyOhio 2010 (and to the Chicago Python Users group) on PyPy and Unladen Swallow.

Citation preview

Page 1: Making Python Fast - PyPy and Unladen Swallow

PyPy and Unladen Swallow - Making

Python Fast

Friday, July 30, 2010

Page 2: Making Python Fast - PyPy and Unladen Swallow

Why is Python slow?

Why is CPython slow?

What are we going to do about it?

Friday, July 30, 2010

Page 3: Making Python Fast - PyPy and Unladen Swallow

Python the abstract language, not the implementation.

Very dynamic.

Almost nothing known at compile time.

Frame introspection.

Object model.

Globals/Builtins

Why is Python Slow

Friday, July 30, 2010

Page 4: Making Python Fast - PyPy and Unladen Swallow

Frame Introspectionimport sys

def f(): a = 3 g()

def g(): try: raise Exception except Exception, e: frame = sys.exc_info()[2].tb_frame print frame.f_back.f_locals["a"]

f()

Friday, July 30, 2010

Page 5: Making Python Fast - PyPy and Unladen Swallow

Object Modelclass A(object): def __init__(self, **kwargs): self.__dict__.update(kwargs)

o = A(a=1, b=2)print o.a

Friday, July 30, 2010

Page 6: Making Python Fast - PyPy and Unladen Swallow

Dynamic

def f(a, b): print a + b

Friday, July 30, 2010

Page 7: Making Python Fast - PyPy and Unladen Swallow

Globals/Builtins

def f(l): yield len(l) yield len(l)

for i in f([3]): print i len = lambda o: 3

Friday, July 30, 2010

Page 8: Making Python Fast - PyPy and Unladen Swallow

Why is CPython Slow

“Primitive” bytecode VM

Value boxing

Reference counting

Friday, July 30, 2010

Page 9: Making Python Fast - PyPy and Unladen Swallow

What Are We Going To Do About It

Unladen Swallow

PyPy

Friday, July 30, 2010

Page 10: Making Python Fast - PyPy and Unladen Swallow

Unladen Swallow

Google funded branch of Python

Started out off of Python 2.6

PEP 3146 - Merging Unladen Swallow into Py3k

LLVM based function JIT

Friday, July 30, 2010

Page 11: Making Python Fast - PyPy and Unladen Swallow

LLVM

Low Level Virtual Machine

Not a VM like CPython.

Take Python representation of a function and turn into LLVM representation of a function and generate machine code.

Includes all sorts of optimizations and code generators.

Friday, July 30, 2010

Page 12: Making Python Fast - PyPy and Unladen Swallow

JIT

Profile and see which functions are called the most.

Record what types are seen for each operation.

Emit optimized machine code (that bails back to the interpreter if guards fail).

Friday, July 30, 2010

Page 13: Making Python Fast - PyPy and Unladen Swallow

PyPy

Python in Python

JIT Generator

Tracing JIT

Friday, July 30, 2010

Page 14: Making Python Fast - PyPy and Unladen Swallow

RPython

Restricted Python

Statically typed subset of Python

Can be efficiently converted to C, JVM bytecode, CIL (.NET bytecode)

Friday, July 30, 2010

Page 15: Making Python Fast - PyPy and Unladen Swallow

JIT Generator

Take an interpreter written in RPython

Add a few hints to the source code

Automatically generate a JIT for it

Friday, July 30, 2010

Page 16: Making Python Fast - PyPy and Unladen Swallow

Tracing JIT

Profile code looking for hot loops

Record types seen within a loop

Generated optimized machine code for loops

Friday, July 30, 2010

Page 17: Making Python Fast - PyPy and Unladen Swallow

Benchmarks

The Python Benchmark Suite

Extracted from Unladen Swallow

Used by CPython, Unladen Swallow, PyPy

Friday, July 30, 2010

Page 18: Making Python Fast - PyPy and Unladen Swallow

CPython vs Unladen Swallow

Benchmark CPython 2.6 Unladen Swallow Difference

2to3

django

html5lib

nbody

rietveld

slowpickle

slowspitfire

slowunpickle

spambayes

25.13s 24.87 s 1.01

1.08 s 0.80 s 1.35

14.29 s 13.20 s 1.08

0.51 s .28 s 1.84

0.75 s 0.55 s 1.37

0.75 s 0.55 s 1.37

0.83 s 0.61 s 1.36

0.33 s 0.26 s 1.26

0.31 s 0.34 s 1.10

Friday, July 30, 2010

Page 19: Making Python Fast - PyPy and Unladen Swallow

CPython vs PyPy

Friday, July 30, 2010

Page 20: Making Python Fast - PyPy and Unladen Swallow

Faster is Possible

Friday, July 30, 2010

Page 21: Making Python Fast - PyPy and Unladen Swallow

Global/Builtin Lookup Caching

Loading a global takes 1 dictionary lookup.

Loading a builtin takes 2.

Globals/Builtins rarely, if ever, change.

Friday, July 30, 2010

Page 22: Making Python Fast - PyPy and Unladen Swallow

PyPy

Uses a dictionary for modules similar to V8 hidden classes.

Check that the dict has the right shape.

Read the field directly out of it.

Friday, July 30, 2010

Page 23: Making Python Fast - PyPy and Unladen Swallow

In Unladen Swallow

When the JIT compiler sees a LOAD_GLOBAL opcode it does the lookup at compile time, writes the exact address of the value into the machine code, and registers a listener with the globals/builtins dictionary.

If the globals/builtins dictionary is written to it invalidates the machine code.

Friday, July 30, 2010

Page 24: Making Python Fast - PyPy and Unladen Swallow

Inlining

Good programming practice is to split up functions.

Function calls are expensive.

Also, calls across the Python interpreter/C (or other target language) barrier are expensive.

Remove argument parsing, frame, and “raw” function call overhead.

Friday, July 30, 2010

Page 25: Making Python Fast - PyPy and Unladen Swallow

In Unladen Swallow

This hasn’t landed in trunk yet.

When compiling code if a CALL_FUNCTION always points to the same function check how “expensive” that function is, if it’s low copy its bytecode into our bytecode.

Also, at CPython compile time turn all library functions into LLVM IR, so we can inline that as well.

Friday, July 30, 2010

Page 26: Making Python Fast - PyPy and Unladen Swallow

In PyPy

Tracing JIT automatically goes through all function calls.

Final operations list automatically has all calls inlined.

Library functions are compiled to jitcode (the JIT’s IR) at PyPy compile time, so they can be inlined too.

Friday, July 30, 2010

Page 27: Making Python Fast - PyPy and Unladen Swallow

Questions?Complaints? Thrown

Vegetables?

Friday, July 30, 2010