Rust meetup Mad 2018-04-05 v4
José Luis Esteban Aparicio
The selfish side of
Me
Why???
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
Security
Performance
Reliability
Security
Performance
Reliability
Security
History
Ya disponible Chrome 62, con 35 actualizaciones de seguridad y ...
Performance
Programmers were happy
Once upon a time...
Suddenly...
Herb Sutter
Was Moore an economist?
“Free lunch is over”
Data races
Locks are not the solution
Reliability
Security
Performance
Reliability
Security
Performance
Reliability
Performance
Control
Safe
C
Performance
Control
Safe
C
Performance
Control
Safe
C
Many more...
The selfish side of
Mutability
Ownership
Borrowing
Lifetimes
Mutability
Ownership
Borrowing
Lifetimes
YOU HAVE TO SHARE
Are you sure???
Look at...
Yes but…
Yes but…
In RUST is idiomatic not to share
- - - Coupling
+ + + Cohesion
mutability
mutability
Mutability
Turing mach.
NO mutability
Lambda calc.
(λx.x)
NO mutability
mutability
int myVariable = 0;��// some code...��myVariable = ComputeFactor(params...);
NO mutability
// some code...��const int myVariable = ComputeFactor(params...);
NO mutability
// some code...��const int myVariable = ComputeFactor(params...);
--- Coupling
NO mutability
// some code...��const int myVariable = ComputeFactor(params...);
0 Time Coupling
NO mutability
// some code...��const int myVariable = ComputeFactor(params...);
less space Coupling
From Effective C++
Scott Meyers (chapter 3):
Use const whenever possible.
From C++ Core Guidelines
Con.1: By default, make objects immutable
Immutable objects are easier to reason about, ...
From C++ Core Guidelines
Con.4: … Prevent surprises from unexpectedly changed object values.
mutability
Small diff of complex struct
Small diff of complex struct
Copy is expensive
Small diff of complex struct
Small diff of complex struct
PERSISTENT
DATA STRUCTS
PERSISTENT
DATA STRUCTS
More expensive than mutation
Contains mutation under the hood
PERSISTENT
DATA STRUCTS
Control Performance
Safe
C
Mutability
sharing
sharing
sharing
Information
sharing
Resources
then
Information
sharing
Resources
Sharing
resources
Who is responsible???
Creation, deletion???
U B
Expensive
Memory
&
CPU
Not enough
Just memory
Control & perf
Safe
C
Mutability
No GC
Sharing
+
Mutability
???
Sharing
+
mutability
- - - Coupling
+ + + Cohesion
+++ Coupling
--- Cohesion
Sharing + mutability
(Sharing + mutability) + concurrency
Sharing
+
Mutability
& CONCURRENCY
Sharing
+
Mutability
& CONCURRENCY
Most lang. Don’t help
(Sharing + mutability) + concurrency
Examples
func (a)
a = ...
func (a)
a = ...
func (in a)
a = ...
func(const t& a)
a = ...
f a = ...
func (a)
a = ...
Python, Ruby, Java, C#, js, Go...
func (a)
a = ...
b = a
b = a
b= xxx
b = a
b= xxx
a= ???
a = b
b= xxx
a= ???
Immutable by default
Warn on excessive mutability
Suppose copying is very, very, very expensive
It cannot be copied
NO COPY
NO MUT
& NEXT???
NO COPY
NO MUT
& NEXT???
ownership
ownership
ownership
ownership
ownership
ownership
ownership
ownership
ownership
responsible for releasing the resources???
ownership
owner
responsible for releasing the resources???
Ownership (vars)
Ownership (vars)
Ownership (vars)
Ownership (func)
Ownership (func)
Ownership (func)
Ownership (func)
Ownership
moved
Ownership (func)
Ownership
moved
Not initialized
Not initialized
RVO & ownership
Ownership
moved
RVO & ownership
Ownership (vars)
Ownership (func)
Borrowing
I own the ring
Borrowing
Borrowing
I can borrow it,
I continues owning the ring
Borrowing
Borrowing
Borrowing
Borrowing
Borrowing
Borrowing
Borrowing
I own the ring
I have to destroy it
Borrowing
Borrowing 2
Borrowing
Borrowing
I own the ring
I have to destroy it
Borrowing
Borrowing
What happen with my reference to the ring???
LIFETIME
Another day
Ownership
Ownership
moved
borrowing (params)
c0 gave the resource to c1
Resource moved from c0 to c1
c0
borrowing (vars)
c0
print_counter
borrowing (vars)
c0
print_counter
borrowing (vars)
c0
borrowing (vars)
c0
borrowing (vars)
c0
c1
borrowing (vars)
c0
c1
print_counter
borrowing (vars)
c0
c1
print_counter
borrowing (vars)
c0
c1
borrowing (vars)
c0
borrowing (vars)
c0
borrowing (vars)
Sharing
+
mutability
Sharing + mutability
Ownership &
Borrowing
Sharing Control
Sharing + mutability
c0 is NOT mutable
Sharing + mutability
borrowing c0 to mutable
Sharing + mutability
Can’t not borrow immutable to a mutable
Sharing + mutability
Moving ownership from NOT-mutable to mutable
Sharing + mutability
Moving ownership from NOT-mutable to mutable
Moving ownership from NOT-mutable to mutable
Sharing + mutability
Moving ownership from NOT-mutable to mutable
It helps to reduce mutability and improve the performance
Sharing + mutability
Moving ownership from NOT-mutable to mutable
mutability is not viric
Suppose copying is very, very, very expensive
It cannot be copied
mutability is not viric
mutability is not viric
Functional style
mutability is not viric
Explicit no copy
mutability is not viric
No automagic copy elision
mutability is not viric
Zero cost abstraction
mutability is not viric
WYSIWCD
What You Say is What Compiler Does
Don’t let IMmutability to ruin a good performance
Don’t let IMmutability to ruin a good performance
Looks functional
No mutability
No copies
Don’t let IMmutability to ruin a good performance
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.
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]
O (n log n)
O (n log n)
Copy elision
under the hood???
O (n log n)
O (n log n)
Too much mutability with small control
O (n log n)
Great performance!!!
No mut on params
Param copies
O (n log n)
O (n log n)
Copy elision
under the hood???
O (n log n)
O (n log n)
O (n log n)
O (n log n)
Danger!!!
a.sort will work, but mutating the param!!!
O (n log n)
All solutions copy params
O (n log n)
All solutions copy params
The alternative is very dangerous
O (n log n)
O (n log n)
NO COPIES!!!
O (n log n)
NO COPIES!!!
LOCAL�EXPLICIT MUTABILITY
O (n log n)
NO COPIES!!!
LOCAL�EXPLICIT MUTABILITY
O (n)
O (n)
Copy elision
under the hood!!!
Persist DS
O (n)
NO COPIES!!!
LOCAL �EXPLICIT MUTABILITY
Don’t let IMmutability to ruin a good performance
When no need to share…
Say it explicitly
LET THEY COME
Sharing, and mut
Mutability
Ownership
Borrowing
Mutability
Ownership
Borrowing
Rc
Arc
COW
Review
- - - Coupling
+ + + Cohesion
Security
Performance
Reliability
Security
Performance
Reliability
Security
Software security!!!
Performance
Reliability
Software reliability
Then…
Do not always is good to share???
Sharing information it’s OK
Sharing resources is complicated
Sharing MUTABLE resources is a huge problem
You can control sharing and mutability with Rust
Sharing not mutable resources
==
Sharing info
Persistent DS
More expensive than mutation
Most lang. Don’t help
(Sharing + mutability) + concurrency
Sharing
+
mutability
Sharing + mutability
Ownership &
Borrowing
Sharing & mut Control
ABSTRACTION
NO RUNTIME
Sharing + mutability
Moving ownership from NOT-mutable to mutable
mutability is not viric
COMPILER
IF IT COMPILES
IF IT COMPILES
IT IS SAFE
IF IT COMPILES
IT IS SAFE
No matter if you work with threads, reduces copies, aggressive on optimizations
COMPILE → SAFE
COMPILE → SAFE
NOT SAFE→ NOT COMPILE
COMPILE → SAFE
NOT SAFE→ NOT COMPILE
NOT COMPILE → NOT SAFE
COMPILE → SAFE
NOT SAFE→ NOT COMPILE
NOT COMPILE → NOT SAFE
YOU CAN WRITE SAFE CODE THAT DOESN’T COMPILE
Many more...