Using Ruby 3 in Rails 6

angga kusumandaru
2 min readJan 3, 2021

--

And how much performance increase

Ruby 3 has been released on 25 December 2020, and increase speed performance almost 3 times when compare with Ruby 2.0 (not latest one 2.7.2) and also when using JIT. We will try to check live world performance using Ruby on Rails 6. As comparison we check speed using ruby 2.7.2 (latest Ruby 2 when these article published) instead long ago Ruby 2.0.0.

Ruby is interpreted language, it need to read source code and gets broken down into byte code and sent an interpreter to be executed. On other side, compiled language like Java, Swift, C, program which you’re writing, need to turn it into a native application. Then you run that compiled code.

JIT — Just In Time acronym is a hybrid of compiled and interpreted languages. First it run on interpreted, and when found some method / function called in multiple times, it compiled that specific function to got optimisation effect.

JIT already birth on Ruby 2.6, but it show some deformed performance in Rails because the process of optimising individual methods is slower since for larger and complex methods is actually slower than interpreting that method directly. And since each methods consume 2Mb memory it float exponentially and burden machines

On Ruby 2.7, there is improvement for JIT like these articles, consist of JIT max size and minimize call size, de-optimized compilation and Frame-omitted method inlining.

https://github.com/mame/optcarrot
  • Installation
brew update && brew upgrade ruby-buildrbenv install 3.0.0rbenv local 3.0.0gem install railsrails new ruby3 -d postgresql
  • Add some gem for test and benchmark
gem 'rspec-rails'gem 'rspec-benchmark'gem 'rexml' 
# stream parsing need for Rails 6
# add require 'rexml/document' on application.rb

Now we try to test with several aspect, with use Ruby 2.7.2, Ruby 2.7.2 JIT, Ruby 3.0.0 and Ruby 3.0.0 JIT

benchmark

For above benchmark, we run 3 times to got average speed. Speed is increase about 8 percent only in same function, for database interaction (active record) and cpu intensive task it not have some improvement. At least it not slow down Rails 6 as before Ruby 2.6 release. see these articles

So is it worth it to move from Ruby 2.7 to Ruby 3?

If you just common Rails consumer which need day to day operation to database, message broker or third party, improvement is par.

But if you need power of concurrency for internal calculation or parallel request like Ractor, along with Async Fiber you can considered to move into Ruby 3

--

--