-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcd-kickoff-2016.html
311 lines (301 loc) · 15.5 KB
/
cd-kickoff-2016.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Felix Klock (@pnkfelix)" />
<meta name="author" content="February 2016" />
<title>Hack Without Fear (with Rust)</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css"/>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="reveal.js/css/theme/mozilla-sandstone.css" id="theme">
<link rel="stylesheet" href="slide-style.css"/>
<link rel="stylesheet" href="code-style.css"/>
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'reveal.js/css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="title">Hack Without Fear (with Rust)</h1>
<h2 class="author">Felix Klock (<code>@pnkfelix</code>)</h2>
<h2 class="author">February 2016</h2>
<h3 class="date">FYI: Slides form <em>2D space</em>: <kbd class="key">␠</kbd> goes to next slide; <kbd class="key">←</kbd> <kbd class="key">↑</kbd> <kbd class="key">→</kbd> <kbd class="key">↓</kbd> navigate; <kbd class="key">␛</kbd> gives overview.</h3>
</section>
<section><section id="hack-without-fear" class="titleslide slide level1 center"><h1>Hack Without Fear</h1></section><section id="section" class="slide level2 center equation">
<h1></h1>
<p>An "Equation"</p>
<table>
<tbody>
<tr class="odd">
<td style="text-align: left;"></td>
<td style="text-align: left;">Abstraction without overhead</td>
</tr>
<tr class="even">
<td style="text-align: left;"></td>
<td style="text-align: left;">Memory safety without GC</td>
</tr>
<tr class="odd">
<td style="text-align: left;">+</td>
<td style="text-align: left;">Concurrency without data races</td>
</tr>
<tr class="even">
<td style="text-align: left;"></td>
<td style="text-align: left;">⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯</td>
</tr>
<tr class="odd">
<td style="text-align: left;">=</td>
<td style="text-align: left;">Hack without Fear</td>
</tr>
</tbody>
</table>
<div class="fragment">
<p>What is meant by "Abstraction", "Memory Safety", "Concurrency"?</p>
</div>
</section><section id="high-level-coding-..." class="slide level2">
<h1>High-level coding ...</h1>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="co">/// sums all positive values in `v`</span>
<span class="kw">fn</span> sum_pos(v: &[<span class="dt">i32</span>]) -> <span class="dt">i32</span> {
<span class="kw">let</span> <span class="kw">mut</span> sum = <span class="dv">0</span>;
<span class="kw">for</span> i <span class="kw">in</span> v.iter().filter(|i| **i > <span class="dv">0</span>) {
sum += *i;
}
sum
}</code></pre></div>
<!-- NOTE: Above is *not* the actual source for playpen
link. See a01_abstraction_source.md for that. -->
<p><a href="https://play.rust-lang.org/?code=%23%5Binline%28never%29%5D%0A%2F%2F%2F%20sums%20all%20positive%20values%20in%20%60v%60%0Afn%20sum_pos%28v%3A%20%26%5Bi32%5D%29%20-%3E%20i32%20%7B%0A%20%20%20%20let%20mut%20sum%20%3D%200%3B%0A%20%20%20%20for%20i%20in%20v.iter%28%29.filter%28%7Ci%7C%20**i%20%3E%200%29%20%7B%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20*i%3B%0A%20%20%20%20%7D%0A%20%20%20%20sum%0A%7D%0A%0Afn%20main%28%29%20%7B%0A%20%20%20%20println%21%28%22sum%3A%20%7B%7D%22%2C%20sum_pos%28%26%5B-13%2C%2010%2C%2020%2C%2030%2C%2040%5D%29%29%0A%7D&version=nightly">sum_pos</a></p>
</section><section id="with-low-level-performance" class="slide level2">
<h1>... with low-level performance</h1>
<div class="sourceCode"><pre class="sourceCode nasm"><code class="sourceCode nasm"> xorl <span class="ot">%eax, %eax</span>
leaq ref4197(<span class="ot">%rip), %rcx</span>
leaq ref4197<span class="dv">+20</span>(<span class="ot">%rip), %rdx</span>
<span class="kw">jmp</span> .LBB0_1
<span class="fu">.LBB0_3:</span>
addl <span class="ot">%esi, %eax</span>
.<span class="kw">align</span> <span class="dv">16</span><span class="bn">, 0x90</span>
<span class="fu">.LBB0_1:</span>
cmpq <span class="ot">%rcx, %rdx</span>
<span class="kw">je</span> .LBB0_4
movl (<span class="ot">%rcx), %esi</span>
addq<span class="bn"> $4, </span><span class="ot">%rcx</span>
testl <span class="ot">%esi, %esi</span>
<span class="kw">jle</span> .LBB0_1
<span class="kw">jmp</span> .LBB0_3
<span class="fu">.LBB0_4:</span>
retq</code></pre></div>
<!--
```rust
#[inline(never)]
/// sums all positive values in `v`
fn sum_pos(v: &[i32]) -> i32 {
let mut sum = 0;
for i in v.iter().filter(|i| **i > 0) {
sum += *i;
}
sum
}
fn main() {
println!("sum: {}", sum_pos(&[-13, 10, 20, 30, 40]))
}
```
-->
<!--
HACK: commenting out code block but it will still get rendered into link below.
(The code is presented and linked in a01_abstraction.md.)
Further more, the code block will still get converted into
actual Rust code in the current module by `tango`, so
that is why this is in a file distinct from
a01_abstraction.md.
-->
</section><section id="memory-safety" class="slide level2">
<h1>Memory Safety</h1>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> this_wont_compile(v: &<span class="kw">mut</span> <span class="dt">Vec</span><<span class="dt">i32</span>>) -> <span class="dt">i32</span> {
<span class="kw">let</span> <span class="kw">mut</span> sum = <span class="dv">0</span>;
<span class="kw">for</span> &i <span class="kw">in</span> v.iter() {
sum += i;
<span class="kw">if</span> i > <span class="dv">0</span> { v.push(<span class="dv">0</span>); }
}
<span class="kw">return</span> sum;
}</code></pre></div>
<p>(<a href="https://play.rust-lang.org/?code=fn%20this_wont_compile%28v%3A%20%26mut%20Vec%3Ci32%3E%29%20-%3E%20i32%20%7B%0A%20%20%20%20let%20mut%20sum%20%3D%200%3B%0A%20%20%20%20for%20%26i%20in%20v.iter%28%29%20%7B%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20i%3B%0A%20%20%20%20%20%20%20%20if%20i%20%3E%200%20%7B%20v.push%280%29%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%20sum%3B%0A%7D%0A%0Afn%20main%28%29%20%7B%7D&version=nightly">playpen</a>)</p>
<!--
```rust
fn this_wont_compile(v: &mut Vec<i32>) -> i32 {
let mut sum = 0;
for &i in v.iter() {
sum += i;
if i > 0 { v.push(0); }
}
return sum;
}
fn main() {}
```
-->
<pre class="fragment"><code><anon>:5:20: 5:21 error: cannot borrow `*v` as mutable because it is
also borrowed as immutable [E0502]
<anon>:5 if i > 0 { v.push(0); }
^~
<anon>:3:15: 3:16 note: previous borrow of `*v` occurs here; the
immutable borrow prevents subsequent moves or
mutable borrows of `*v` until the borrow ends
<anon>:3 for &i in v.iter() {
^~</code></pre>
</section><section id="parallel-and-safe" class="slide level2" data-transition="fade-out">
<h1>Parallel <span style="visibility: hidden">and Safe</span></h1>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> parallel_qsort(<span class="kw">mut</span> vec: &<span class="kw">mut</span> [<span class="dt">i32</span>]) {
<span class="kw">use</span> {rayon, rand};
<span class="kw">if</span> vec.len() <= <span class="dv">1</span> { <span class="kw">return</span>; }
<span class="kw">let</span> pivot = rand::random::<<span class="dt">usize</span>>() % vec.len();
<span class="kw">let</span> mid = vec.partition(pivot);
<span class="kw">let</span> (less, greater) = vec.split_at_mut(mid);
rayon::join(|| parallel_qsort(less),
|| parallel_qsort(greater)
);
}</code></pre></div>
<p>(Shout out to <a href="https://crates.io/crates/rayon/">Rayon</a>.)</p>
<!--
```rust
trait Partition { fn partition(&mut self, at_idx: usize) -> usize; }
impl<'a, X> Partition for &'a mut [X] where X: Ord {
fn partition(&mut self, at_idx: usize) -> usize {
self.swap(0, at_idx);
let (mut i, mut j) = (1, self.len() - 1);
while i < j {
if self[i] < self[0] {
i += 1;
continue;
}
if self[j] > self[0] {
j -= 1;
continue;
}
// at this point, self[i] >= pivot and self[j] <= pivot.
self.swap(i, j);
i += 1;
j -= 1;
}
self.swap(0, i);
return i;
}
}
```
-->
</section><section id="parallel-and-safe-1" class="slide level2" data-transition="fade">
<h1>Parallel and Safe</h1>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> parallel_qsort(<span class="kw">mut</span> vec: &<span class="kw">mut</span> [<span class="dt">i32</span>]) {
<span class="kw">use</span> {rayon, rand};
<span class="kw">if</span> vec.len() <= <span class="dv">1</span> { <span class="kw">return</span>; }
<span class="kw">let</span> pivot = rand::random::<<span class="dt">usize</span>>() % vec.len();
<span class="kw">let</span> mid = vec.partition(pivot);
<span class="kw">let</span> (less, greater) = vec.split_at_mut(mid);
rayon::join(|| parallel_qsort(less),
|| parallel_qsort(less)
);
}</code></pre></div>
</section><section id="parallel-and-safe-2" class="slide level2" data-transition="fade">
<h1>Parallel and Safe</h1>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> parallel_qsort(<span class="kw">mut</span> vec: &<span class="kw">mut</span> [<span class="dt">i32</span>]) {
<span class="kw">use</span> {rayon, rand};
<span class="kw">if</span> vec.len() <= <span class="dv">1</span> { <span class="kw">return</span>; }
<span class="kw">let</span> pivot = rand::random::<<span class="dt">usize</span>>() % vec.len();
<span class="kw">let</span> mid = vec.partition(pivot);
<span class="kw">let</span> (less, greater) = vec.split_at_mut(mid);
rayon::join(|| parallel_qsort(less),
|| parallel_qsort(less) <span class="co">// <- BUG: DATA RACE</span>
);
}</code></pre></div>
<pre><code>code.rs:71:17: 71:40 error: closure requires unique access to `less`
but it is already borrowed [E0500]
code.rs:71 || parallel_qsort(less) // <- BUG: DATA RACE
^~~~~~~~~~~~~~~~~~~~~~~
code.rs:71:35: 71:39 note: borrow occurs due to use of `less`
in closure
code.rs:71 || parallel_qsort(less) // <- BUG: DATA RACE
^~~~
code.rs:70:17: 70:40 note: previous borrow of `less` occurs here due
to use in closure; the unique capture
prevents subsequent moves or borrows of
`less` until the borrow ends
code.rs:70 rayon::join(|| parallel_qsort(less),
^~~~~~~~~~~~~~~~~~~~~~~</code></pre>
</section></section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true, // Display controls in the bottom right corner
progress: true, // Display a presentation progress bar
history: true, // Push each slide change to the browser history
center: false, // Vertical centering of slides
maxScale: 1.5, // Bounds for smallest/largest possible content scale
slideNumber: false, // Display the page number of the current slide
theme: 'mozilla-sandstone', // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
// { src: 'reveal.js/plugin/search/search.js', async: true, condition: function() { return !!document.body.classList; }, }
// { src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]});
</script>
</body>
</html>