Table of contents
    blog cover

    Notable Changes in Ruby on Rails 7.2

    Software Engineer
    Software Engineer
    Ruby on Rails
    Ruby on Rails
    Ruby on Rails 7.2 has arrived with a host of new features and enhancements that focus on improving the performance and efficiency of your applications. In this blog post, we'll delve into some of the most noteworthy changes.
    Ref: Ruby on Rails 7.2 Release Notes — Ruby on Rails Guides

    Jemalloc: Optimize Memory Allocation

    One of the standout updates in Rails 7.2 is the inclusion of Jemalloc in the default Dockerfile. Jemalloc is a memory allocator designed to reduce fragmentation and improve memory usage efficiency, particularly in multi-threaded environments.

    This is especially beneficial for applications running on platforms like Heroku, where optimized memory usage can lead to cost savings and performance improvements.
    In our case, integrating Jemalloc into our Rails application resulted in a significant reduction in memory usage on Heroku, dropping from 6GB to 4GB.

    To implement Jemalloc in your Heroku application, you can use the Heroku Buildpack for Jemalloc.

    You can read more details in the jemalloc with Ruby and Docker post.
    Memory improvement


    Puma Worker: New Default Thread Count

    Rails 7.2 introduces a new default configuration for the Puma web server, which reduces the default number of threads from 5 to 3. This change is based on extensive analysis and real-world experience, indicating that a lower thread count can lead to better performance for Rails applications.

    The rationale behind this update is that Ruby applications often spend a significant amount of time waiting for the Global VM Lock (GVL) to release when the thread count is too high. By optimizing the thread count to 3, Rails applications can achieve a more balanced performance, reducing latency and improving request response times.

    Please note that you have to test and adjust the number of workers/threads based on your computer resources. There is no one-fit configuration for all apps. Different applications and environments may require different settings, so it's essential to monitor your application's performance and adjust the configuration accordingly to achieve optimal results.

    -------
    The Global VM Lock (GVL), also known as the Global Interpreter Lock (GIL) in some contexts, is a mutex that protects access to Ruby's interpreter internals. It is a mechanism used in certain implementations of Ruby, particularly CRuby (the reference implementation), to ensure that only one thread executes Ruby code at a time.

    Enable YJIT by default if running Ruby 3.3+

    Another exciting addition to Rails 7.2 is the default enablement of YJIT (Yet another JIT), Ruby's Just-In-Time compiler, for applications running on Ruby 3.3 or newer. YJIT has been shown to offer significant performance gains, with improvements of 15-25% in latency for Rails applications.

    YJIT accelerates Ruby code execution by compiling Ruby code into native machine code, which can be executed more quickly by the CPU. This provides a substantial boost in performance, especially for applications with complex computations and large codebases.

    To take advantage of YJIT, ensure that your application is running on Ruby 3.3 or later. If, for some reason, you wish to disable YJIT, you can do so by setting the following configuration in your Rails application:

    // language: ruby
    Rails.application.config.yjit = false

    Add Browser Version Guard by Default

    Rails 7.2 introduces a built-in browser version guard, allowing developers to specify which browser versions are permitted to access their applications. This feature can be configured globally or on a per-action basis using the `allow_browser` method. Browsers below the specified versions will be blocked and served a "406 Not Acceptable" status page.

    For example, you can allow only modern browsers that support specific features:
    // language: ruby
    class ApplicationController < ActionController::Base
      allow_browser versions: :modern
    end

    This addition helps ensure that your application provides a consistent experience for users by preventing access from outdated or unsupported browsers.
    Created at 2024-08-17 18:00:24 +0700

    Related blogs