Rust for Python Native Extensions

Rust for Python Native Extensions

Lightning talk presented at the Rust London Meetup in March 2016.

For an example of calling Rust from Python, check out https://github.com/maciejkula/python-rustlearn

F401afbfb8100568304f0caf45c79575?s=128

Maciej Kula

March 17, 2016
Tweet

Transcript

  1. 9.

    CYTHON A dialect of Python that compiles to C/C++. Has

    done great things for lowering barries to entry.
  2. 11.

    ... 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;
  3. 13.

    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)
  4. 14.

    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 }
  5. 16.

    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