Ruby

Micro Talk: Introducing the Crystal Programming Language

Micro Talk: Introducing the Crystal Programming Language

by Will Leinweber

The video titled "Micro Talk: Introducing the Crystal Programming Language" features Will Leinweber discussing the Crystal programming language, which is heavily inspired by Ruby, yet offers substantial differences that make it appealing to developers, especially those familiar with Ruby.

Key Points Discussed:

- Crystal Overview:

- Crystal is a compiled programming language that utilizes the LLVM toolchain, leading to performance benefits.

- Unlike Ruby, Crystal is statically typed and uses static dispatch, enhancing safety and performance.

  • Developer Familiarity:

    • Crystal's syntax and idioms are reminiscent of Ruby, making it easier for Ruby developers to adopt.
    • Type inference allows developers to write code without always declaring types explicitly, resembling Ruby's duck typing.
  • Performance Advantages:

    • Crystal is significantly faster than Ruby, with improved RAM efficiency.
    • Benchmarks show it can handle requests per second effectively while consuming less memory.
    • Example: Mike Herman reported building a minimum viable implementation of Sidekiq in three days with Crystal, showcasing immediate performance improvements.
  • Object-oriented Features:

    • Crystal maintains Ruby's object-oriented principles, blocks, and a similar standard library.
    • It also includes a testing framework that allows for easy specification writing.
  • Type System & Safety:

    • Crystal's compilation checks for type correctness, catching errors before runtime.
    • Supports union types for flexibility, allowing a method to support multiple return types while ensuring safety.
  • Macro System:

    • The macro system enhances functionality like property access, allowing clearer code and better debugging outputs.
  • Interoperability:

    • Linking to C libraries is straightforward, enabling easy incorporation of existing functionality, such as querying a PostgreSQL database.
  • Memory Management:

    • Differences in struct and class memory allocation: structs are stack-allocated, while classes are heap-allocated, influencing garbage collection methods.
  • Concurrency Support:

    • Inspired by Go, Crystal's concurrency model employs co-routines and channels, providing familiarity for developers accustomed to Go's concurrency concepts.

Conclusion:

The session emphasizes Crystal's potential for Ruby developers seeking both a similar but more performance-oriented language. Its unique features, especially the robust type system and efficient performance, make it a compelling option in the landscape of modern programming languages.

Will Leinweber encourages viewers to explore Crystal further and consider the benefits it offers in terms of developer happiness, performance, and ease of use.

00:00:15.330 Thank you for the introduction. I'm really happy to be here. I've heard so much about this conference over the years, and I'm glad I could make it.
00:00:17.470 If you have any questions about the talk later on, this is where you can get in touch with me. Now, let's talk about Crystal.
00:00:25.510 I've been a contributor to the Crystal programming language for a little over a year and even wrote a Ghost driver for it because PostgreSQL is pretty cool.
00:00:35.410 So, what is Crystal? It is a language that is heavily inspired by Ruby, and if you come to it as a Rubyist, you'll feel right at home. However, it is compiled and uses a toolchain called LLVM.
00:00:42.460 LLVM powers a lot of the Apple ecosystem, PlayStation, and others. That's really important because many advances in other languages benefit Crystal as well, resulting in significant speed improvements.
00:01:02.800 Crystal is entirely self-hosted, which means that the lexer, parser, compiler, and standard library are written in Crystal itself. This makes it easy to contribute to and understand how things work.
00:01:25.600 One key aspect is that if you write your own code, the optimizations can extend to the standard library without any barriers, unlike the separation that exists when working with languages like C and Ruby.
00:01:47.260 The main difference, however, is that Crystal is statically typed with static dispatch. You might think this isn't very Ruby-like, but I will show you examples that demonstrate how it still retains some of Ruby's feel.
00:02:01.030 In many cases, you won't need to declare types explicitly due to Crystal's type inference system. It has a unique way of handling unions of types; if a method can return multiple types, it creates a union.
00:02:28.209 This allows it to feel like duck typing, making the language both fast and familiar. This is meant to get you excited about Crystal.
00:02:49.240 If we compare Crystal on the left and Ruby on the right, you can see that the structure and syntax are quite similar. For instance, it works like Sinatra in Ruby.
00:03:03.169 In performance benchmarks, Crystal is significantly faster and consumes less RAM, achieving two more requests per second while using two orders of magnitude less RAM.
00:03:45.019 Benchmark results should be taken with a grain of salt; however, they clearly indicate that even for regular codes, you can expect significant improvements.
00:03:58.159 Mike Herman, creator of Sidekiq, shared that just three days after starting with Crystal, he was able to build a minimum viable implementation of Sidekiq and experienced immediate improvements.
00:04:14.060 Regarding similarities, Crystal maintains object-oriented principles, blocks, and much of the standard library from Ruby. For example, this code in Crystal operates the same way it does in Ruby.
00:04:29.440 It even has a testing framework that enables you to write specs easily, giving you a familiar environment to work in.
00:04:48.680 However, it's important to note some differences that set Crystal apart from simply being a compiled version of Ruby.
00:05:00.680 For instance, the way properties are defined is slightly different, and there's type annotation involved. One appealing feature in Crystal is that you can simplify the use of instance variables with a straightforward syntax.
00:05:50.060 Crystal also offers a more elegant way for methods to accept blocks and chain requests, streamlining the syntax without needing to revert to the larger block syntax.
00:06:16.560 The biggest difference lies in its type system, as it is a statically typed language. This means that type checking occurs at compile time, enhancing performance and catching errors ahead of time.
00:06:38.520 For instance, Crystal can generate two different methods for a function that accepts multiple types, allowing for efficient coding while maintaining clarity.
00:07:05.480 Union types allow for flexibility in Crystal while providing the benefits of traditional typing. The distinction between compile-time types and runtime types helps prevent runtime errors.
00:07:30.260 In Crystal, if you invoke a function that relies on a specific data type and the wrong type is used, the compiler catches this error before runtime, ensuring safer code.
00:07:47.250 Moreover, Crystal allows type annotations, further organizing code and making it easier for developers to understand the expected data types while improving overall performance.
00:08:02.060 Through comprehensive type definitions and method overloading, Crystal simplifies coding. You can still maintain an elegant interface while avoiding cumbersome runtime type checks.
00:08:37.710 Macros in Crystal also allow for effective use of property readers and writers. For example, pretty-printing code can significantly enhance debugging by providing clearer outputs.
00:09:24.389 Linking to C libraries in Crystal is remarkably straightforward. Just a few lines of code can pull up functions and allow you to execute a query against an active PostgreSQL database.
00:09:51.389 This simplicity is particularly powerful for a new language like Crystal, which benefits from leveraging existing C libraries.
00:10:10.679 Lastly, one of the major distinctions between structures and classes in Crystal pertains to memory allocation. Structs are allocated on the stack, while classes reside on the heap and require garbage collection, influencing performance and efficiency.
00:10:30.520 With structs, you maintain efficiency by avoiding garbage collection, which enhances performance when working with many small objects.
00:10:45.580 Beyond that, string handling in Crystal also optimizes performance by rewriting constructs at compile time to prevent inefficient string concatenation.
00:10:59.560 Moreover, a lot of the concurrency support leans on the Go community's concepts, utilizing co-routines and channels.
00:11:20.130 So, if you're familiar with those concepts, you'll find them similar in Crystal. Thank you very much for your time, and if you're interested in trying Crystal, I encourage you to explore it.