Crystal is a new, elegant, multi-paradigm programming language that is productive and fast. It has R̶u̶b̶y̶’̶s a Ruby-inspired syntax and compiles to native code. It is actually unreal how similar to Ruby this language looks like.This language combines efficient code with developer productivity, adds full OOP, a great concurrency model and a compiler that holds your hand.
This article is meant to give you a short overview, a direct performance comparison to Ruby and show some things that set it apart. It is advised you know at least some Ruby before continuing on reading.
Let’s actually get a feel as to how performant Crystal is.I wrote an AA Tree in both Crystal and Ruby.Note: Code quality might not be top-notch. Some lines of Crystal code are intentionally written more explicitly
We are going to be running this code to benchmark each implementation:
What this essentially does is it adds numbers to our tree (which sorts them internally) and then removes each one, one by one. We also check if the tree contains the given number twice per addition/deletion.
The code snippet above is actually Crystal code.Like I said, these languages are identical at first glance. Rewriting the code from Crystal to Ruby took me a total of 50 line changes for a 360 line file. 27 if you were greedyIt is worth noting that those changes are simply removing .as()
method calls and type annotations.
Let’s build the executable and start testing
> enether$ crystal build AA_Tree.cr -o crystal_tree — release
# 100 elements> enether$ ./crystal_tree 100Time it took: 0.0006560 seconds.> enether$ ruby AA_Tree.rb 100Time it took: 0.00172 seconds.
# 10K elements> enether$ ./crystal_tree 10000Time it took: 0.0044000 seconds.> enether$ ruby AA_Tree.rb 10000Time it took: 0.288619 seconds.
# 100K elements> enether$ ./crystal_tree 100000Time it took: 0.0498230 seconds.> enether$ ruby AA_Tree.rb 100000Time it took: 3.414404 seconds.
# 1 million elements> enether$ ./crystal_tree 1000000Time it took: 0.5007820 seconds.> enether$ ruby AA_Tree.rb 1000000Time it took: 39.370083 seconds.
# 10 million elements> enether$ ./crystal_tree 100000000Time it took: 5.6283920 seconds.> enether$ ruby AA_Tree.rb 100000000# Still running
As you can see, it runs laps around Ruby and proves to be ~80 times faster if we were to judge by our 1 millions elements example.
Despite the similarities, there are substantial differences to Ruby, here we will highlight the most obvious and interesting ones.
The most apparent difference is that Crystal uses and mostly enforces types for variables. It has great type inference — if you do not explicitly define the type of a variable the compiler figures it out itself.
The way this language does typing is a sort of mix between static and dynamic typing. It allows you to change a variable’s type
but it also allows you to enforce a variable’s type
Were you wondering what the (Int32 | String)
type was in the error message above?This is a so-called type union, which is a set of multiple types.If we were to enforce a
to be a union of Int32
and String
, the compiler would allow us to assign either type to that variable as it knows to expect both.
The compiler can figure out the type of a variable himself in most cases. The type inference algorithm is specifically built to work when the type of the variable is obvious to a human reader and does not dig too deep into figuring out the specific type.
In the cases where multiple conditions are plausible, the compiler puts a union type on the variable. Crystal code won’t compile if the possible types do not support a given method invoked on them.
This is the way the compiler protects you from silly mistakes with mismatched types, something that is really common in dynamic languages. Its like having your very own programming assistant!
The compiler is smart enough to figure out when a variable is obviously from a given type
There are ways to ensure the compiler that the appropriate type is set. puts a.as(String).camelcase
This checks that the a
variable is a string and if it is not, it throws an error.
As we said, we have the option to enforce a variable’s type or let it be whatever.This holds true for a method’s parameters as well.
It is usually good practice to not enforce a variable, as it leads to more generic code.
Its concurrent model is inspired by that of Go, namely CSP (Communication Sequential Processing).It uses lightweight threads (called fibers) whose execution is managed by the runtime scheduler, not the operating system. Communication between said threads is done through channels, which can either be buffered or unbuffered.
A lot of fibers who communicate between each other through channels
Crystal currently runs in a single thread but their roadmap intends to implement multithreading. This means that it currently has no support for parallelism (except for process forking), but that is subject to change.
Because at this moment there’s only a single thread executing your code, accessing and modifying a variable in different fibers will work just fine. However, once multiple threads is introduced in the language, it might break. That’s why the recommended mechanism to communicate data is through channels.
Crystal has good support for metaprogramming through macros. A macro is something that pastes code into the file during compilation.
Let’s define our own version of Ruby’s attr_writer
Calling attr_writer(foo, Int32)
will evaluate to
def foo(foo : Int32) @foo = fooend
Crystal macros support iteration and conditionals and can access constants.
Crystal has taken a lot of cool features from other languages and provides various syntax sugar that is oh so sweet!
def initialize(@name, @age, @gender, @nationality)
is equal to
def initialize(name, age, gender, nationality)@name = name@age = age@gender = gender@nationality = nationalityend
def initialize(name, age, gender, nationality)@name = name@age = age@gender = gender@nationality = nationalityend
Switch statements support invoking methods on the giving object without repeatedly specifying its name.
My personal favorite — Crystal allows you to name a function’s parameters one way for the outside world and one way for the method’s body
As you saw earlier, this language is compiled to an executable. Regardless, it still has something like a REPL which proves to be similar to our beloved irb
— https://github.com/crystal-community/icrYou can also directly run a file without having to compile it and then run it, via the crystal
command.
> enether$ crystal AA_Tree.cr 200000Time it took: 0.536102 seconds.
This runs a little bit slower because we do not make use of the optimizations that the - release
build flags brings with itself.
There is a way to write a performant library in Crystal which you can run in your Ruby code. The way you do this is to bind Crystal to C, which allows you to use it from Ruby.I did not delve too deep into this but apparently it is easy and you can do it without writing a single line of C. That is awesome!
If you write Ruby, picking up Crystal is natural and can quickly find yourself writing performance-critical software in it. I believe it has a lot of potential and can yield a lot of benefits to our community but also to non-ruby programmers, as the syntax is just too easy to pass up. It is a joy to write and it runs blazingly fast, that is an unique combination which very few languages can boast with.I hope I’ve sparked your interest by these short examples! I strongly encourage you to take a look at the language for yourself and notify me if I’ve missed something.
Here are some resources to read further up on:Google GroupGitter ChatIRCSubredditNewsletter