1 of 272

Rust meetup Mad 2018-04-05 v4

José Luis Esteban Aparicio

The selfish side of

2 of 272

3 of 272

Me

4 of 272

  • OSS Lover
  • C++
  • Financial
  • Distributed systems
  • Realtime (soft)
  • MOM (Tibco, Qpid)

5 of 272

  • Functional programming lover
  • Microservices designer
  • Haskell, Erlang/Elixir, Rust appassionate
  • Eternal learner

6 of 272

Why???

7 of 272

Non specific prog.lang.

A# .NET, A# (Axiom), A-0 System, A+, A++, ABAP, ABC, ABC ALGOL, ABLE, ABSET, ABSYS, ACC, Accent, Ace DASL, ACL2, ACT-III, Action!, ActionScript, Ada, Adenine, Agda, Agilent VEE, Agora, AIMMS, Alef, ALF, ALGOL 58, ALGOL 60, ALGOL 68, ALGOL W, Alice, Alma-0, AmbientTalk, Amiga E, AMOS, AMPL, APL, AppleScript, Arc, ARexx, Argus, AspectJ, Assembly language, ATS, Ateji PX, AutoHotkey, Autocoder, AutoIt, AutoLISP / Visual LISP, Averest, AWK, Axum, B, Babbage, BAIL, Bash, BASIC, bc, BCPL, BeanShell, Batch (Windows/Dos), Bertrand, BETA, Bigwig, Bistro, BitC, BLISS, Blue, Bon, Boo, Boomerang, Bourne shell (including bash and ksh), BREW, BPEL, BuildProfessional, C, C--, C++ - ISO/IEC 14882, C# - ISO/IEC 23270, C/AL, Caché ObjectScript, C Shell, Caml, Candle, Cayenne, CDuce, Cecil, Cel, Cesil, Ceylon, CFML, Cg, Ch, Chapel, CHAIN, Charity, Charm, Chef, CHILL, CHIP-8, chomski, ChucK, CICS, Cilk, CL (IBM), Claire, Clarion, Clean, Clipper, CLIST, Clojure, CLU, CMS-2, COBOL - ISO/IEC 1989, Cobra, CODE, CoffeeScript, Cola, ColdC, ColdFusion, COMAL, Combined Programming Language (CPL), COMIT, Common Intermediate Language (CIL), Common Lisp (also known as CL), COMPASS, Component Pascal, Constraint Handling Rules (CHR), Converge, Cool, Coq, Coral 66, Corn, CorVision, COWSEL, CPL, csh, CSP, Csound, Curl, Curry, Cyclone, Cython, D, DASL (Datapoint's Advanced Systems Language), DASL (Distributed Application Specification Language), Dart, DataFlex, Datalog, DATATRIEVE, dBase, dc, DCL, Deesel (formerly G), Delphi, DCL, DinkC, DIBOL, Dog, Draco, DRAKON, Dylan, DYNAMO, E, E#, Ease, Easy PL/I, EASYTRIEVE PLUS, ECMAScript, Edinburgh IMP, EGL, Eiffel, ELAN, Elixir, Elm, Emacs Lisp, Emerald, Epigram, EPL, Erlang, es, Escapade, Escher, ESPOL, Esterel, Etoys, Euclid, Euler, Euphoria, EusLisp Robot Programming Language, CMS EXEC, EXEC 2, Executable UML, F[edit], F#, Factor, Falcon, Fancy, Fantom, FAUST, Felix, Ferite, FFP, Fjölnir, FL, Flavors, Flex, FLOW-MATIC, FOCAL, FOCUS, FOIL, FORMAC, @Formula, Forth, Fortran - ISO/IEC 1539, Fortress, FoxBase, FoxPro, FP, FPr, Franz Lisp, F-Script, FSProg, G, Game Maker Language, GameMonkey Script, GAMS, GAP, G-code, Genie, GDL, Gibiane, GJ, GEORGE, GLSL, GNU E, GM, Go, Go!, GOAL, Gödel, Godiva, GOM (Good Old Mad), Goo, Gosu, GOTRAN, GPSS, GraphTalk, GRASS, Groovy, Hack (programming language), HAL/S, Hamilton C shell, Harbour, Hartmann pipelines, Haskell, Haxe, High Level Assembly, HLSL, Hop, Hope, Hugo, Hume, HyperTalk, IBM Basic assembly language, IBM HAScript, IBM Informix-4GL, IBM RPG, ICI, Icon, Id, IDL, Idris, IMP, Inform, Io, Ioke, IPL, IPTSCRAE, ISLISP, ISPF, ISWIM, J, J#, J++, JADE, Jako, JAL, Janus, JASS, Java, JavaScript, JCL, JEAN, Join Java, JOSS, Joule, JOVIAL, Joy, JScript, JScript .NET, JavaFX Script, Julia, K, Kaleidoscope, Karel, Karel++, KEE, KIF, Kojo, Kotlin, KRC, KRL, KRL (KUKA Robot Language), KRYPTON, ksh, L, L# .NET, LabVIEW, Ladder, Lagoona, LANSA, Lasso, LaTeX, Lava, LC-3, Leadwerks Script, Leda, Legoscript, LIL, LilyPond, Limbo, Limnor, LINC, Lingo, Linoleum, LIS, LISA, Lisaac, Lisp - ISO/IEC 13816, Lite-C, Lithe, Little b, Logo, Logtalk, LPC, LSE, LSL, LiveCode, LiveScript, Lua, Lucid, Lustre, LYaPAS, Lynx, M, M2001, M4, Machine code, MAD (Michigan Algorithm Decoder), MAD/I, Magik, Magma, make, Maple, MAPPER (Unisys/Sperry) now part of BIS, MARK-IV (Sterling/Informatics) now VISION:BUILDER of CA, Mary, MASM Microsoft Assembly x86, Mathematica, MATLAB, Maxima (see also Macsyma), Max (Max Msp - Graphical Programming Environment), MaxScript internal language 3D Studio Max, Maya (MEL), MDL, Mercury, Mesa, Metacard, Metafont, MetaL, Microcode, MicroScript, MIIS, MillScript, MIMIC, Mirah, Miranda, MIVA Script, ML, Moby, Model 204, Modelica, Modula, Modula-2, Modula-3, Mohol, MOO, Mortran, Mouse, MPD, MSIL - deprecated name for CIL, MSL, MUMPS, NASM, NATURAL, Napier88, Neko, Nemerle, nesC, NESL, Net.Data, NetLogo, NetRexx, NewLISP, NEWP, Newspeak, NewtonScript, NGL, Nial, Nice, Nickle, NPL, Not eXactly C (NXC), Not Quite C (NQC), NSIS, Nu, NWScript, NXT-G, o:XML, Oak, Oberon, Obix, OBJ2, Object Lisp, ObjectLOGO, Object REXX, Object Pascal, Objective-C, Objective-J, Obliq, Obol, OCaml, occam, occam-π, Octave, OmniMark, Onyx, Opa, Opal, OpenEdge ABL, OPL, OPS5, OptimJ, Orc, ORCA/Modula-2, Oriel, Orwell, Oxygene, Oz, P#, ParaSail (programming language), PARI/GP, Pascal - ISO 7185, Pawn, PCASTL, PCF, PEARL, PeopleCode, Perl, PDL, PHP, Phrogram, Pico, Pict, Pike, PIKT, PILOT, Pipelines, Pizza, PL-11, PL/0, PL/B, PL/C, PL/I - ISO 6160, PL/M, PL/P, PL/SQL, PL360, PLANC, Plankalkül, Planner, PLEX, PLEXIL, Plus, POP-11, PostScript, PortablE, Powerhouse, PowerBuilder - 4GL GUI appl. generator from Sybase, PowerShell, PPL, Processing, Processing.js, Prograph, PROIV, Prolog, Promela, PROSE modeling language, PROTEL, ProvideX, Pro*C, Pure, Python, Q (equational programming language), Q (programming language from Kx Systems), Qalb, Qi, QtScript, QuakeC, QPL, R, R++, Racket, RAPID, Rapira, Ratfiv, Ratfor, rc, REBOL, Red, Redcode, REFAL, Reia, Revolution, rex, REXX, Rlab, RobotC, ROOP, RPG, RPL, RSL, RTL/2, Ruby, Rust, S, S2, S3, S-Lang, S-PLUS, SA-C, SabreTalk, SAIL, SALSA, SAM76, SAS, SASL, Sather, Sawzall, SBL, Scala, Scheme, Scilab, Scratch, Script.NET, Sed, Seed7, Self, SenseTalk, SequenceL, SETL, Shift Script, SIMPOL, SIMSCRIPT, Simula, Simulink, SISAL, SLIP, SMALL, Smalltalk, Small Basic, SML, SNOBOL(SPITBOL), Snowball, SOL, Span, SPARK, SPIN, SP/k, SPS, Squeak, Squirrel, SR, S/SL, Stackless Python, Starlogo, Strand, Stata, Stateflow, Subtext, SuperCollider, SuperTalk, Swift (Apple programming language), Swift (parallel scripting language), SYMPL, SyncCharts, SystemVerilog, T, TACL, TACPOL, TADS, TAL, Tcl, Tea, TECO, TELCOMP, TeX, TEX, TIE, Timber, TMG, compiler-compiler, Tom, TOM, Topspeed, TPU, Trac, TTM, T-SQL, TTCN, Turing, TUTOR, TXL, TypeScript, Turbo C++, Ubercode, UCSD Pascal, Umple, Unicon, Uniface, UNITY, Unix shell, UnrealScript, Vala, VBA, VBScript, Verilog, VHDL, Visual Basic, Visual Basic .NET, Microsoft Visual C++, Visual C#, Visual DataFlex, Visual DialogScript, Visual Fortran, Visual FoxPro, Visual J++, Visual J#, Visual Objects, Visual Prolog, VSXu, Vvvv, WATFIV, WATFOR, WebDNA, WebQL, Windows PowerShell, Winbatch, Wolfram, X++, X#, X10, XBL, XC (exploits XMOS architecture), xHarbour, XL, Xojo, XOTcl, XPL, XPL0, XQuery, XSB, XSLT - See XPath, Yorick, YQL, Z notation, Zeno, ZOPL, ZPL

8 of 272

Security

Performance

Reliability

Security

Performance

Reliability

9 of 272

Security

10 of 272

History

11 of 272

12 of 272

13 of 272

Ya disponible Chrome 62, con 35 actualizaciones de seguridad y ...

14 of 272

15 of 272

16 of 272

17 of 272

18 of 272

19 of 272

20 of 272

21 of 272

22 of 272

23 of 272

24 of 272

25 of 272

Performance

26 of 272

27 of 272

28 of 272

Programmers were happy

Once upon a time...

29 of 272

Suddenly...

Herb Sutter

Was Moore an economist?

“Free lunch is over”

30 of 272

31 of 272

Data races

Locks are not the solution

32 of 272

33 of 272

34 of 272

Reliability

35 of 272

36 of 272

37 of 272

38 of 272

Security

Performance

Reliability

Security

Performance

Reliability

39 of 272

Performance

Control

Safe

C

40 of 272

Performance

Control

Safe

C

41 of 272

42 of 272

Performance

Control

Safe

C

43 of 272

44 of 272

  • Safe memory
  • No GC
  • No fake OOP
  • No null pointers
  • ADT
  • Phantom types
  • AOT compiling
  • Very good tooling
  • No exceptions
  • Cross compiling
  • Traits
  • Macros
  • Immutability
  • Borrowing
  • Ownership
  • Static typing
  • Phantom types
  • ...

Many more...

45 of 272

The selfish side of

46 of 272

Mutability

Ownership

Borrowing

Lifetimes

Mutability

Ownership

Borrowing

Lifetimes

47 of 272

48 of 272

YOU HAVE TO SHARE

49 of 272

Are you sure???

50 of 272

Look at...

51 of 272

Yes but…

52 of 272

Yes but…

In RUST is idiomatic not to share

53 of 272

54 of 272

- - - Coupling

+ + + Cohesion

55 of 272

mutability

56 of 272

mutability

57 of 272

Mutability

Turing mach.

NO mutability

Lambda calc.

(λx.x)

58 of 272

NO mutability

59 of 272

mutability

int myVariable = 0;��// some code...��myVariable = ComputeFactor(params...);

60 of 272

NO mutability

// some code...��const int myVariable = ComputeFactor(params...);

61 of 272

NO mutability

// some code...��const int myVariable = ComputeFactor(params...);

--- Coupling

62 of 272

NO mutability

// some code...��const int myVariable = ComputeFactor(params...);

0 Time Coupling

63 of 272

NO mutability

// some code...��const int myVariable = ComputeFactor(params...);

less space Coupling

64 of 272

From Effective C++

Scott Meyers (chapter 3):

Use const whenever possible.

65 of 272

From C++ Core Guidelines

Con.1: By default, make objects immutable

Immutable objects are easier to reason about, ...

66 of 272

From C++ Core Guidelines

Con.4: … Prevent surprises from unexpectedly changed object values.

67 of 272

68 of 272

mutability

69 of 272

Small diff of complex struct

Small diff of complex struct

70 of 272

Copy is expensive

Small diff of complex struct

Small diff of complex struct

71 of 272

72 of 272

73 of 272

PERSISTENT

DATA STRUCTS

74 of 272

PERSISTENT

DATA STRUCTS

More expensive than mutation

75 of 272

Contains mutation under the hood

PERSISTENT

DATA STRUCTS

76 of 272

Control Performance

Safe

C

Mutability

77 of 272

sharing

78 of 272

sharing

79 of 272

sharing

Information

80 of 272

sharing

Resources

then

Information

81 of 272

sharing

Resources

  • Memory
  • File
  • Socket
  • ...

82 of 272

Sharing

resources

Who is responsible???

Creation, deletion???

83 of 272

84 of 272

85 of 272

U B

86 of 272

87 of 272

Expensive

Memory

&

CPU

88 of 272

Not enough

Just memory

89 of 272

Control & perf

Safe

C

Mutability

No GC

90 of 272

Sharing

+

Mutability

???

91 of 272

Sharing

+

mutability

92 of 272

- - - Coupling

+ + + Cohesion

93 of 272

+++ Coupling

--- Cohesion

Sharing + mutability

94 of 272

(Sharing + mutability) + concurrency

95 of 272

Sharing

+

Mutability

& CONCURRENCY

Sharing

+

Mutability

& CONCURRENCY

96 of 272

Most lang. Don’t help

(Sharing + mutability) + concurrency

97 of 272

98 of 272

Examples

99 of 272

func (a)

a = ...

100 of 272

func (a)

a = ...

101 of 272

func (in a)

a = ...

102 of 272

func(const t& a)

a = ...

103 of 272

f a = ...

104 of 272

func (a)

a = ...

Python, Ruby, Java, C#, js, Go...

105 of 272

func (a)

a = ...

106 of 272

b = a

107 of 272

b = a

b= xxx

108 of 272

b = a

b= xxx

a= ???

109 of 272

a = b

b= xxx

a= ???

110 of 272

Immutable by default

111 of 272

Warn on excessive mutability

112 of 272

113 of 272

Suppose copying is very, very, very expensive

114 of 272

It cannot be copied

115 of 272

116 of 272

117 of 272

118 of 272

NO COPY

NO MUT

& NEXT???

NO COPY

NO MUT

& NEXT???

119 of 272

ownership

120 of 272

ownership

121 of 272

ownership

122 of 272

ownership

123 of 272

ownership

124 of 272

ownership

125 of 272

ownership

126 of 272

ownership

127 of 272

ownership

responsible for releasing the resources???

128 of 272

ownership

129 of 272

owner

responsible for releasing the resources???

130 of 272

Ownership (vars)

131 of 272

Ownership (vars)

132 of 272

Ownership (vars)

133 of 272

Ownership (func)

134 of 272

Ownership (func)

135 of 272

Ownership (func)

136 of 272

Ownership (func)

Ownership

moved

137 of 272

Ownership (func)

Ownership

moved

138 of 272

139 of 272

Not initialized

140 of 272

Not initialized

141 of 272

142 of 272

143 of 272

144 of 272

145 of 272

146 of 272

147 of 272

RVO & ownership

148 of 272

Ownership

moved

RVO & ownership

149 of 272

150 of 272

Ownership (vars)

151 of 272

Ownership (func)

152 of 272

Borrowing

I own the ring

153 of 272

Borrowing

154 of 272

Borrowing

I can borrow it,

I continues owning the ring

155 of 272

Borrowing

156 of 272

Borrowing

157 of 272

Borrowing

158 of 272

Borrowing

159 of 272

Borrowing

160 of 272

Borrowing

161 of 272

Borrowing

I own the ring

I have to destroy it

162 of 272

Borrowing

163 of 272

Borrowing 2

164 of 272

Borrowing

165 of 272

Borrowing

166 of 272

I own the ring

I have to destroy it

Borrowing

167 of 272

Borrowing

What happen with my reference to the ring???

LIFETIME

Another day

168 of 272

169 of 272

170 of 272

Ownership

171 of 272

Ownership

moved

172 of 272

borrowing (params)

173 of 272

174 of 272

175 of 272

176 of 272

c0 gave the resource to c1

177 of 272

Resource moved from c0 to c1

178 of 272

179 of 272

c0

borrowing (vars)

180 of 272

c0

print_counter

borrowing (vars)

181 of 272

c0

print_counter

borrowing (vars)

182 of 272

c0

borrowing (vars)

183 of 272

c0

borrowing (vars)

184 of 272

c0

c1

borrowing (vars)

185 of 272

c0

c1

print_counter

borrowing (vars)

186 of 272

c0

c1

print_counter

borrowing (vars)

187 of 272

c0

c1

borrowing (vars)

188 of 272

c0

borrowing (vars)

189 of 272

c0

borrowing (vars)

190 of 272

Sharing

+

mutability

191 of 272

Sharing + mutability

Ownership &

Borrowing

Sharing Control

192 of 272

Sharing + mutability

c0 is NOT mutable

193 of 272

Sharing + mutability

borrowing c0 to mutable

194 of 272

Sharing + mutability

Can’t not borrow immutable to a mutable

195 of 272

Sharing + mutability

Moving ownership from NOT-mutable to mutable

196 of 272

Sharing + mutability

Moving ownership from NOT-mutable to mutable

197 of 272

Moving ownership from NOT-mutable to mutable

198 of 272

Sharing + mutability

Moving ownership from NOT-mutable to mutable

It helps to reduce mutability and improve the performance

199 of 272

Sharing + mutability

Moving ownership from NOT-mutable to mutable

mutability is not viric

200 of 272

Suppose copying is very, very, very expensive

201 of 272

It cannot be copied

202 of 272

203 of 272

204 of 272

205 of 272

mutability is not viric

206 of 272

mutability is not viric

Functional style

207 of 272

mutability is not viric

Explicit no copy

208 of 272

mutability is not viric

No automagic copy elision

209 of 272

mutability is not viric

Zero cost abstraction

210 of 272

mutability is not viric

WYSIWCD

What You Say is What Compiler Does

211 of 272

Don’t let IMmutability to ruin a good performance

212 of 272

Don’t let IMmutability to ruin a good performance

Looks functional

No mutability

No copies

213 of 272

Don’t let IMmutability to ruin a good performance

214 of 272

Given two sets, a and b.

You have to verify that the set containing the

square of the elements of a, has the same elements of b.

215 of 272

OK

a = [1,2,3,4]�b = [1,4,9,16]

OK

a = [2,3,1,4]�b = [16,4,9,1]

WRONG

a = [1,2,3,4]�b = [1,4,9]

216 of 272

O (n log n)

217 of 272

O (n log n)

Copy elision

under the hood???

218 of 272

O (n log n)

219 of 272

O (n log n)

Too much mutability with small control

220 of 272

O (n log n)

Great performance!!!

No mut on params

Param copies

221 of 272

O (n log n)

222 of 272

O (n log n)

Copy elision

under the hood???

223 of 272

O (n log n)

224 of 272

O (n log n)

225 of 272

O (n log n)

226 of 272

O (n log n)

Danger!!!

a.sort will work, but mutating the param!!!

227 of 272

O (n log n)

All solutions copy params

228 of 272

O (n log n)

All solutions copy params

The alternative is very dangerous

229 of 272

O (n log n)

230 of 272

O (n log n)

NO COPIES!!!

231 of 272

O (n log n)

NO COPIES!!!

LOCAL�EXPLICIT MUTABILITY

232 of 272

O (n log n)

NO COPIES!!!

LOCAL�EXPLICIT MUTABILITY

233 of 272

O (n)

234 of 272

O (n)

Copy elision

under the hood!!!

Persist DS

235 of 272

O (n)

NO COPIES!!!

LOCAL �EXPLICIT MUTABILITY

236 of 272

Don’t let IMmutability to ruin a good performance

When no need to share…

Say it explicitly

237 of 272

238 of 272

LET THEY COME

Sharing, and mut

239 of 272

Mutability

Ownership

Borrowing

Mutability

Ownership

Borrowing

Rc

Arc

COW

240 of 272

Review

241 of 272

- - - Coupling

+ + + Cohesion

242 of 272

Security

Performance

Reliability

Security

Performance

Reliability

243 of 272

Security

244 of 272

Software security!!!

245 of 272

Performance

246 of 272

247 of 272

Reliability

248 of 272

Software reliability

249 of 272

Then…

Do not always is good to share???

250 of 272

Sharing information it’s OK

251 of 272

Sharing resources is complicated

252 of 272

Sharing MUTABLE resources is a huge problem

253 of 272

You can control sharing and mutability with Rust

254 of 272

Sharing not mutable resources

==

Sharing info

255 of 272

Persistent DS

More expensive than mutation

256 of 272

Most lang. Don’t help

(Sharing + mutability) + concurrency

257 of 272

Sharing

+

mutability

258 of 272

259 of 272

Sharing + mutability

Ownership &

Borrowing

Sharing & mut Control

260 of 272

ABSTRACTION

NO RUNTIME

261 of 272

262 of 272

Sharing + mutability

Moving ownership from NOT-mutable to mutable

mutability is not viric

263 of 272

COMPILER

264 of 272

IF IT COMPILES

265 of 272

IF IT COMPILES

IT IS SAFE

IF IT COMPILES

IT IS SAFE

No matter if you work with threads, reduces copies, aggressive on optimizations

266 of 272

COMPILE → SAFE

267 of 272

COMPILE → SAFE

NOT SAFE→ NOT COMPILE

268 of 272

COMPILE → SAFE

NOT SAFE→ NOT COMPILE

NOT COMPILE → NOT SAFE

269 of 272

COMPILE → SAFE

NOT SAFE→ NOT COMPILE

NOT COMPILE → NOT SAFE

YOU CAN WRITE SAFE CODE THAT DOESN’T COMPILE

270 of 272

  • Safe memory
  • No GC
  • No fake OOP
  • No null pointers
  • ADT
  • Phantom types
  • AOT compiling
  • Very good tooling
  • No exceptions
  • Cross compiling
  • Traits
  • Macros
  • Immutability
  • Borrowing
  • Ownership
  • Static typing
  • Phantom types
  • ...

Many more...

271 of 272

272 of 272