speeding up python with rust - amazon s3 · speeding up python with rust. twist bioscience...

20
@TwistBioscience #WeMakeDNA Pablo Klijnjan Speeding up Python with Rust

Upload: others

Post on 24-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

@TwistBioscience #WeMakeDNA

Pablo Klijnjan

Speeding up Python with Rust

Page 2: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python
Page 3: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

3Twist Bioscience Corporation

Page 4: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 4

• Core Python library

• Used by one of our web services and directly by other teams

• Not as fast as required

• Scientists consuming it are used to Python • Decided to re-write in Rust and consume it as a Python extension

Some issues with Python

Page 5: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 5

What’s a Rust?

• System level programming language developed at Mozilla

• Fast

• Secure

• Productive

• Flexible

• Empowering

Page 6: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python
Page 7: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 7

def is_blank_py_naive(filepath):

with open(filepath, "r") as f:

for c in f.read():

if c != " ":

return False

return True

An example

Page 8: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 8

Input: 100MB file full of whitespaces

------- benchmark: 1 tests --------

Name Mean (ms)

-----------------------------------

test_is_blank_PY_naive 2,572.614

-----------------------------------

Page 9: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 9

BLANK_RE = re.compile("^\s*$")

def is_blank_py(filepath):

with open(filepath, "r") as f:

return BLANK_RE.match(f.read()) is not None

Page 10: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 10

-------------- benchmark: 2 tests -------------

Name Mean (ms)

-----------------------------------------------

test_is_blank_PY 550.4425

test_is_blank_PY_naive 2,597.0330

-----------------------------------------------

Page 11: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 11

fn is_blank(filepath: String) -> bool {

let mut file = File::open(filepath)?;

let mut content = String::new();

file.read_to_string(&mut content)?;

content.chars().all(|c| c.is_whitespace())

}

Rust implementation

Page 12: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 12

-------------- benchmark: 3 tests -------------

Name Mean (ms)

-----------------------------------------------

test_is_blank_RUST_naive 116.2861

test_is_blank_PY 565.9652

test_is_blank_PY_naive 2,618.9210

-----------------------------------------------

Page 13: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 13

Calling Rust from Python

• Define a function to expose in Rust• Configure the Rust toolchain to build a dynamic lib• Load the dynamic lib from Python

Page 14: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 14

#![feature(proc_macro, specialization)]

...

#[pymodinit(_fast_is_blank)]

fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

#[pyfn(m, "is_blank")]

fn is_blank(filepath: String) -> PyResult<bool> {

...

Ok(content.chars().all(|c| c.is_whitespace()))

}

Ok(())

}

PyO3

Turn supporting feature on

Bindings to the generated module

Expose the function

Page 15: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 15

Cargo settings

[dependencies]pyo3 = "0.2"

[lib]name = "fast_is_blank"crate-type = ["cdylib"]

Page 16: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 16

from setuptools import setup

from setuptools_rust import RustExtension

setup(

name='is_blank',

version='0.1.0',

packages=['is_blank'],

rust_extensions=[RustExtension('is_blank._fast_is_blank', 'Cargo.toml')],

install_requires=[],

setup_requires=['setuptools-rust>=0.9.2'],

include_package_data=True,

zip_safe=False,

)

Setup.py integration

Page 17: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python
Page 18: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Twist Bioscience Corporation 18

Links

• https://github.com/PyO3/pyo3

• https://github.com/PyO3/setuptools-rust

• https://doc.rust-lang.org/book/second-edition/index.html

• https://github.com/Twistbioscience/pycon-2018-rust-example

Page 19: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Questions?

Page 20: Speeding up Python with Rust - Amazon S3 · Speeding up Python with Rust. Twist Bioscience Corporation 3 ... • Not as fast as required • Scientists consuming it are used to Python

Thank you!