Slide 1

Slide 1 text

RUST FOR PYTHON NATIVE EXTENSIONS

Slide 2

Slide 2 text

PYTHON IS GREAT easy to learn batteries included huge package ecosystem

Slide 3

Slide 3 text

IT'S ALSO SLOW hugely dynamic interpreted

Slide 4

Slide 4 text

Which is ironic, because...

Slide 5

Slide 5 text

IT'S THE LANGUAGE FOR DATA SCIENCE/HPC most of deep learning numpy/scipy/scikit-learn pandas

Slide 6

Slide 6 text

EVERYTHING DONE THROUGH NATIVE EXTENSIONS C/C++ Fortran

Slide 7

Slide 7 text

Yes, you really need a Fortran compiler to do data science in Python.

Slide 8

Slide 8 text

THERE ARE SOME ALTERNATIVES

Slide 9

Slide 9 text

CYTHON A dialect of Python that compiles to C/C++. Has done great things for lowering barries to entry.

Slide 10

Slide 10 text

Need to care about memory allocation, dangling pointers STILL, YOU'RE WRITING C

Slide 11

Slide 11 text

... and the generated code looks like this. static int __pyx_f_7_get_row_end( struct __pyx_obj_7 *__pyx_v_self, int __pyx_v_row) { int __pyx_r; Py_ssize_t __pyx_t_1; __pyx_t_1 = (__pyx_v_row + 1); __pyx_r = (*((int *) ( /* dim=0 */ ((char *) (((int *) __pyx_v_self->indptr.data) + __pyx_t_1)) ))); goto __pyx_L0;

Slide 12

Slide 12 text

And interfacing with Python is really easy. RUST CAN'T COME SOON ENOUGH

Slide 13

Slide 13 text

USE PYTHON CFFI import cffi ffi = cffi.FFI() ffi.cdef(""" struct Widget; struct Widget *make_widget(); int frobnicate(struct Widget* widget, float degree); """) lib = ffi.dlopen(so_path) widget = lib.make_widget() lib.frobnicate(widget, 0.5)

Slide 14

Slide 14 text

THE RUST SIDE IS JUST THE C ABI pub struct Widget { // snip } #[no_mangle] pub fn frobnicate(widget_ptr: *mut Widget, degree: f32) -> i32 { let widget = unsafe { Box::from_raw(widget_ptr) }; // snip mem::forget(widget); // If you want to keep it around 42 }

Slide 15

Slide 15 text

IT ALL WORKS BEAUTIFULLY.

Slide 16

Slide 16 text

WE HAVE A RUST ML MODEL IN PRODUCTION AT LYST Based on rustlearn, a Rust ML library Integrates seamlessly into our Python stack. We pre-build the binaries in our CI environment, so no Rust necessary on servers Runs some of our recommendation services

Slide 17

Slide 17 text

SPREAD THE WORD!

Slide 18

Slide 18 text

@Maciej_Kula THANKS FOR LISTENING