Oxidising Python
with
Rust
"whoami"
Game Developer
Rust Evangelist
JS Zealot
C++ Hater
Programmer
Python Fan
Web Technologies Radical
Why is Python not enough?
Performance!
Python is an interpreted language which also means it has a heavy runtime and overall slow ecosystem which makes it a problem to use it for Computationally heavy work.
Additionally, the GIL also locks the multithreaded compute to a single thread which adds to the problem.
Reusability
access to a much larger ecosystem
Rust is already helping.....
PyOxidiser
is a tool for packaging and distributing Python applications as "binaries"
Why
Rust?
What is Rust?
Rust is a system level programming language that allows for great flexibility, control and safety while maintaining blazing fast performance.
But,
Why not C?
Or even C++
-
Safety Through Type System
-
Better Tooling
-
Higher Level Abstractions
-
Modern Syntax
But what's so good about it?
Safety
through Reference Counting and Affine Typing
fn add_vec(v : &mut Vec<i32>) {
v.push(4);
v.push(5);
}
fn main() {
let mut x = vec!(1,2,3); // mutability is explicit
let v1 : &i32 = &x[0];. // X[0] is borrowed
add_vec(&mut x); // error x already has a borrowed ref
println!("{}", v1);
}
void add_vec(vector<int>& v) {
v.push_back(4);
v.push_back(5);
}
void main() {
vector<int> v(1,2);
int &v1 = v[0];
add_vec(v); // mut ref vec
cout << v1 << endl; // might cause a segfault
}
Zero-Cost Abstractions
Rust follows a similar philosophy to C++ in terms of offering zero-cost abstractions and it deliveries quite well on the promises it makes.
Most if not all that you use in Rust has modern syntax familiar to those who use languages like Python while providing almost zero overhead.
Tooling
Cargo is the package manager and crate host for rust. Akin to npm for JS, or PyPi.
And yes we call Rust packages crates.
Cargo allows you to use simple commands to install, create and manage crates.
Modern Syntax
Functional Programming Constructs
reduce() , map() , filter()
Pattern Matching
Direct Modules Integration
modules are a first party participant of Rust lang
And a lot more.............
Writing my our first Rust Python Module
What it takes?
Writing a module that is supposed to be accessed via another language is usually very hard to do.
Involves setup on both sides and interfacing with complex FFI functions and generating binary or channels of communication.
But due to the fact that Rust lacks a runtime and support C ABI interfacing.
We can actually use CFFI.
But?
Of course, there is a better way we are talking about Rust here. :P
And it involves using Python Bindings that generate Python Interpreter use ready binary with absolutely zero Python code or use of complex FFI systems.
rust-cpython is the original project but you can use PyO3 (it's fork) for greater flexibility, although this only supports Rust nightly builds.
Example rust-cpython lib code
What's the Performance Difference like?
Deserialization performance of Hyperjson. (Rust Py Module for JSON parsing) https://github.com/mre/hyperjson
Simplejson & Rapidjson both are/use C and C++ libs repectively.
Why & When to write a Rust Module?
oxidising-python-for-performance-and-fun
By Swarnim Arun
oxidising-python-for-performance-and-fun
Slides for introduction to Oxidising Python & FFI
- 766