Slide 1

Slide 1 text

JSConf AU 2018 Pumping up Node.js modules with Rust Vigneshwer Dhinakaran | Mozilla TechSpeaker | @dvigneshwer

Slide 2

Slide 2 text

Need of Node.js Native Addon Performance Freedom in memory management low-level API

Slide 3

Slide 3 text

Why Rust? Freedom from Data Race conditions Strong type system High level iterators More control over memory Welcoming Community

Slide 4

Slide 4 text

Facts & Figures

Slide 5

Slide 5 text

Speeding up crypto on Wire desktop apps https://medium.com/@wireapp/speeding-up-crypto-on-wire-desktop-apps-3ff37fc98c3f Up to 141× faster

Slide 6

Slide 6 text

Tokio MiniHTTP The library is a proof-of-concept implementation of a simple HTTP/1.1 server using Tokio. http://aturon.github.io/blog/2016/08/11/futures/

Slide 7

Slide 7 text

Gradual adoption works.

Slide 8

Slide 8 text

2016 -18 For the third year in a row, Rust is stackoverflow's most loved language https://insights.stackoverflow.com/survey/2018/

Slide 9

Slide 9 text

Friends of Rust Organizations running Rust in production. (https://www.rust-lang.org/en-US/friends.html)

Slide 10

Slide 10 text

Rust is easy to learn!

Slide 11

Slide 11 text

Variables

Slide 12

Slide 12 text

Functions

Slide 13

Slide 13 text

Flow control

Slide 14

Slide 14 text

Flow control

Slide 15

Slide 15 text

Looping

Slide 16

Slide 16 text

Types

Slide 17

Slide 17 text

Package Manager

Slide 18

Slide 18 text

Ownership The act, state, or right of possessing something

Slide 19

Slide 19 text

Garbage Collection Heap Memory new Circle(5) new Circle(5) new Circle(5)

Slide 20

Slide 20 text

Referencing Now both bookingService and repairService hold a reference to vehicle

Slide 21

Slide 21 text

Same implementation in Rust Doesn't compile!

Slide 22

Slide 22 text

Ownership ● Rust moves ownership by default ● The owner has the right to destroy the thing it owns ● The memory is freed as soon as the owned variable goes out of it's scope ● Hence vehicle may already be destroyed at the point when it's passed to repair_service ● Rust catches these errors at compile time

Slide 23

Slide 23 text

Borrowing The act, state, or right of possessing something

Slide 24

Slide 24 text

Making it work in Rust Lending things is the key!

Slide 25

Slide 25 text

Borrowing ● A reference is passed without transferring ownership ● One can borrow immutable (&) or mutable (&mut) but not both at the same time ● Shared ownership can be achieved through special pointers with runtime checks

Slide 26

Slide 26 text

Mutability Rules ● All variables are immutable by default ● Only one mutable reference at a time ( But as many immutable &’s as you want ) ● Mutable references block all other access (The &mut must go out of scope before using other &’s )

Slide 27

Slide 27 text

Using Rust with Node.js

Slide 28

Slide 28 text

Application to generate Fibonacci numbers /Cargo.toml ~ package.json https://github.com/RisingStack/node-with-rust /src/embed.rs

Slide 29

Slide 29 text

Node.js Integration You are calling Rust from Node.js now!

Slide 30

Slide 30 text

Deep dive into Neon project

Slide 31

Slide 31 text

Getting Started

Slide 32

Slide 32 text

Hello world native/Cargo.toml /native/src/lib.rs https://guides.neon-bindings.com/hello-world/

Slide 33

Slide 33 text

Hello world lib/index.js Build & Run https://guides.neon-bindings.com/hello-world/

Slide 34

Slide 34 text

call.scope eventHandler initObject init_obj init_prop set_prop Object::set setter JavaScript Rust V8 JavaScript GC’ed Heap Memory

Slide 35

Slide 35 text

Benchmarking Execution time : ~ 1 sec

Slide 36

Slide 36 text

Benchmarking Execution time : ~ 250 ms

Slide 37

Slide 37 text

Benchmarking

Slide 38

Slide 38 text

Benchmarking ~ 100 ms

Slide 39

Slide 39 text

Summary

Slide 40

Slide 40 text

When should you use Rust? ● Rewrite modules of your code that are computationally heavy (Complex math optimizations etc) ● For accessing the hardware(ex :- IOT applications, device drivers, GPU’s etc) ● Implementing advance concurrency paradigms ● JS to Rust calls are non-trivial, so be wise!

Slide 41

Slide 41 text

What does Rust have to offer? ● Modern C/C++ replacement ● Memory Safety without Garbage Collection ● No runtime needed ● Rich Typesystem ● Strong Functional Programming influence

Slide 42

Slide 42 text

https://hacks.mozilla.org/2018/03/making-webassembly-better-for-rust-for-all-languages/ The future is exciting!

Slide 43

Slide 43 text

Thanks! @dvigneshwer [email protected] https://tinyurl.com/JSConfAU18