magically run your code faster with pypy

27
Ronan Lamy MAGICALLY RUN YOUR CODE FASTER WITH PYPY

Upload: ronan-lamy

Post on 15-Apr-2017

142 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Magically run your code faster with PyPy

Ronan Lamy

MAGICALLY RUN YOURCODE FASTER WITH

PYPY

Page 2: Magically run your code faster with PyPy

ABOUT MEPyPy core devPython consultant and freelance developerContact:

[email protected]@ronanlamy

ABOUT PYPY

Page 3: Magically run your code faster with PyPy

ABOUT PYPY

http://pypy.org

"PyPy is a fast, compliant alternativeimplementation of the Python language"

Page 4: Magically run your code faster with PyPy

Guido van Rossum, PyCon 2015

"If you want your code magically to runfaster, you should probably just use PyPy"

Page 5: Magically run your code faster with PyPy

PLANMake it workMake it fastWhat about Python 3?

Page 6: Magically run your code faster with PyPy

MAKE IT WORK

Page 7: Magically run your code faster with PyPy

INSTALLATION OPTIONSFrom the package manager

On Ubuntu, use the PPA: ppa:pypy/ppaOfficial binaries from Portable PyPy (Linux only):

pyenv:

http://pypy.org/download.htmlhttps://github.com/squeaky-

pl/portable-pypyhttps://github.com/yyuu/pyenv

Page 8: Magically run your code faster with PyPy

USE VIRTUALENV$ virtualenv ­p path/to/pypy pypy­env$ . pypy­env/bin/activate$ python ­m pip install ­U pip

Page 9: Magically run your code faster with PyPy

GARBAGE COLLECTIONCPython has reference counting

Object cleanup is deterministic (sometimes!)PyPy is garbage collected

Object cleanup happens “randomly”

Page 10: Magically run your code faster with PyPy

GARBAGE COLLECTION ISSUESDon’t use __del__Always close resources

Use with: blocks-X track-resources

Page 11: Magically run your code faster with PyPy

C EXTENSIONSYou don’t need to write C!Use cfficpyext: emulation of CPython C API

Page 12: Magically run your code faster with PyPy

C EXTENSION ISSUESUsually works (new!)If not:

Use (or write!) cffi alternativeStub/rewrite in Python

Page 13: Magically run your code faster with PyPy

MAKE IT FAST

Page 14: Magically run your code faster with PyPy

CPYTHON

Page 15: Magically run your code faster with PyPy

C compilerCPythonsource (C)

Pythoncode Bytecode Byte

interp.

python

Do stuff or whatever

Page 16: Magically run your code faster with PyPy

PYPY

Page 17: Magically run your code faster with PyPy

RPython toolchainPyPysource

(RPython)

Pythoncode Bytecode Byte

interp.

Tracing

Machinecode

pypy

Do stuff or whatever

Page 18: Magically run your code faster with PyPy

OPTIMISING FOR PYPYAim for mostly-static typesFunction calls are ~freeInstance mapsList strategiesDicts are slow

Page 19: Magically run your code faster with PyPy

BENCHMARKINGMeasure, don’t guess!Your tests are not a good benchmark“WARNING: timeit is a very unreliable tool. use perf orsomething else for real measurements”

Page 20: Magically run your code faster with PyPy

PROFILINGvmprof

Page 21: Magically run your code faster with PyPy

PYTHON 3

Page 22: Magically run your code faster with PyPy

3.3Community fundedpypy3.3-v5.5.0 released 12 October 2016Last 3.3 release

Page 23: Magically run your code faster with PyPy

3.5Mozilla Open Source Support grantMilestones:1. Interpreter changes2. C-API3. ssl and other stdlib modules4. Benchmarking and optimisation

Page 24: Magically run your code faster with PyPy

THE END

Page 25: Magically run your code faster with PyPy

EXTRAS

Page 26: Magically run your code faster with PyPy

CFFIDesign principle: users only need to know C and PythonBundled with PyPyPerformance: OK on CPython, fast on PyPyDocs: http://cffi.readthedocs.org

Page 27: Magically run your code faster with PyPy

CFFI EXAMPLEexample_build.py

from cffi import FFIffi = FFI()ffi.cdef(""" int foo(int** X, int m, int n);""")ffi.set_source("_example", '#include "foo.h"')

if __name__ == "__main__": ffi.compile()

example.pyfrom _example import ffi, libx = ffi.new('int[42][42]')lib.foo(x, 42, 42)