This blog is part of our Ruby 3.0 series.
For all Rubyists, 2020 was a special year. Why wouldn't it be? Ruby 2 was released in 2013. We have been using Ruby 2.x for almost 7 years and we have been waiting to see Ruby 3 get released. Finally, the wait is over now. Ruby 3.0.0 has been released. It's time to unwrap the gift box and see all the Ruby 3 features we got.
Ruby 3 major updates
The number 3 is very significant in the Ruby 3 release. Be it release version number, making performance 3x faster, or the trio of core contributors(Matz, TenderLove, Koichi). Similarly, there were 3 major goals of Ruby 3: being faster, having better concurrency, and ensuring correctness.
1. Ruby 3 performance
One of the major focuses for Ruby 3 was the performance. In fact, the initial discussion for Ruby 3 was started around it. Matz had set a very ambitious goal of making Ruby 3 times faster.
What is Ruby 3x3?
Before discussing this, let's revisit Ruby's core philosophy.
"I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy." - Matz
About Ruby 3x3, some asked whether the goal was to make Ruby the fastest language? The answer is no. The main goal of Ruby 3x3 was to make Ruby 3 times faster than Ruby 2.
“No language is fast enough.” - Matz
Ruby was not designed to be fastest and if it would have been the goal, Ruby wouldn't be the same as it is today. As Ruby language gets performance boost, it definitely helps our application be faster and scalable.
"In the design of the Ruby language we have been primarily focused on productivity and the joy of programming. As a result, Ruby was too slow." - Matz
There are two areas where performance can be measured: memory and CPU.
CPU optimization
Some enhancements in Ruby internals have been made to improve the speed. The Ruby team has optimized the JIT(Just In Time) compiler from previous versions. Ruby MJIT compiler was first introduced in Ruby 2.6. Ruby 3 MJIT comes with better security and seems to improve web apps’ performance to a greater extent.
MJIT implementation is different from the usual JIT. When methods get called repeatedly e.g. 10000 times, MJIT will pick such methods which can be compiled into native code and put them into a queue. Later MJIT will fetch the queue and convert them to native code.
Please check JIT vs MJIT for more details.
Memory optimization
Ruby 3 comes with an enhanced garbage collector. It has python's buffer-like API which helps in better memory utilization. Since Ruby 1.8, Ruby has continuously evolved in Garbage collection algorithms.
Automatic Garbage Compaction
The latest change in garbage collection is Garbage Compaction. It was introduced in Ruby 2.7 where the process was a bit manual. But in version 3 it is fully automatic. The compactor is invoked aptly to make sure proper memory utilization.
Objects Grouping
The garbage compactor moves objects in the heap. It groups dispersed objects together at a single place in the memory so that this memory can be used even by heavier objects later.
2. Parallelism and Concurrency in Ruby 3
Concurrency is one of the important aspects of any programming language. Matz feels that Threads are not the right level of abstraction for Ruby programmers to use.
“I regret adding Threads.” - Matz
Ruby 3 makes it a lot easier to make applications where concurrency is a major focus. There are several features and improvements added in Ruby 3 related to concurrency.
Fibers
Fibers are considered a disruptive addition in Ruby 3. Fibers are light-weight workers which appear like Threads but have some advantages. It consumes less memory than Threads. It gives greater control to the programmer to define code segments that can be paused or resumed resulting in better I/O handling.
Falcon Rack web server uses Async Fibers internally. This allows Falcon to not block on I/O. Asynchronously managing I/O gives a great uplift to the Falcon server to serve requests concurrently.
Fiber Scheduler
Fiber Scheduler is an experimental feature added in Ruby 3. It was introduced to intercept blocking operations such as I/O. The best thing is that it allows lightweight concurrency and can easily integrate into the existing codebase without changing the original logic. It's an interface and that can be implemented by creating a wrapper for a gem like EventMachine or Async. This interface design allows separation of concerns between the event loop implementation and the application code.
Following is an example to send multiple HTTP requests concurrently using Async.
1require 'async' 2require 'net/http' 3require 'uri' 4 5LINKS = [ 6 'https://bigbinary.com', 7 'https://basecamp.com' 8] 9 10Async do 11 LINKS.each do |link| 12 Async do 13 Net::HTTP.get(URI(link)) 14 end 15 end 16end
Please check fibers for more details.
Ractors (Guilds)
As we know Ruby’s global VM lock (GVL) prevents most Ruby Threads from computing in parallel. Ractors work around the GVL to offer better parallelism. Ractor is an Actor-Model like concurrent abstraction designed to provide a parallel execution without thread-safety concerns.
Ractors allows Threads in different Ractors to compute at the same time. Each Ractor has at least one thread, which may contain multiple fibers. In a Ractor, only a single thread is allowed to execute at a given time.
The following program returns the square root of a really large number. It calculates the result for both numbers in parallel.
1# Math.sqrt(number) in ractor1, ractor2 run in parallel 2 3ractor1, ractor2 = *(1..2).map do 4 Ractor.new do 5 number = Ractor.recv 6 Math.sqrt(number) 7 end 8end 9 10# send parameters 11ractor1.send 3**71 12ractor2.send 4**51 13 14p ractor1.take #=> 8.665717809264115e+16 15p ractor2.take #=> 2.251799813685248e+15
3. Static Analysis
We need tests to ensure correctness of our program. However by its very nature tests could mean code duplication.
“I hate tests because they aren't DRY.” - Matz
To ensure the correctness of a program, static analysis can be a great tool in addition to tests.
The static analysis relies on inline type annotations which aren't DRY. The solution to address this challenge is having .rbs files parallel to our .rb files.
RBS
RBS is a language to describe the structure of a Ruby program. It provides us an overview of the program and how overall classes, methods, etc. are defined. Using RBS, we can write the definition of Ruby classes, modules, methods, instance variables, variable types, and inheritance. It supports commonly used patterns in Ruby code, and advanced types like unions and duck typing.
The .rbs files are something similar to .d.ts files in TypeScript. Following is a small example of how a .rbs file looks like. The advantage of having a type definition is that it can be validated against both implementation and execution.
The below example is pretty self-explanatory. One thing we can note here though, each_post accepts a block or returns an enumerator.
1# user.rbs 2 3class User 4 attr_reader name: String 5 attr_reader email: String 6 attr_reader age: Integer 7 attr_reader posts: Array[Post] 8 9 def initialize: (name: String, 10 email: String, 11 age: Integer) -> void 12 13 def each_post: () { (Post) -> void } -> void 14 | () -> Enumerator[Post, void] 15end
Please check RBS gem documentation for more details.
Typeprof
Introducing type definition was a challenge because there is already a lot of existing Ruby code around and we need a tool that could automatically generate the type signature. Typeprof is a type analysis tool that reads plain Ruby code and generates a prototype of type signature in RBS format by analyzing the methods, and its usage. Typeprof is an experimental feature. Right now only small subset of ruby is supported.
“Ruby is simple in appearance, but is very complex inside, just like our human body.” - Matz
Let's see an example.
1# user.rb 2class User 3 4 def initialize(name:, email:, age:) 5 @name, @email, @age = name, email, age 6 end 7 8 attr_reader :name, :email, :age 9end 10 11User.new(name: "John Doe", email: '[email protected]', age: 23)
Output
1$ typeprof user.rb 2 3# Classes 4class User 5 6 attr_reader name : String 7 attr_reader email : String 8 attr_reader age : Integer 9 10 def initialize : (name: String, 11 email: String, 12 age: Integer) -> [String, String, Integer] 13 14end
Other Ruby 3 features and changes
In the 7-year period, the Ruby community has seen significant improvement in performance and other aspects. Apart from major goals, Ruby 3 is an exciting update with lots of new features, handy syntactic changes, and new enhancements. In this section, we will discuss some notable features.
“We are making Ruby even better.” - Matz
One-line pattern matching syntax change
Previously one-line pattern matching used the keyword in. Now it's replaced with =>.
Ruby 2.7
1 { name: 'John', role: 'CTO' } in {name:} 2 p name # => 'John'
Ruby 3.0
1 { name: 'John', role: 'CTO' } => {name:} 2 p name # => 'John'
Find pattern
The find pattern was introduced in Ruby 2.7 as an experimental feature. This is now part of Ruby 3.0. It is similar to pattern matching in Elixir or Haskell.
1users = [ 2 { name: 'Oliver', role: 'CTO' }, 3 { name: 'Sam', role: 'Manager' }, 4 { role: 'customer' }, 5 { name: 'Eve', city: 'New York' }, 6 { name: 'Peter' }, 7 { city: 'Chicago' } 8] 9 10users.each do |person| 11 case person 12 in { name:, role: 'CTO' } 13 p "#{name} is the Founder." 14 in { name:, role: designation } 15 p "#{name} is a #{designation}." 16 in { name:, city: 'New York' } 17 p "#{name} lives in New York." 18 in {role: designation} 19 p "Unknown is a #{designation}." 20 in { name: } 21 p "#{name}'s designation is unknown." 22 else 23 p "Pattern not found." 24 end 25end 26 27"Oliver is the Founder." 28"Sam is a Manager." 29"Unknown is a customer." 30"Eve lives in New York." 31"Peter's designation is unknown." 32"Pattern not found."
Endless Method definition
This is another syntax enhancement that is optional to use. It enables us to create method definitions without end keyword.
1def: increment(x) = x + 1 2 3p increment(42) #=> 43
Except method in Hash
Sometimes while working on a non Rails app I get undefined method except. The except method was available only in Rails. In Ruby 3 Hash#except was added to Ruby itself.
1user = { name: 'Oliver', age: 29, role: 'CTO' } 2 3user.except(:role) #=> {:name=> "Oliver", :age=> 29}
Memory View
This is again an experimental feature. This is a C-API that will allow extension libraries to exchange raw memory area. Extension libraries can also share metadata of memory area that consists of shape and element format. It was inspired by Python’s buffer protocol.
Arguments forwarding
Arguments forwarding (...) now supports leading arguments.
It is helpful in method_missing, where we need method name as well.
1def method_missing(name, ...) 2 if name.to_s.end_with?('?') 3 self[name] 4 else 5 fallback(name, ...) 6 end 7end
Other Notable changes
- Pasting in IRB is much faster.
- The order of backtrace had been reversed. The error message and line number are printed first, rest of the backtrace is printed later.
- Hash#transform_keys accepts a hash that maps old keys with new keys.
- Interpolated String literals are no longer frozen when # frozen-string-literal: true is used.
- Symbol#to_proc now returns a lambda Proc.
- Symbol#name has been added, which returns the symbol's name as a frozen string.
Many other changes can be checked at Ruby 3 News for more details.
Transition
A lot of core libraries have been modified to fit the Ruby 3 goal needs. But this doesn't mean that our old applications will suddenly stop working. The Ruby team has made sure that these changes are backward compatible. We might see some deprecation warnings in our existing code. The developers can fix these warnings to smoothly transition from an old version to the new version. We are all set to use new features and get all new performance improvements.
Conclusion
With great improvements in performance, memory utilization, static analysis, and new features like Ractors and schedulers, we have great confidence in the future of Ruby. With Ruby 3, the applications can be more scalable and more enjoyable to work with. The coming 2021 is not just a new year but rather a new era for all Rubyists. We at BigBinary thank everyone who contributed towards the Ruby 3 release directly or indirectly.
Happy Holidays and Happy New Year folks!!!