+ All Categories
Home > Documents > Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust &...

Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust &...

Date post: 24-May-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
40
Rust & Servo - An Introduction Zhen Zhang [email protected] May 18, 2016 1
Transcript
Page 1: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Rust & Servo - An Introduction

Zhen Zhang [email protected] 18, 2016

1

Page 2: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Rust - The Modern SystemProgramming Language

2

Page 3: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A Taste of Rust

• The mini interpreter• Playground

3

Page 4: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A Taste of Rust (Cont.)

Figure 1: https://www.rust-lang.org 4

Page 5: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Why Rust is Awesome?

Featuring:

• zero-cost abstractions• move semantics• guaranteed memory safety• threads without data races• trait-based generics• pattern matching• type inference• minimal runtime• efficient C bindings

5

Page 6: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #1: Zero-cost abstraction

What you don’t use, you don’t pay for.And further: What you do use, youcouldn’t hand code any better.[Stroustrup, 1994]

6

Page 7: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #2: Trait-based generics

• The ONLY notion of interface• Statically dispatched: C++ templates• Dynamically dispatched: Java Interface• Trait bound and inheritance

7

Page 8: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #2: Trait-based generics

Play

Figure 2: Example snippet - trait 8

Page 9: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #3: Memory safety

• A BIG topic in fact• Static methods: Cyclone, MLKit … Rust• Dynamic methods: Garbage collection! Java,

Python, C# ….• Rust has many concepts…

9

Page 10: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.1: Ownership and Borrowing

Classical problem: Memory leak

• If you own something, you may borrow it out,but you must make sure that you can get itback

• If you don’t want something, you may transferthe ownership, but don’t try to interfere withit after giving out

• If you own something, you must live longerthan it

10

Page 11: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.1: Ownership and Borrowing (Cont.)

Play

Figure 3: Example snippet – Ownership

11

Page 12: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.2: References and Mutability

Classical problem: Concurrent data races

• Similar to C++• Hardened with ownership control, lifetime and

mutability• Principles:

• one or more references (&T) to a resource,• exactly one mutable reference (&mut T).

12

Page 13: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.2: References and Mutability (Cont.)

Play

Figure 4: Example snippet – References

13

Page 14: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.3: Lifetimes

Classical problems: Use after free

• I acquire a handle to some kind of resource.• I lend you a reference to the resource.• I decide I’m done with the resource, and

deallocate it, while you still have your reference.• You decide to use the resource.

14

Page 15: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

A#3.3: Lifetimes (Cont.)

Play

Figure 5: Example snippet – Lifetimes

15

Page 16: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #4: ADT and PatternMatching

• Algebraic data-types (functional flavor!)• Destruct/match: More safe/expressive/elegant

then switch or dynamic overload

16

Page 17: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #4: ADT and PatternMatching (Cont.)

Play

Figure 6: Example snippet – Patternmatching 17

Page 18: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #5: Type System

• Local type inference• Generics (Parametric polymorphism)• Trait-based polymorphism• Associated types (type families)• NO support for higher-kinded types yet

18

Page 19: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #5: Type System (Cont.)

Play

Figure 7: Example snippet – Typeinference and Generics

19

Page 20: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Awesomeness #5: Type System (Cont.)

Figure 8: Example snippet –Trait based polymorphism

Figure 9: Example snippet –Associated types

20

Page 21: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Rust’s advantages over C++

1. cargo ecosystem2. Safety3. Simplicity of design4. Modern language constructs

21

Page 22: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

But Rust is not perfect

1. Value-returning flavor error handling2. Lack of OOP3. Too many macros4. Steep learning curve (even for C/C++ hackers)5. Still in fast evolving6. Too many types of pointers, closures, strings ….

22

Page 23: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Rust in the wild

• Servo Browser Engine, Mozilla Foundation• Rust Infrastructure, Mozilla Foundation• Ruby app profiler, Skylight Inc.• Infra, DropBox Inc.• Xi Editor, Google Employee• Redox OS

23

Page 24: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Misc

• Resource:• The Book• Rust For Systems Programmers• USTC mirror of latest binaries• Jump into the wheel-reinventing battle against

C/C++/Go/D/… on GitHub!

• Discussions(in Chinese):• 如何看待 Dropbox 从 Go 转向 Rust ?• Rust 所宣称的 zero-cost abstractions 是怎么回事?

24

Page 25: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Servo - The Modern ParallelBrowser Engine

25

Page 26: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Motivation

Why Mozilla want to invent a new language to writesomething to replace Gecko (in C++)?

• Takes advantage of parallelism at many levels• Eliminating common sources of bugs and

security vulnerabilities associated with incorrectmemory management and data races

How does the effort pay off?

26

Page 27: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Performance and Benchmark

• Mozilla’s Servo Lets Rust Shine• Mozilla’s Servo Engine Is Crazy Fast Compared

To Gecko• Benchmarking

27

Page 28: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Performance and Benchmark (Cont.)

Figure 10: Servo v.s. Gecko 28

Page 29: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Browser Internals 101

• Layout engine• JS runtime• Network/Resource management• UI

29

Page 30: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Browser Internals 101 (Cont.)

Figure 11: Browser Reference Architecture30

Page 31: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Browser Internals 101 (Cont.)

Figure 12: FireFox Architecture 31

Page 32: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Servo’s Architecture

Figure 13: Task supervision diagram 32

Page 33: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Servo’s Architecture (Cont.)

Each constellation manages a pipeline of tasks:

• Script• Layout• Renderer• Compositor

https://github.com/servo/servo/wiki/Design

33

Page 34: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Servo’s Strategies for parallelism and con-currency

• Task-based architecture• Concurrent rendering• Tiled rendering• Layered rendering• Image decoding• GC JS concurrent with layout

34

Page 35: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Personal Experience of Hacking on Servo

• Very mature infra, easy to use tools• You can learn a lot• Nice and supportive people in Mozilla• Very friendly to new contributors

35

Page 36: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Roadmap

• Ship one Rust component in Firefox Nightly,riding the trains

• Experiment with the uplift of a major piece ofServo into Gecko

• June tech demo

36

Page 37: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Servo needs you

• Step 1: Learn Rust• Step 2: Try to solve an essay issue• Step 3: First PR landed!

37

Page 38: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Backup Slides

38

Page 39: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Most Loved PL in 2016 is Rust

https://stackoverflow.com/research/developer-survey-2016

39

Page 40: Rust & Servo - An Introductionustc-lambda.github.io/files/rust-pre.pdf · 2019-02-16 · Rust & Servo - An Introduction Zhen Zhang izgzhen@gmail.com May 18, 2016 1. Rust - The Modern

Rust just turns one year old

Rust 1.0 was released at May 15, 2015. Here is apost on the past year.

40


Recommended