Slide 1

Slide 1 text

Pumping Python modules using Rust PyCon US 2018 Vigneshwer Dhinakaran | Mozilla TechSpeaker | @dvigneshwer

Slide 2

Slide 2 text

Need for native extension Performance Freedom in memory management Access to low-level APIs

Slide 3

Slide 3 text

Solutions to native extension Python community & it’s strong lib ecosystem to the rescue

Slide 4

Slide 4 text

Native extension aren’t new to Python devs

Slide 5

Slide 5 text

Native extension aren’t new to Python devs

Slide 6

Slide 6 text

Native extension aren’t new to Python devs

Slide 7

Slide 7 text

Native extension aren’t new to Python devs

Slide 8

Slide 8 text

Reality: Not many of us can write C/C++ extensions

Slide 9

Slide 9 text

Writing C/C++ extension is scary & painful Manual memory management Segfaults Data Races

Slide 10

Slide 10 text

Other options? ● Cython ● Numba ● PyPy ● ...

Slide 11

Slide 11 text

What’s the main ask here for us? C/C++ ‘s Performance, Portability & Embeddability With Guaranteed Safety

Slide 12

Slide 12 text

Rust is a system programming language that has great control like C/C++, delivers productivity like in Python, and is super safe.

Slide 13

Slide 13 text

Zen of Rust Memory Safety without Garbage Collection Rich Typesystem High level iterators Freedom from data race Welcoming Community

Slide 14

Slide 14 text

Facts & Figures

Slide 15

Slide 15 text

Speeding up crypto on Wire desktop apps https://medium.com/@wireapp/speeding-up-crypto-on-wire-desktop-apps-3ff37fc98c3f Up to 141× faster

Slide 16

Slide 16 text

Fixing Python Performance with Rust https://blog.sentry.io/2016/10/19/fixing-python-performance-with-rust.html

Slide 17

Slide 17 text

Snips Uses Rust to Build an Embedded Voice Assistant

Slide 18

Slide 18 text

Tokio MiniHTTP The library is a proof-of-concept implementation of a simple HTTP/1.1 server using Tokio. http://aturon.github.io/blog/2016/08/11/futures/

Slide 19

Slide 19 text

2016 -18 For the third year in a row, Rust is stackoverflow's most loved language https://insights.stackoverflow.com/survey/2018/

Slide 20

Slide 20 text

Friends of Rust Organizations running Rust in production. (https://www.rust-lang.org/en-US/friends.html)

Slide 21

Slide 21 text

History: How did it all begin?

Slide 22

Slide 22 text

Browsers Compete for performance & need Safety at its core

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

24

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Gradual adoption works.

Slide 27

Slide 27 text

Rust is easy to learn!

Slide 28

Slide 28 text

Variables

Slide 29

Slide 29 text

Functions

Slide 30

Slide 30 text

Flow control

Slide 31

Slide 31 text

Flow control

Slide 32

Slide 32 text

Looping

Slide 33

Slide 33 text

Types

Slide 34

Slide 34 text

Package Manager

Slide 35

Slide 35 text

Ownership The act, state, or right of possessing something

Slide 36

Slide 36 text

Garbage Collection Heap Memory Circle(5) Circle(5) Circle(5)

Slide 37

Slide 37 text

Referencing Now both bookingService and repairService hold a reference to vehicle

Slide 38

Slide 38 text

Same implementation in Rust Doesn't compile!

Slide 39

Slide 39 text

Ownership ● Rust moves ownership by default ● The owner has the right to destroy the thing it owns ● The memory is freed as soon as the owned variable goes out of it's scope ● Hence vehicle may already be destroyed at the point when it's passed to repair_service ● Rust catches these errors at compile time

Slide 40

Slide 40 text

Borrowing The act, state, or right of possessing something

Slide 41

Slide 41 text

Making it work in Rust Lending things is the key!

Slide 42

Slide 42 text

Borrowing ● A reference is passed without transferring ownership ● One can borrow immutable (&) or mutable (&mut) but not both at the same time ● Shared ownership can be achieved through special pointers with runtime checks

Slide 43

Slide 43 text

Mutability Rules ● All variables are immutable by default ● Only one mutable reference at a time ( But as many immutable &’s as you want ) ● Mutable references block all other access (The &mut must go out of scope before using other &’s )

Slide 44

Slide 44 text

Yes !!! Safe memory management without GC

Slide 45

Slide 45 text

High-level iterators in Rust

Slide 46

Slide 46 text

High-level iterators Execution time : ~ 250 ms

Slide 47

Slide 47 text

High-level iterators

Slide 48

Slide 48 text

Rayon ~ 50 ms

Slide 49

Slide 49 text

custom data type in Rust

Slide 50

Slide 50 text

Struct ● Structs are like lightweight classes ● They can have fields ● They can be instantiated

Slide 51

Slide 51 text

Struct But not partially !

Slide 52

Slide 52 text

Struct Rust has option enum for such cases. Every Option is either Some and contains a value, or None, and does not.

Slide 53

Slide 53 text

Struct can have methods

Slide 54

Slide 54 text

Traits Traits are Rust’s sole notion of interface Traits must be brought into scope with use It has to be looked up in a vtable at run time ~ DYNAMIC DISPATCH

Slide 55

Slide 55 text

Generics talk is now generic with a Talk trait bound The compiler generates specialized code ~ Static Dispatch Zero -Cost Abstraction !!

Slide 56

Slide 56 text

Developing & Shipping Rust ext. in Python modules

Slide 57

Slide 57 text

How does this work? ● A Foreign Function Interface is a way for a programming language to define functions and enable a different (foreign) programming language to call those functions ● Based on platform-dependant C application binary interface ● Rust produces efficient C bindings

Slide 58

Slide 58 text

Rust is ideal for creating and using FFI

Slide 59

Slide 59 text

Rust FFI capabilities ● Building the project ● Create the extensions

Slide 60

Slide 60 text

Interfacing & building the python module ● build.py ● setup.py

Slide 61

Slide 61 text

Other libs ● setuptools-rust https://pypi.org/project/setuptools-rust/ ● milksnake https://github.com/getsentry/milksnake

Slide 62

Slide 62 text

Rust-Cpython

Slide 63

Slide 63 text

Rust-Cpython : Integration ● Import the dependencies ● Write the rust equivalent logic ● Export the rust function

Slide 64

Slide 64 text

Rust-Cpython : Benchmarks

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Tools ● rustup: the Rust toolchain installer ● rustfmt : the Rust code formatting tool ● rust-clippy : Rust code linter ● Cargo : package manager ● ...

Slide 67

Slide 67 text

67 Community Photo credit: David McSpadden https://www.flickr.com/photos/familyclan/15535822737/

Slide 68

Slide 68 text

68 RFC Process

Slide 69

Slide 69 text

69

Slide 70

Slide 70 text

Summary

Slide 71

Slide 71 text

When should you use Rust? ● Rewrite modules of your code that are computationally heavy (Complex math optimizations etc) ● For accessing the hardware(ex :- IOT applications, device drivers, GPU’s etc) ● Implementing advance concurrency paradigms ● Python to Rust calls are non-trivial, so be wise!

Slide 72

Slide 72 text

What does Rust have to offer? ● Modern C/C++ replacement ● Memory Safety without Garbage Collection ● No runtime needed ● Rich Typesystem ● Strong Functional Programming influence

Slide 73

Slide 73 text

Feel Awesome

Slide 74

Slide 74 text

Thanks! @dvigneshwer [email protected] https://tinyurl.com/PyConUS2018