Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

The price of performance shouldn’t have to be crashes and exploits.

Slide 5

Slide 5 text

• Safe unions and pointers • Explicit memory layout & stack allocation • Optional garbage collection • Mutability control & race-free concurrency Rust: perf + safety

Slide 6

Slide 6 text

ALGEBRAIC DATA TYPES

Slide 7

Slide 7 text

enum Color { Red, Green, Blue }

Slide 8

Slide 8 text

enum Tree { Empty, Leaf(int), Node(~Tree, ~Tree) }

Slide 9

Slide 9 text

tag tag int tag ~Tree ~Tree Empty Leaf(int) Node(~Tree, ~Tree)

Slide 10

Slide 10 text

struct Tree { enum TreeTag tag; union { int leaf; Pair node; } data; }; enum TreeTag { Empty, Leaf, Node }; struct Pair { Tree *left, *right; };

Slide 11

Slide 11 text

PATTERN MATCHING

Slide 12

Slide 12 text

match tree { Empty => 0, Leaf(_) => 1, Node(~ref left, ~ref right) => f(left) + f(right) }

Slide 13

Slide 13 text

enum Option { None, Some(T) }

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

fn double(p: ~int) -> int { *p * 2 }

Slide 16

Slide 16 text

fn double(p: Option<~int>) -> int { match p { None => 0, Some(~n) => n * 2 } }

Slide 17

Slide 17 text

OWNERSHIP

Slide 18

Slide 18 text

void *realloc(void *, ...); void free(void *); Owned pointers

Slide 19

Slide 19 text

void *realloc(void *, ...); void free(void *); template class Hashtable { vector> buckets; ... }; Owned pointers

Slide 20

Slide 20 text

memcpy(void *dest, const void *src, uintptr_t count); Temporary pointers

Slide 21

Slide 21 text

memcpy(void *dest, const void *src, uintptr_t count); template class Hashtable { ... V& find(K& key); }; Temporary pointers

Slide 22

Slide 22 text

the right to free memory What is ownership?

Slide 23

Slide 23 text

struct Hashtable { buckets: ~[Option>] } struct Bucket { key: K, value: V }

Slide 24

Slide 24 text

{ let b = ~[]; let table = Hashtable { buckets: b }; ... }

Slide 25

Slide 25 text

{ let b = ~[]; let table = Hashtable { buckets: b }; ... } heap allocate

Slide 26

Slide 26 text

{ let b = ~[]; let table = Hashtable { buckets: b }; ... } heap allocate stack allocate

Slide 27

Slide 27 text

{ let b = ~[]; let table = Hashtable { buckets: b }; ... } heap allocate stack allocate move

Slide 28

Slide 28 text

fn move_from() { let x: ~int = ~22; move_to(x); } fn move_to(p: ~int) { printfln!(“%d”, *p); }

Slide 29

Slide 29 text

fn move_from() { let x: ~int = ~22; move_to(x); } fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate

Slide 30

Slide 30 text

fn move_from() { let x: ~int = ~22; move_to(x); } fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate move

Slide 31

Slide 31 text

fn move_from() { let x: ~int = ~22; move_to(x); } fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate free p move

Slide 32

Slide 32 text

fn move_from() { let x: ~int = ~22; move_to(x); } fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate free p move printfln!(“%d”, *x); // error

Slide 33

Slide 33 text

BORROWING

Slide 34

Slide 34 text

fn f() { let x: ~int = ~22; borrow(x); printfln!(“%d”, *x); } fn borrow(x: &int) { printfln!(“borrowing: %d”, *x); }

Slide 35

Slide 35 text

fn f() { let x: int = 22; borrow(&x); printfln!(“%d”, *x); } fn borrow(x: &int) { printfln!(“borrowing: %d”, *x); }

Slide 36

Slide 36 text

let w = ~22; { let x = &w; let y = &*x; } ‘a ‘b ‘c ‘d Lifetimes

Slide 37

Slide 37 text

fn find<‘a,K,V>(tbl: &’a Hashtable, key: &K) -> Option<&’a V> { ... }

Slide 38

Slide 38 text

fn find<‘a,K,V>(tbl: &’a Hashtable, key: &K) -> Option<&’a V> { ... } pointer with lifetime ‘a to table

Slide 39

Slide 39 text

fn find<‘a,K,V>(tbl: &’a Hashtable, key: &K) -> Option<&’a V> { ... } pointer with lifetime ‘a to table pointer with lifetime ‘a to value

Slide 40

Slide 40 text

template class Hashtable V& Hashtable::find(K& key); unsafe!

Slide 41

Slide 41 text

CONCURRENCY

Slide 42

Slide 42 text

Heap

Slide 43

Slide 43 text

Heap

Slide 44

Slide 44 text

let (port, chan) = stream(); do spawn { let p = ~Point { x: 1.0, y: 2.0 }; chan.send(p); } let p = port.recv(); printfln!(p.to_str());

Slide 45

Slide 45 text

• Pluggable I/O • Optional scheduler • Data race-free shared data structures • Potential for race-free fork-join parallelism • Working on SIMD and GPU libs There’s more

Slide 46

Slide 46 text

UNSAFE

Slide 47

Slide 47 text

unsafe { transmute::<&str,&[u8]>(“L”) == &[76, 0] } unsafe fn f(...) { ... }

Slide 48

Slide 48 text

unsafe { transmute::<&str,&[u8]>(“L”) == &[76, 0] } unsafe fn f(...) { ... } ☢

Slide 49

Slide 49 text

1. Don’t use unsafe code. 2. If you absolutely must break Rule 1, • get out of unsafe code ASAP, and • always provide a safe interface at the boundaries. The rules

Slide 50

Slide 50 text

Low-level programming… without the segfaults.

Slide 51

Slide 51 text

Thank you.

Slide 52

Slide 52 text

Image credits Sean Martell http://blog.seanmartell.com Ben Clinch http://www.flickr.com/photos/benclinch/3048906022/ juicyrai http://www.flickr.com/photos/wink/180979062/ Rama, Wikimedia Commons http://en.wikipedia.org/wiki/File:Sir_Tony_Hoare_IMG_5125.jpg Jared Zimmerman http://www.flickr.com/photos/spoinknet/7932382540/ CCAC North Library http://www.flickr.com/photos/ccacnorthlib/3553821699/ Mike Hayes http://www.flickr.com/photos/rotties/449650275/