Sijin T V
Sijin T V A passionate Software Engineer who contributes to the wonders happenning on the internet

Rails 7.2 — Better eager loading with Zeitwerk

With Rails 7.2 a subtle but useful change landed: more efficient eager loading of autoload paths via the Zeitwerk code loader.

What changed

Previously Rails would load all autoload paths during eager_load in production (or when config.eager_load = true), which could include many directories irrelevant for runtime. In 7.2 it now filters and skips certain patterns, reducing boot-time and memory overhead.

Why this matters

  • Faster application startup
  • Less memory consumed by loading unused constants
  • Better behaviour in dev/production parity

How to take advantage

  1. Confirm you’re running Rails 7.2 (check your Gemfile + rails ­-v).
  2. Ensure your app uses Zeitwerk mode (default since Rails 6).
  3. Review config.autoload_paths in config/application.rb — remove obsolete directories.
  4. In production, measure boot-time before & after.
  5. In development, be mindful of reloading: if you add new autoload directories, keep naming consistent so Zeitwerk picks them up.
1
2
3
4
5
6
7
8
9
# config/application.rb
module MyApp
  class Application < Rails::Application
    # default: config.loader = :zeitwerk
    config.autoload_paths += %W(#{config.root}/lib)
    # make sure `lib/` only contains code actually loaded
  end
end

Example scenario

Suppose you had lib/extras/old_scripts added in autoload paths but you don’t actually reference any constants there. With Rails 7.2 you may find that directory is skipped in eager loading which means less load time and memory footprint.

Summary

Rails 7.2 brings a small but meaningful improvement to eager loading via Zeitwerk. If you’re on Rails 7.x, it’s worth reviewing your autoload paths and measuring performance. The fix is simple, the benefit is measurable

comments powered by Disqus