magically run your code faster with pypy

Post on 15-Apr-2017

142 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ronan Lamy

MAGICALLY RUN YOURCODE FASTER WITH

PYPY

ABOUT MEPyPy core devPython consultant and freelance developerContact:

Ronan.Lamy@gmail.com@ronanlamy

ABOUT PYPY

ABOUT PYPY

http://pypy.org

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

Guido van Rossum, PyCon 2015

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

PLANMake it workMake it fastWhat about Python 3?

MAKE IT WORK

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

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

GARBAGE COLLECTIONCPython has reference counting

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

Object cleanup happens “randomly”

GARBAGE COLLECTION ISSUESDon’t use __del__Always close resources

Use with: blocks-X track-resources

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

C EXTENSION ISSUESUsually works (new!)If not:

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

MAKE IT FAST

CPYTHON

C compilerCPythonsource (C)

Pythoncode Bytecode Byte

interp.

python

Do stuff or whatever

PYPY

RPython toolchainPyPysource

(RPython)

Pythoncode Bytecode Byte

interp.

Tracing

Machinecode

pypy

Do stuff or whatever

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

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”

PROFILINGvmprof

PYTHON 3

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

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

THE END

EXTRAS

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

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)

top related