Slide 1

Slide 1 text

1 Nicholas Matsakis / [email protected] / nmatsakis (IRC)

Slide 2

Slide 2 text

2 Photo credit: Giorgio Monteforti Systems Programming Before Rust…

Slide 3

Slide 3 text

Hack without fear!

Slide 4

Slide 4 text

4 Performance class ::String def blank? /\A[[:space:]]*\z/ == self end end Ruby: 964K iter/sec

Slide 5

Slide 5 text

static VALUE rb_str_blank_as(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: #if ruby_version_before_2_2() case 0x180e: #endif /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } Performance Ruby: 964K iter/sec C: 10.5M iter/sec 10x! https://github.com/SamSaffron/fast_blank

Slide 6

Slide 6 text

Performance 6 class ::String def blank? /\A[[:space:]]*\z/ == self end end extern “C” fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) } Get Rust string slice Get iterator over each character Are all characters whitespace? Rust: 11M iter/sec Ruby: 964K iter/sec C: 10.5M iter/sec

Slide 7

Slide 7 text

7 “Must be this tall to write multi-threaded code”

Slide 8

Slide 8 text

Parallel Programming 8 fn load_images(paths: &[PathBuf]) -> Vec { paths.iter() .map(|path| { Image::load(path) }) .collect() } For each path… …load an image… …create and return a vector.

Slide 9

Slide 9 text

Parallel Programming 9 fn load_images(paths: &[PathBuf]) -> Vec { paths.par_iter() .map(|path| { Image::load(path) }) .collect() } …make it parallel. extern crate rayon; Third-party library Can also do: processes with channels, mutexes, non-blocking data structures…

Slide 10

Slide 10 text

Safer Parallel Programming 10 fn load_images(paths: &[PathBuf]) -> Vec { let mut jpegs = 0; paths.par_iter() .map(|path| { if path.ends_with(“jpeg”) { jpegs += 1; } Image::load(path) }) .collect(); } Data-race Will not compile

Slide 11

Slide 11 text

Cargo and crates.io 11

Slide 12

Slide 12 text

12 (just a sampling)

Slide 13

Slide 13 text

13 From http://jvns.ca/blog/2016/09/11/rustconf-keynote/ Open and welcoming

Slide 14

Slide 14 text

Ownership 14

Slide 15

Slide 15 text

fn main() { let name = format!(“…”); helper(name); helper(name); } fn helper(name: String) { println!(..); } Ownership Take ownership of a String 15 Error: use of moved value: `name`

Slide 16

Slide 16 text

void main() { Vector name = …; helper(name); helper(name); } void helper(Vector name) { … } “Ownership” in Java Take reference to Vector 16 new Thread(…);

Slide 17

Slide 17 text

Borrowing: Shared Borrows 17

Slide 18

Slide 18 text

Borrow the string, creating a reference fn helper(name: &String) { println!(..); } Shared borrow Change type to a reference to a String 18 fn main() { let name = format!(“…”); let r = &name; helper(r); helper(r); }

Slide 19

Slide 19 text

Shared == Immutable 19 fn helper(name: &String) { println!(“{}”, name); } fn helper(name: &String) { name.push(‘x’); } OK. Just reads. Error. Writes. * Actually: mutation only in controlled circumstances. * error: cannot borrow immutable borrowed content `*name` as mutable name.push_str(“s”); ^^^^

Slide 20

Slide 20 text

Borrowing: Mutable Borrows 20

Slide 21

Slide 21 text

fn update(name: &mut String) { name.push_str(“…”); } fn main() { let mut name = …; update(&mut name); println!(“{}”, name); } Mutable borrow Take a mutable reference to a String 21 Lend the string mutably Mutate string in place Prints the updated string. mut

Slide 22

Slide 22 text

22 name: String name: &String name: &mut String Ownership: control all access, will free when done Shared reference: many readers, no writers Mutable reference: no other readers, one writer

Slide 23

Slide 23 text

Play time 23 http://is.gd/no0tTH Waterloo, Cassius Coolidge, c. 1906

Slide 24

Slide 24 text

How do we get safety? 24 https://www.flickr.com/photos/langtind/2217639550/in/photostream/

Slide 25

Slide 25 text

Dangers of mutation 25 let mut buffer: String = format!(“Rustacean”); let slice = &buffer[1..]; buffer.push_str(“s”); println!(“{:?}”, slice); ‘R’ ‘u’ … ‘n’ String data len capacity ‘R’ ‘u’ … ‘n’ data len ‘R’ ‘u’ … ‘n’ ‘s’ Dangling reference!

Slide 26

Slide 26 text

Rust solution 26 Compile-time read-write-lock: Creating a shared reference to X “read locks” X. - Other readers OK. - No writers. - Lock lasts until reference goes out of scope. Creating a mutable reference to X “writes locks” X. - No other readers or writers. - Lock lasts until reference goes out of scope. Never have a reader/writer at same time.

Slide 27

Slide 27 text

27 fn main() { let mut buffer: String = format!(“Rustacean”); let slice = &buffer[1..]; buffer.push_str(“s”); println!(“{:?}”, slice); } Borrow “locks” `buffer` for lifetime `’l` of resulting reference http://is.gd/MCPVWg ‘l Rule: No mutation during lifetime of borrow. Lifetime: span of code where reference is used.

Slide 28

Slide 28 text

‘l fn main() { let mut buffer: String = format!(“Rustacean”); for i in 0 .. buffer.len() { let slice = &buffer[i..]; buffer.push_str(“s”); println!(“{:?}”, slice); } buffer.push_str(“s”); } 28 Borrow “locks” `buffer` until `slice` goes out of scope OK: `buffer` is not borrowed here

Slide 29

Slide 29 text

29 Play time http://is.gd/no0tTH Waterloo, Cassius Coolidge, c. 1906

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

Rust in use within Gecko 31 Media stack (MP4 parser) • mp4 video parser written in Rust • will become default soon Style system (“stylo”) • CSS styling, in parallel • goal is to land around June Paint system (“WebRender”) • written in Rust, uses GPU • goal is end-of-year URL parser • been an exploit vector in the past • unknown date to ship …and beyond?

Slide 32

Slide 32 text

Resources 32 intorust.com users.rust-lang.org rust-lang.github.io/book/ ❤