D2前端技术论坛

BE FEARLESS Why Rust-lang is your ticket to stable, high-performance web applications

1. > BE FEARLESS Why Rust-lang is your ticket to stable, high-performance web applications _
2. Started career as a Frontend Engineer @ Alimama (advertise group in Alibaba Inc.) WHO AM I Started a pure-Rust project My first in production Rust project A full-time Rust engineer @ City Brain team, Aliyun
3. Started career as a Frontend Engineer WHY Rust Thought Frontend is easier Than java, CPP…
4. Started career as a Frontend Engineer WHY Rust Thought Frontend is easier Than java, CPP… Frontend is much much harder
5. Why Is Frontend Hard My favorite book when learning JavaScript
6. Why Is Frontend Hard • Too many caveats • Hiding many details behind the scene • Growing too fast, new tech every year
7. Started career as a Frontend Engineer @ Alimama (advertise group in Alibaba Inc.) Started a pure-Rust project • @ 2016 • We Needed a fast, stable image-generator service for the Taobao App frontpage • Called Minolta
8. Node.js Our Need • Too many caveats • Is this fast enough? Are we profiling the right way?? Is multi-process/ multi-threading really helping? • Hiding many details behind the scene • ASync file IO ? Really? Does Promises have overhead? Will this 3rd-lib crash? • Growing too fast, new tech every year • Should we move to async/await? • Be cool or be fast?
9. • Can we easily know how the code we wrote is executed? • Can we know whether the code / deps will crash / error or not? • Can we know whether the runtime is optimizing our code or not?
10. Rust Our Need • Strong-typed, compiled language One of the fastest Clear thread model • Is this fast enough? Are we profiling the right way?? Is multi-process/ multi-threading really helping? • Raw, clear async model Zero-overhead async (futures) Static Error handling / crash detection • Async file IO ? Really? Does Promises have overhead? Will this 3rd-lib crash? • Growing fast, remain stable Both cool and fast! • Should we move to async/await? Be cool or be fast?
11. Minolta (Rust) Co-work with @ 4800 RPS 82% CPU 5.86% MEM Battle tested Went through two 11.11 No critical crash down First Rust project In production As we know, in Alibaba
12. Minolta (Rust) Co-work with @ • It’s a CPU-bounded web application. • It interacts with some C-only libraries (freetype, cairo, librsvg…) • It needs to download resources on every request, and properly handle many different error kinds • We got 20x speedup compared to the Node.js version
13. Confetti (Rust) 6000 RPS 24MS RT GPU rendered on Server Next-gen graph rendering Inspired by Pathfinder (Mozilla)
14. Smash (Rust, WIP) Distributed Graph Computation Based on Naiad Paper High performance city simulation Less than 8ms per iteration Blazing fast messaging Strong network I/O guarantee WASM speeded up client
15. You Can
16. If U need Frontend Backend IO heavy CPU heavy High level System level Exp first Lang insurance
17. RUST is adopted
18. OK… What is RUST?
19. RUST Rust empowers you to reach farther, to program with confidence in a wider variety of domains than you did before.
20. RUST PERFORMANCE RELIABILITY • Fast & memory efficient • Borrow checker (@ Compile time!) • No runtime, no gc • Lifetime • C-FFI support, easy for embedding • Strong guarded thread safety
21. WHY YOU COULD BE FEARLESS WITH RUST?
22. RUST OWNERSHIP
23. RUST OWNERSHIP OWNERSHIP ENABLES IT REMOVES • RAII Style deconstructors • A source of memory leaks • No dangling pointers • Many NullPointerException • No data races • Fearless multithreading
24. RUST MULTI-THREADING
25. RUST BORROW CHECKER • Owned: T Compile guarantee: • Immutable borrow: &T • Mutable borrow: &mut T Many Immutable borrow OR One mutable borrow
26. RUST GUARANTEE Send Sync • Variable could be send from thread A to B • Variable could be shared between threads without a memcpy • Auto added to most types • Not available for thread-local, e.g. Rc • Mutex, Atomic • Concurrent data structures!
27. RUST Choose your guarantee • Data on heap: Box<T> • Thread local ref count: RC<T> Thread local mutable ref count: RefCell<T> • Cross thread ref count: Arc<T> Cross thread mutable: Mutex<T> / RwLock<T>
28. These are problems In C++, not JS!
29. Really?
30. Popular in FE IT REMOVES • Typescript • Countless type check and unsafe • React / Redux • Too many logic modifying data • Immutable • Data mutable hard to track • …
31. Rust makes me a Better programmer
32. Rust and WASM
33. Credit: Raphael Amorim WASM 1st class supported languages: C C++ Rust
34. JavaScript Rust
35. LIBS • • • • RUST + WASM ECOSYSTEM Js-sys Javascript bindings (zero overhead) Web-sys Web API bindings (zero overhead) Wasm-pack web assembly packaging CLI Wasm-bindgen rust to & from wasm bindgen
36. Rust feature IT ENABLES • Ownership / Lifetime • Early drop unused variables • Immutable first • Unexpected mutation • Weak Ref / Global Statics • Better memory management • Fearless concurrency • Thread support (Upcoming) • … • …
37. When Threads are adopted in WASM, Rust would almost be the only choice to write confidence, zero-cost multithreaded logic in WASM
38. Ouch… This article is quite popular
39. This article declares that after serious optimization, pure JavaScript code could outrun Rust + WASM code
40. Can you optimize This hard every time?
41. @ Jandem I work on SpiderMonkey JS performance (and got mentioned in the article for fixing a perf bug!) and this is exactly the point I wanted to make. If you're not a VM engineer, it's much harder to write fast JS. Wasm also gives you much more predictable performance across engines, without requiring (often engine-specific) 'hacks' like function cloning.
42. Thank you

相关幻灯片