Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla...
Transcript of Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla...
![Page 1: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/1.jpg)
Rust: SystemsProgramming for
EveryoneFelix Klock (@pnkfelix), Mozilla
space : next slide; esc : overview; arrows navigatehttp://bit.ly/1LQM3PS
![Page 2: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/2.jpg)
Why ...?
![Page 3: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/3.jpg)
Why use Rust?Fast code, low memory footprint
Go from bare metal (assembly; C FFI) ...
... to high-level (collections, closures, generic
containers) ...
with zero cost (no GC, unboxed closures,
monomorphization of generics)
Safety and Parallelism
![Page 4: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/4.jpg)
Safety and ParallelismSafety
No segmentation faults
No undefined behavior
No data races
(Multi-paradigm) Parallelismmsg passing via channels
shared state via Arc and atomics, Mutex, etc
use native threads... or scoped threads... or work-stealing...
![Page 5: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/5.jpg)
Why would you (Felix) workon Rust?
It's awesome!
(Were prior slides really not a sufficient answer?)
oh, maybe you meant ...
![Page 6: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/6.jpg)
Why would Mozilla sponsor Rust?Hard to prototype research-y browser changes atop C++ code base
Rust ⇒ Servo, WebRenderWant Rust for next-gen infrastructure (services, IoT)
"Our mission is to ensure the Internet is a globalpublic resource, open and accessible to all. AnInternet that truly puts people first, whereindividuals can shape their own experience and areempowered, safe and independent."
"accessible to all"
![Page 7: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/7.jpg)
Where is Rust now?1.0 release was back in May 2015
Rolling release cycle (up to Rust 1.7 as of March 2nd 2016)
Open source from the begininghttps://github.com/rust-lang/rust/
Open model for future change (RFC process)https://github.com/rust-lang/rfcs/
Awesome developer community (~1,000 people in #rust, ~250people in #rust-internals, ~1,300 unique commiters to rust.git)
![Page 8: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/8.jpg)
Talk plan"Why Rust" Demonstration"Ownership is easy" (... or is it?)Sharing Stuff
Sharing capabilities (Language stuff)
Sharing work (Parallelism stuff)
Sharing code (Open source distribution stuff)
![Page 9: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/9.jpg)
Lightning Demo
![Page 10: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/10.jpg)
Demo: sequential web page fetchfn sequential_web_fetch() { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", "http://imgur.com", "http://mozilla.org"];
for &site in sites { // step through the array... let client = Client::new(); let res = client.get(site).send().unwrap(); assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site, char_count); } }
(lets get rid of the Rust-specific pattern binding in for; this is not atutorial)
![Page 11: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/11.jpg)
Demo: sequential web page fetchfn sequential_web_fetch() { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", "http://imgur.com", "http://mozilla.org"];
for site_ref in sites { // step through the array... let site = *site_ref; // (separated for expository purposes)
{ // (and a separate block, again for expository purposes) let client = Client::new();
let res = client.get(site).send().unwrap(); assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site, char_count); } } }
![Page 12: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/12.jpg)
Demo: concurrent web page fetchfn concurrent_web_fetch() -> Vec<::std::thread::JoinHandle<()>> { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", "http://imgur.com", "http://mozilla.org"]; let mut handles = Vec::new(); for site_ref in sites { let site = *site_ref; let handle = ::std::thread::spawn(move || { // block code put in closure: ~~~~~~~ let client = Client::new();
let res = client.get(site).send().unwrap(); assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site, char_count); });
handles.push(handle); }
return handles; }
![Page 13: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/13.jpg)
Print outsSequential version:
site: http://www.eff.org/ chars: 42425 site: http://rust-lang.org/ chars: 16748 site: http://imgur.com chars: 152384 site: http://mozilla.org chars: 63349
(on every run, when internet, and sites, available)
Concurrent version:site: http://imgur.com chars: 152384 site: http://rust-lang.org/ chars: 16748 site: http://mozilla.org chars: 63349 site: http://www.eff.org/ chars: 42425
(on at least one run)
![Page 14: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/14.jpg)
"what is this 'soundness' of whichyou speak?"
![Page 15: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/15.jpg)
Demo: soundness Ifn sequential_web_fetch_2() { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", // ~~~~~ sites, an array (slice) of strings, is stack-local "http://imgur.com", "http://mozilla.org"];
for site_ref in sites { // ~~~~~~~~ site_ref is a *reference to* elem of array. let client = Client::new(); let res = client.get(*site_ref).send().unwrap(); // moved deref here ~~~~~~~~~ assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site_ref, char_count); } }
![Page 16: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/16.jpg)
Demo: soundness IIfn concurrent_web_fetch_2() -> Vec<::std::thread::JoinHandle<()>> { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", // ~~~~~ sites, an array (slice) of strings, is stack-local "http://imgur.com", "http://mozilla.org"]; let mut handles = Vec::new(); for site_ref in sites { // ~~~~~~~~ site_ref still a *reference* into an array let handle = ::std::thread::spawn(move || { let client = Client::new(); let res = client.get(*site_ref).send().unwrap(); // moved deref here ~~~~~~~~~ assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site_ref, char_count); // Q: will sites array still be around when above runs? }); handles.push(handle); } return handles; }
![Page 17: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/17.jpg)
some (white) lies:"Rust is just about
ownership"
![Page 18: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/18.jpg)
"Ownership isintuitive"
![Page 19: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/19.jpg)
"Ownership is intuitive"Let's buy a car
let money: Money = bank.withdraw_cash(); let my_new_car: Car = dealership.buy_car(money);
let second_car = dealership.buy_car(money); // <-- cannot reuse
money transferred into dealership, and car transferred to us.
![Page 20: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/20.jpg)
"Ownership is intuitive"Let's buy a car
let money: Money = bank.withdraw_cash(); let my_new_car: Car = dealership.buy_car(money); // let second_car = dealership.buy_car(money); // <-- cannot reuse
money transferred into dealership, and car transferred to us.
my_new_car.drive_to(home); garage.park(my_new_car);
my_new_car.drive_to(...) // now doesn't work
(can't drive car without access to it, e.g. taking it out of the garage)
![Page 21: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/21.jpg)
"Ownership is intuitive"Let's buy a car
let money: Money = bank.withdraw_cash(); let my_new_car: Car = dealership.buy_car(money); // let second_car = dealership.buy_car(money); // <-- cannot reuse
money transferred into dealership, and car transferred to us.
my_new_car.drive_to(home); garage.park(my_new_car); // my_new_car.drive_to(...) // now doesn't work
(can't drive car without access to it, e.g. taking it out of the garage)
let my_car = garage.unpark(); my_car.drive_to(work);
...reflection time...
![Page 22: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/22.jpg)
Correction: Ownership is intuitive,except for programmers ...
(copying data like integers, and characters, and .mp3's, is "free")
... and anyone else who names things
![Page 23: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/23.jpg)
Über Sinn und Bedeutung("On sense and reference" -- Gottlob Frege, 1892)
If ownership were all we had, car-purchase slide seems nonsensical
my_new_car.drive_to(home);
Does this transfer home into the car?
Do I lose access to my home, just because I drive to it?
We must distinguish an object itself from ways to name that object
Above, home cannot be (an owned) Home
home must instead be some kind of reference to a Home
![Page 24: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/24.jpg)
So we will need referencesWe can solve any problem by introducing an extra
level of indirection
-- David J. Wheeler
![Page 25: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/25.jpg)
a truth: Ownership is important
![Page 26: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/26.jpg)
Ownership is importantOwnership enables: which removes:
RAII-style destructors a source of memory leaks (or fd leaks, etc)
no dangling pointers many resource management bugs
no data races many multithreading heisenbugs
Do I need to take ownership here, accepting theassociated resource management responsibility?
Would temporary access suffice?
Good developers ask this already!
Rust forces function signatures to encode the answers
(and they are checked by the compiler)
![Page 27: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/27.jpg)
Sharing Data:Ownership and
References
![Page 28: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/28.jpg)
Rust typesMove Copy Copy if T:CopyVec<T>, String, ... i32, char, ... [T; n], (T1,T2,T3), ...
struct Car { color: Color, engine: Engine }
fn demo_ownership() { let mut used_car: Car = Car { color: Color::Red, engine: Engine::BrokenV8 }; let apartments = ApartmentBuilding::new();
references to data (&mut T, &T):
let my_home: &Home; // <-- an "immutable" borrow let christine: &mut Car; // <-- a "mutable" borrow my_home = &apartments[6]; // (read mut as "exclusive") let neighbors_home = &apartments[5]; christine = &mut used_car; christine.engine = Engine::VintageV8; }
![Page 29: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/29.jpg)
Why multiple &-reference types?Distinguish exclusive access from shared access
Enables safe, parallel API's
![Page 30: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/30.jpg)
A Metaphor
![Page 31: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/31.jpg)
(reminder: metaphorsnever work 100%)
![Page 32: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/32.jpg)
let christine = Car::new();
This is "Christine"
pristine unborrowed car
(apologies to Stephen King)
![Page 33: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/33.jpg)
let read_only_borrow = &christine;
single inspector (immutable borrow)
(apologies to Randall Munroe)
![Page 34: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/34.jpg)
read_only_borrows[2] = &christine; read_only_borrows[3] = &christine; read_only_borrows[4] = &christine;
many inspectors (immutable borrows)
![Page 35: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/35.jpg)
When inspectors are finished, we are left again with:
pristine unborrowed car
![Page 36: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/36.jpg)
let mutable_borrow = &mut christine; // like taking keys ... give_arnie(mutable_borrow); // ... and giving them to someone
driven car (mutably borrowed)
![Page 37: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/37.jpg)
Can't mix the two in safe code!
Otherwise: (data) races!
![Page 38: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/38.jpg)
read_only_borrows[2] = &christine; let mutable_borrow = &mut christine; read_only_borrows[3] = &christine; // ⇒ CHAOS!
mixing mutable and immutable is illegal
![Page 39: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/39.jpg)
Ownership TExclusive access &mut T ("mutable")
Shared access &T ("read-only")
![Page 40: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/40.jpg)
Exclusive access
![Page 41: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/41.jpg)
&mut: can I borrow the car?fn borrow_the_car_1() { let mut christine = Car::new(); { let car_keys = &mut christine; let arnie = invite_friend_over(); arnie.lend(car_keys); } // end of scope for arnie and car_keys christine.drive_to(work); // I still own the car! }
But when her keys are elsewhere, I cannot drive christine!
fn borrow_the_car_2() { let mut christine = Car::new(); { let car_keys = &mut christine; let arnie = invite_friend_over(); arnie.lend(car_keys); christine.drive_to(work); // <-- compile error } // end of scope for arnie and car_keys }
![Page 42: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/42.jpg)
Extending the metaphorPossessing the keys, Arnie could take the car for a new paint job.
fn lend_1(arnie: &Arnie, k: &mut Car) { k.color = arnie.fav_color; }
Or lend keys to someone else (reborrowing) before paint job
fn lend_2(arnie: &Arnie, k: &mut Car) { arnie.partner.lend(k); k.color = arnie.fav_color; }
Owner loses capabilities attached to &mut-borrows only temporarily (*)
(*): "Car keys" return guaranteed by Rust; sadly, not by physical world
![Page 43: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/43.jpg)
End of metaphor(on to models)
![Page 44: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/44.jpg)
Pointers, Smart andOtherwise
![Page 45: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/45.jpg)
(More pictures)
![Page 46: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/46.jpg)
Stack allocationlet b = B::new();
stack allocation
![Page 47: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/47.jpg)
let b = B::new();
let r1: &B = &b; let r2: &B = &b;
stack allocation and immutable borrows
(b has lost write capability)
![Page 48: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/48.jpg)
let mut b = B::new();
let w: &mut B = &mut b;
stack allocation and mutable borrows
(b has temporarily lost both read and write capabilities)
![Page 49: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/49.jpg)
Heap allocation: Box<B>let a = Box::new(B::new());
pristine boxed B
a (as owner) has both read and write capabilities
![Page 50: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/50.jpg)
Immutably borrowing a boxlet a = Box::new(B::new()); let r_of_box: &Box<B> = &a; // (not directly a ref of B)
let r1: &B = &*a; let r2: &B = &a; // <-- coercion!
immutable borrows of heap-allocated B
a retains read capabilities (has temporarily lost write)
![Page 51: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/51.jpg)
Mutably borrowing a boxlet mut a = Box::new(B::new());
let w: &mut B = &mut a; // (again, coercion happening here)
mutable borrow of heap-allocated B
a has temporarily lost both read and write capabilities
![Page 52: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/52.jpg)
Heap allocation: Vec<B>let mut a = Vec::new(); for i in 0..n { a.push(B::new()); }
![Page 53: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/53.jpg)
vec, filled to capacity
![Page 54: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/54.jpg)
Vec Reallocation... a.push(B::new());
before after
![Page 55: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/55.jpg)
![Page 56: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/56.jpg)
Slices: borrowing parts of an array
![Page 57: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/57.jpg)
Basic Vec<B>let mut a = Vec::new(); for i in 0..n { a.push(B::new()); }
pristine unborrowed vec
(a has read and write capabilities)
![Page 58: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/58.jpg)
Immutable borrowed sliceslet mut a = Vec::new(); for i in 0..n { a.push(B::new()); } let r1 = &a[0..3]; let r2 = &a[7..n-4];
mutiple borrowed slices vec
(a has only read capability now; shares it with r1 and r2)
![Page 59: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/59.jpg)
Safe overlap between &[..]let mut a = Vec::new(); for i in 0..n { a.push(B::new()); } let r1 = &a[0..7]; let r2 = &a[3..n-4];
overlapping slices
![Page 60: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/60.jpg)
Basic Vec<B> again
pristine unborrowed vec
(a has read and write capabilities)
![Page 61: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/61.jpg)
Mutable slice of whole veclet w = &mut a[0..n];
mutable slice of vec
(a has no capabilities; w now has read and write capability)
![Page 62: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/62.jpg)
Mutable disjoint sliceslet (w1,w2) = a.split_at_mut(n-4);
disjoint mutable borrows
(w1 and w2 share read and write capabilities for disjoint portions)
![Page 63: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/63.jpg)
Shared Ownership
![Page 64: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/64.jpg)
Shared Ownershiplet rc1 = Rc::new(B::new()); let rc2 = rc1.clone(); // increments ref-count on heap-alloc'd value
shared ownership via ref counting
(rc1 and rc2 each have read access; but neither can statically assumeexclusive (mut) access, nor can they provide &mut borrows without
assistance.)
![Page 65: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/65.jpg)
Dynamic Exclusivity
![Page 66: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/66.jpg)
RefCell<T>: Dynamic Exclusivitylet b = Box::new(RefCell::new(B::new()));
let r1: &RefCell<B> = &b; let r2: &RefCell<B> = &b;
box of refcell
![Page 67: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/67.jpg)
RefCell<T>: Dynamic Exclusivitylet b = Box::new(RefCell::new(B::new())); let r1: &RefCell<B> = &b; let r2: &RefCell<B> = &b; let w = r2.borrow_mut(); // if successful, w acts like &mut B
fallible mutable borrow
// below panics if w still in scope
![Page 68: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/68.jpg)
// below panics if w still in scope let w2 = b.borrow_mut();
![Page 69: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/69.jpg)
Previous generalizes toshared ownership
![Page 70: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/70.jpg)
Rc<RefCell<T>>let rc1 = Rc::new(RefCell::new(B::new())); let rc2 = rc1.clone(); // increments ref-count on heap-alloc'd value
shared ownership of refcell
![Page 71: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/71.jpg)
Rc<RefCell<T>>let rc1 = Rc::new(RefCell::new(B::new())); let rc2 = rc1.clone(); let r1: &RefCell<B> = &rc1; let r2: &RefCell<B> = &rc2; // (or even just r1)
borrows of refcell can alias
![Page 72: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/72.jpg)
Rc<RefCell<T>>let rc1 = Rc::new(RefCell::new(B::new())); let rc2 = rc1.clone(); let w = rc2.borrow_mut();
there can be only one!
![Page 73: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/73.jpg)
What static guarantees doesRc<RefCell<T>> have?
Not much!
If you want to port an existing imperative algorithm with all sorts ofsharing, you could try using Rc<RefCell<T>>.
You then might spend much less time wrestling with Rust's type(+borrow) checker.
The point: Rc<RefCell<T>> is nearly an anti-pattern. It limits staticreasoning. You should avoid it if you can.
![Page 74: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/74.jpg)
Other kinds of shared ownershipTypedArena<T>
Cow<T>
Rc<T> vs Arc<T>
![Page 75: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/75.jpg)
Sharing Work:Parallelism /Concurrency
![Page 76: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/76.jpg)
Threading APIs (plural!)std::thread
dispatch : OS X-specific "Grand Central Dispatch"
crossbeam : Lock-Free Abstractions, Scoped "Must-be" Concurrency
rayon : Scoped Fork-join "Maybe" Parallelism (inspired by Cilk)
(Only the first comes with Rust out of the box)
![Page 77: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/77.jpg)
std::threadfn concurrent_web_fetch() -> Vec<::std::thread::JoinHandle<()>> { use hyper::{self, Client}; use std::io::Read; // pulls in chars method
let sites = &["http://www.eff.org/", "http://rust-lang.org/", "http://imgur.com", "http://mozilla.org"]; let mut handles = Vec::new(); for site_ref in sites { let site = *site_ref; let handle = ::std::thread::spawn(move || { // block code put in closure: ~~~~~~~ let client = Client::new();
let res = client.get(site).send().unwrap(); assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site, char_count); });
handles.push(handle); }
return handles; }
![Page 78: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/78.jpg)
dispatchfn concurrent_gcd_fetch() -> Vec<::dispatch::Queue> { use hyper::{self, Client}; use std::io::Read; // pulls in chars method use dispatch::{Queue, QueueAttribute};
let sites = &["http://www.eff.org/", "http://rust-lang.org/", "http://imgur.com", "http://mozilla.org"]; let mut queues = Vec::new(); for site_ref in sites { let site = *site_ref; let q = Queue::create("qcon2016", QueueAttribute::Serial); q.async(move || { let client = Client::new();
let res = client.get(site).send().unwrap(); assert_eq!(res.status, hyper::Ok); let char_count = res.chars().count(); println!("site: {} chars: {}", site, char_count); });
queues.push(q); }
return queues; }
![Page 79: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/79.jpg)
crossbeamlock-free data structures
scoped threading abstraction
upholds Rust's safety (data-race freedom)guarantees
![Page 80: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/80.jpg)
lock-free data structures
![Page 81: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/81.jpg)
crossbeam MPSC benchmarkmean ns/msg (2 producers, 1 consumer; msg count 10e6; 1G heap)
Rustchannel
crossbeamMSQ
crossbeamSegQueue
ScalaMSQ
JavaConcurrentLinkedQueue
108ns 98ns53ns
461ns
192ns
![Page 82: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/82.jpg)
crossbeam MPMC benchmarkmean ns/msg (2 producers, 2 consumers; msg count 10e6; 1G heap)
Rustchannel(N/A)
crossbeamMSQ
crossbeamSegQueue
ScalaMSQ
JavaConcurrentLinkedQueue
102ns58ns
239ns204ns
See "Lock-freedom without garbage collection"https://aturon.github.io/blog/2015/08/27/epoch/
![Page 83: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/83.jpg)
scoped threading?std::thead does not allow sharing stack-local data
fn std_thread_fail() { let array: [u32; 3] = [1, 2, 3];
for i in &array { ::std::thread::spawn(|| { println!("element: {}", i); }); } }
error: array does not live long enough
![Page 84: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/84.jpg)
crossbeam scoped threadingfn crossbeam_demo() { let array = [1, 2, 3];
::crossbeam::scope(|scope| { for i in &array { scope.spawn(move || { println!("element: {}", i); }); } }); }
::crossbeam::scope enforces parent thread joins on all spawnedchildren before returning
ensures that it is sound for children to access local referencespassed into them.
![Page 85: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/85.jpg)
crossbeam scope: "must-be concurrency"
Each scope.spawn(..) invocation fires up a freshthread
(Literally just a wrapper around std::thread)
![Page 86: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/86.jpg)
rayon: "maybeparallelism"
![Page 87: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/87.jpg)
rayon demo 1: map reduceSequential
fn demo_map_reduce_seq(stores: &[Store], list: Groceries) -> u32 { let total_price = stores.iter() .map(|store| store.compute_price(&list)) .sum(); return total_price; }
Parallel (potentially)fn demo_map_reduce_par(stores: &[Store], list: Groceries) -> u32 { let total_price = stores.par_iter() .map(|store| store.compute_price(&list)) .sum(); return total_price; }
![Page 88: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/88.jpg)
Rayon's Rulethe decision of whether or not to use parallel threads
is made dynamically, based on whether idle coresare available
i.e., solely for offloading work, not for when concurrent operation isnecessary for correctness
(uses work-stealing under the hood to distribute work among a fixedset of threads)
![Page 89: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/89.jpg)
rayon demo 2: quicksortfn quick_sort<T:PartialOrd+Send>(v: &mut [T]) { if v.len() > 1 { let mid = partition(v); let (lo, hi) = v.split_at_mut(mid); rayon::join(|| quick_sort(lo), || quick_sort(hi)); } }
fn partition<T:PartialOrd+Send>(v: &mut [T]) -> usize { // see https://en.wikipedia.org/wiki/ // Quicksort#Lomuto_partition_scheme ... }
![Page 90: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/90.jpg)
rayon demo 3: buggy quicksortfn quick_sort<T:PartialOrd+Send>(v: &mut [T]) { if v.len() > 1 { let mid = partition(v); let (lo, hi) = v.split_at_mut(mid); rayon::join(|| quick_sort(lo), || quick_sort(hi)); } }
fn quick_sort<T:PartialOrd+Send>(v: &mut [T]) { if v.len() > 1 { let mid = partition(v); let (lo, hi) = v.split_at_mut(mid); rayon::join(|| quick_sort(lo), || quick_sort(lo)); // ~~ data race! } }
(See blog post "Rayon: Data Parallelism in Rust" bit.ly/1IZcku4)
![Page 91: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/91.jpg)
Big Idea3rd parties identify (and provide) new abstractions forconcurrency and parallelism unanticipated in std lib.
![Page 92: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/92.jpg)
Soundness and 3rdParty Concurrency
![Page 93: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/93.jpg)
The Secret SauceSend
Sync
lifetime bounds
![Page 94: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/94.jpg)
Send and SyncT: Send means an instance of T can be transferred between threads
(i.e. move or copied as appropriate)
T: Sync means two threads can safely share a reference to aninstance of T
![Page 95: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/95.jpg)
ExamplesT: Send : T can be transferred between threads
T: Sync : two threads can share refs to a T
String is SendVec<T> is Send (if T is Send)(double-check: why not require T: Sync for Vec<T>: Send?)Rc<T> is not Send (for any T)but Arc<T> is Send (if T is Send and Sync)(to ponder: why require T:Send for Arc<T>?)&T is Send if T: Sync&mut T is Send if T: Send
![Page 96: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/96.jpg)
Send and Sync are onlyhalf the story
other half is lifetime bounds; comesee me if curious
![Page 97: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/97.jpg)
Sharing Code:Cargo
![Page 98: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/98.jpg)
Sharing Codestd::thread is provided with std lib
But dispatch, crossbeam, and rayon are 3rd party
(not to mention hyper and a host of other crates used in this talk'sconstruction)
What is Rust's code distribution story?
![Page 99: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/99.jpg)
Cargocargo is really simple to use
cargo new -- create a project cargo test -- run project's unit tests cargo run -- run binaries associated with project cargo publish -- push project up to crates.io
Edit the associated Cargo.toml file to:
add dependenciesspecify version / licensing infoconditionally compiled featuresadd build-time behaviors (e.g. code generation)
"What's this about crates.io?"
![Page 100: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/100.jpg)
crates.ioOpen-source crate distribution site
Has every version of every crate
Cargo adheres to semver
![Page 101: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/101.jpg)
SemverThe use of in cargo basically amounts to this:Semantic Versioning
Major versions (MAJOR.minor.patch) are free to break whatever theywant.
New public API's can be added with minor versions updates(major.MINOR.patch), as long as they do not impose breaking changes.
In Rust, breaking changes includes data-structure representationchanges.
Adding fields to structs (or variants to enums) can cause their memoryrepresentation to change.
![Page 102: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/102.jpg)
Why major versions can includebreaking changes
Cargo invokes the Rust compiler in a way that salts the symbolsexported by a compiled library.
This ends up allowing two distinct (major) versions of a library to beused simultaneously in the same program.
This is important when pulling in third party libraries.
![Page 103: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/103.jpg)
Fixing versionscargo generates a Cargo.lock file that tracks the versions you built
the project with
Intent: application (i.e. final) crates should check their Cargo.lockinto version control
Ensures that future build attempts will choose the same versions
However: library (i.e. intermediate) crates should not check theirCargo.lock into version control.
Instead, everyone should follow sem.ver.; then individual applicationscan mix different libraries into their final product, upgradingintermediate libraries as necessary
![Page 104: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/104.jpg)
Crate dependency graphCompiler ensures one cannot pass struct defined via X version 2.x.y
into function expecting X version 1.m.n, or vice versa.
A: Graph Structure B: Token API
C: Lexical Scanner D: GLL Parser P: Linked Program
![Page 105: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/105.jpg)
In PracticeIf you (*) follow the sem.ver. rules, then you do not usually have tothink hard about those sorts of pictures.
"you" is really "you and all the crates you use"
You may not believe me, but cargo is really simple to useComing from a C/C++ world, this feels like magic(probably feels like old hat for people used to package dependencymanagers)
![Page 106: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/106.jpg)
Final Words
![Page 107: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/107.jpg)
Final Words(and no more pictures)
![Page 108: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/108.jpg)
InteropRust to C
easy: extern { ... } and unsafe { ... }
C to Rust
easy: #[no_mangle] extern "C" fn foo(...) { ... }
Ruby, Python, etc to Rust
see e.g. https://github.com/wycats/rust-bridge
![Page 109: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/109.jpg)
CustomersMozilla (of course)
Skylight
MaidSafe
... others
![Page 110: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/110.jpg)
Pivot from C/C++ to RustMaidsafe is one example of this
![Page 111: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/111.jpg)
Rust as enabler ofindividuals
From "mere script programmer"to "lauded systems hacker"
![Page 112: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/112.jpg)
Or if you prefer:Enabling sharing systems hacking knowledge with
everyone
Programming in Rust has made melook at C++ code in a whole new light
Thanks
![Page 113: Rust: Systems Programming for Everyone - QCon London 2020 · 2016-04-05 · Why would Mozilla sponsor Rust? Hard to prototype research-y browser changes atop C++ code base Rust ¶](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed2df7b00aa243d381d01f9/html5/thumbnails/113.jpg)
Thankswww.rust-lang.org
Hack Without Fear