Share
Embed
Transcript
25 slides
-
About me • Not a Ruby expert • Ruby-curious? •
A Kotlin nerd • More than 20 years of experience • OSS Contributor 2 -
What is Crystal? … are you doing m*th? Yes, we’re
doing m a th. “Cryst a l is a gener a l-purpose, object- oriented l a ngu a ge. With synt a x inspired by Ruby, it’s a compiled l a ngu a ge with st a tic type-checking. Types a re resolved by a n a dv a nced type inference a lgorithm” https://cryst a l-l a ng.org/#intro 3 -
Strenghts From a Ruby perspective • Compiled • F a
st execution • Bin a ry distribution • Low memory consumption • More errors c a ptured before execution • E a sy tr a nsition from Ruby • Synt a x • Fl a vour/Feeling • Very good a t de a ling with bin a ry d a t a a nd n a tive libr a ries 4 -
Weaknes From a Ruby perspective • The feedb a ck
loop is slower • Community is sm a ller • Less libr a ries • M a inten a nce is slower • Tooling isn’t gre a t • Some work is being done 5 -
Similarities Crystal can run some Ruby code f ib.rb 6
$ ruby fib.rb 9227465 $ crystal fib.rb 9227465 -
Ruby Cryst a l 7 https://github.com/M a rioAri a sC/pep
a /blob/m a in/lib/ev a lu a tor.rb https://github.com/M a rioAri a sC/monyet/blob/m a in/src/ev a lu a tor.cr -
Differences About 75%^ to 90%^^ is the same… • Compiled
• St a tic • No runtime Met a progr a mming, inste a d compile-time Met a progr a mming • The type system is more powerful • Enums • Struct • Generics • Union Types • Nill a ble Types • Other types • Some di ff erences in liter a ls a nd control f low 8 ^ According to me ^^ According to R a ilC a r’s a uthor, S a m Ruby -
Enums “An enum is a set of integer values, where
each value has an associated name” https://crystal-lang.org/reference/1.19/syntax_and_semantics/enum.html 9 -
Struct “Structs inherit from Value so they are allocated on
the stack and passed by value” https://crystal-lang.org/reference/1.19/syntax_and_semantics/structs.html 10 -
Generics “Generics allow you to parameterize a type based on
another type. Generics provide type-polymorphism” https://crystal-lang.org/reference/1.19/syntax_and_semantics/generics.html 11 -
Union and nilable types “The type of variable or expression
can consist of multiple types” https://crystal-lang.org/reference/1.19/syntax_and_semantics/union_types.html 12 -
Performance …Gotta go fast!! hyperfine - w 3 'ruby fib.rb'
Benchmark 1: ruby fib.rb Tim e ( m ean ± σ): 359.1 m s ± 8.1 m s [User: 351.7 m s, Syste m : 5.6 m s] Range ( mi n … m ax): 350.1 m s … 376.9 m s 10 runs Ruby 4.0.2 - 18.5 MB 13 -
… with YJIT …Gotta go fast!! Ruby —yjit 4.0.2 -
18.93 MB hyperfine - w 3 'ruby --yjit fib.rb' Benchmark 1: ruby --yjit fib.rb T im e ( m ean ± σ): 92.9 m s ± 0.7 m s [User: 87.2 m s, Syste m : 4.7 m s] Range ( mi n … m ax): 92.0 m s … 95.1 m s 31 runs 0ms 200ms 400ms Ruby 4.0.2 Ruby --yjit 4.0.2 92.9 359.1 14 -
… with Crystal …Gotta go… WATH? Cryst a l 1.19
- 276.53 MB hyperfine - w 3 'crystal fib.rb' Benchmark 1: crystal fib.rb T im e ( m ean ± σ): 566.3 m s ± 7.6 m s [User: 483.8 m s, Syste m : 222.1 m s] Range ( mi n … m ax): 550.0 m s … 574.6 m s 10 runs 0ms 300ms 600ms Ruby 4.0.2 Ruby --yjit 4.0.2 Cryst a l 566.3 92.9 359.1 15 -
… with Crystal… compiled …Gotta go fast!!!!!! Cryst a l
1.19 (compiled) - 2.48 MB crystal build --release -o fib-cr-release fib.rb hyperfine - w 3 './fib-cr-release' Benchmark 1: ./fib-cr-release T im e ( m ean ± σ): 21.5 m s ± 1.1 m s [User: 20.6 m s, Syste m : 0.5 m s] Range ( mi n … m ax): 19.0 m s … 23.9 m s 138 runs 0ms 300ms 600ms Ruby Ruby YJIT C ryst a l C om piled 21.5 566.3 92.9 359.1 16 -
… compared to Go …Gotta go fast!!!!!! Go 1.26.1 (compiled)
- 4.09 MB go build -o fib-go fib.go hyperfine - w 3 './fib-go' Benchmark 1: ./fib-go T im e ( m ean ± σ): 21.2 m s ± 0.8 m s [User: 20.4 m s, Syste m : 0.7 m s] Range ( mi n … m ax): 19.8 m s … 23.6 m s 132 runs 0ms 300ms 600ms Ruby Ruby YJIT C ryst a l C om piled G o 21.2 21.5 566.3 92.9 359.1 17 -
A more elaborate benchmark A full interpreter for the Monkey
Language 0s 20s 40s 60s 80s Ruby Ruby YJIT C ryst a l com p G o 6 3.23 24.21 71.56 18 Ruby https://github.com/M a rioAri a sC/pep a / Cryst a l https://github.com/M a rioAri a sC/monyet Go https://github.com/M a rioAri a sC/monkey -
Where to start? “crystalruby is a gem that allows you
to write Crystal code, inlined in Ruby.” https://github.com/wouterken/crystalruby 19 -
Keep building your Crystal toolbox https://github.com/crystal-lang/crystal/wiki/Crystal-Shards-for-Ruby-Gems 20 Ruby Libr a
ry/Fr a mework Cryst a l Equiv a lent R a ils Lucky / M a rten Sin a tr a Kem a l Rubocop Ameb a Sidekiq Sidekiq.cr -
Advanced Topic: Macros “Macros are methods that receive AST nodes
at compile-time and produce code that is pasted into a program” https://crystal-lang.org/reference/1.19/syntax_and_semantics/macros/index.html 22 -
Advanced Topic: Macros (II) “Macros are methods that receive AST
nodes at compile-time and produce code that is pasted into a program” https://crystal-lang.org/reference/1.19/syntax_and_semantics/macros/index.html 23 -
Topics we didn’t cover 24 • Liter a ls •
Arr a y, H a sh, Tuple, N a medTuple a nd others • Concurrency • C Bindings • Testing -
Resources 25 • Cryst a l For Rubyists (From Kem
a l’s a uthor) • O ff ici a l Reference • Awesome Cryst a l • Cryst a l Progr a mming, P a ckt Publishing