Yes, yes, it does
In my never-ending quest to find harmony between speed of doing things (development speed) and speed of the thing (performance), I came across a project in its incubation phases which had me thinking: “This could be the one language for me.”
It was infatuation at first sight with Crystal, a programming language built for humans and computers. What a noble cause. Having been a fan of the beauty of the Ruby syntax, the promise of Ruby-like syntax with the speed of C was intriguing — life-altering, even.
Since that day, I’ve been closely following the progress of Crystal and today, I will make a case for why you should care. It’s quite honestly one of the most exciting languages with promise for great potential.
Before we get into it, keep in mind that Crystal is not yet ready for production, but you can still find many projects already using it — like this version of Sidekiq, written in Crystal.
Why Crystal?
So, why should anyone care about yet another programming language? Well, because Crystal has combined a concoction of incredibly compelling ingredients that you won’t find in many other languages.
Beautiful syntax
One of the most appealing things about Crystal is the clean and readable Ruby-like syntax. The creators of the language understood the appeal of Ruby as one of the most visually appealing languages and they took it as the inspiration for Crystal.
So, if you are coming from the Ruby world, transitioning to Crystal is going to be straightforward. Most of the time, you will be able to run Ruby code directly in Crystal, or run Crystal programs within a Ruby shell.
To top it all off, you can even use Ruby syntax highlighting with Crystal. Similar to most interpreted languages, Crystal will let you build your wildest imaginations in a few lines of code.
Blazing-fast performance
Crystal is a statically compiled language built on top of the revered LLVM framework and can hold its own against the likes of C, C++, and Rust.
Just let that sink in a little bit… The development speed of Ruby syntax with speeds matching C…
It’s incredibly compelling and hopefully has you as excited as I was when I first heard that claim. Don’t believe me? Just check out some of the latest benchmarks; this benchmark and this benchmark.
Hyper-performance with easy C bindings
If a part of your application or algorithm requires extreme performance, one strategy is to offload the functionality to a C extension or library.
With Crystal, binding to existing C libraries or your own C libraries can be done without writing a single line of C code.
Consider a quick example of a C library hello.c
that we can build using the GCC compiler gcc -c hello.c -o hello.o
.
#include <stdio.h>
void hello(const char * name){
printf("Hello %s!\n", name);
}
After building the binary, you can easily link the binary using Link
and define the lib
declaration which groups the functions and types belonging to that library — followed by calling your function. Voila!
#hello.cr
@[Link(ldflags: "#{__DIR__}/hello.o")]
lib Say
fun hello(name : LibC::Char*) : Void
end
Say.hello("your name")
Static typing
Crystal is a statically-typed language, allowing it to rule out many type-related bugs at compile-time and setting the stage for optimizations that would not be possible in dynamically-typed languages like Ruby or Python.
This directly contributes to the performance of Crystal and what’s even more impressive is that the compiler in Crystal only requires you to explicitly specify types in case of ambiguity — the rest of the time you can work with it like any dynamic language.
Macros
Macros are a way to modify the abstract syntaxtTree created during a programming language’s tokenizing and parsing phase, allowing us to add methods at compile time or create and modify classes.
The main advantage of this is speed — as you save a lot of time spent by the compiler for invoking/calling functions.
Crystal lets you utilize the majority of the language when it comes to writing macros, which means you can do crazy wizardry that normally would be unheard of in a statically-compiled language.
Web frameworks
Any comparison isn’t complete without talking about available web frameworks in the language and if you’ve already fallen in love with the likes of Rails and Phoenix — you’ll feel right at home with Crystal’s web framework Amber.
It was designed from the ground up to follow Rails but is obviously an order of magnitude faster than Rails — with load times in microseconds not milliseconds.
If you fall under the camp of Sinatra lovers, fear not because you’ve got the simplicity of the Kemal framework.
Did I mention that Crystal’s built-in HTTP server has been able to handle over 2 million requests per second in benchmark testing? And most of the frameworks also deliver sub-millisecond response times for web applications.
Concurrency
Crystal currently supports concurrency as a first-class citizen, while parallelism is making its way up the ranks — you can read about its progress on the Crystal website.
Concurrency is supported in Crystal using Fibers — a lighter-weight version of an operating system thread. Unlike threads, which are pre-emptive, Fibers explicitly tell the runtime scheduler when to switch context. This helps Crystal avoid unnecessary context switching.
If you want to add concurrency, you’ll find the spawn
method in Crystal similar to the use of goroutines in Go — which I am personally a huge fan of.
# Simple example
spawn do
sleep 5.seconds
end
Crystal also has support for channels inspired by CSP that allow communicating data between fibers without sharing memory and having to worry about locks or semaphores.
But Hold Up, What’s Not to Like?
As software engineers, we are always making tradeoffs when choosing a programming language or framework and like any language, Crystal isn’t the answer to all your prayers and comes with its own limitations.
Crystal is still relatively young and immature which leads to a lack of community and packages at this particular point in time.
This also results in a scarcity of development tools available even though they are readily becoming available.
If you are aiming to do something incredibly specific, you’ll have trouble finding documentation but this just means we have an opportunity to be the first adopters and hack together cool projects.
Even though concurrency is built into Crystal, work is still being done on parallelism being a first-class citizen.
Due to the pre-production status of the language, there is a possibility of breaking changes until v1.0 is reached.
It also doesn’t have great Windows compatibility but quite honestly, that’s not a negative for me.
Takeaway
It might still be a while until Crystal is ready for production use, with great tools and a thriving community behind it. But it’s comforting to know that a wonderful language like Crystal is in the works behind the scenes.
Looking at all the features that Crystal brings together, it is deserving of a lot of attention and popularity. If I’ve piqued your interest, I would encourage you to check out Crystal and make your own decision.
In the meanwhile, I will continue to evangelize Crytal and keep a keen eye on its progress.
Top comments (0)