Upgrade to Pro — share decks privately, control downloads, hide ads and more …

vigneshwer dhinakaran - Pumping up Python modules using Rust

vigneshwer dhinakaran - Pumping up Python modules using Rust

If you’ve spent much time writing (or debugging) Python performance problems, you’ve probably had a hard time managing memory with its limited language support.

In this talk, we venture deep into the belly of the Rust Language to uncover the secret incantations for building high performance and memory safe Python extensions using Rust.

Rust has a lot to offer in terms of safety and performance for high-level programming languages such Python, Ruby, Js and more with its easy Foreign Function Interface capabilities which enables developers to easily develop bindings for foreign code.

https://us.pycon.org/2018/schedule/presentation/82/

PyCon 2018

May 11, 2018
Tweet

More Decks by PyCon 2018

Other Decks in Programming

Transcript

  1. What’s the main ask here for us? C/C++ ‘s Performance,

    Portability & Embeddability With Guaranteed Safety
  2. Rust is a system programming language that has great control

    like C/C++, delivers productivity like in Python, and is super safe.
  3. Zen of Rust Memory Safety without Garbage Collection Rich Typesystem

    High level iterators Freedom from data race Welcoming Community
  4. 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/
  5. 2016 -18 For the third year in a row, Rust

    is stackoverflow's most loved language https://insights.stackoverflow.com/survey/2018/
  6. 24

  7. 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
  8. 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
  9. 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 )
  10. Struct • Structs are like lightweight classes • They can

    have fields • They can be instantiated
  11. Struct Rust has option enum for such cases. Every Option

    is either Some and contains a value, or None, and does not.
  12. 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
  13. Generics talk is now generic with a Talk trait bound

    The compiler generates specialized code ~ Static Dispatch Zero -Cost Abstraction !!
  14. 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
  15. Rust-Cpython : Integration • Import the dependencies • Write the

    rust equivalent logic • Export the rust function
  16. Tools • rustup: the Rust toolchain installer • rustfmt :

    the Rust code formatting tool • rust-clippy : Rust code linter • Cargo : package manager • ...
  17. 69

  18. 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!
  19. What does Rust have to offer? • Modern C/C++ replacement

    • Memory Safety without Garbage Collection • No runtime needed • Rich Typesystem • Strong Functional Programming influence