Skip to content

Improve boot time of rails commands

Igor Drozdov requested to merge id-improve-boot-time into master

What does this MR do and why?

Improve boot time of rails commands (like rails console).

  1. Move factory_bot into :test category

It increases boot time of rails console, but usually not necessary in development. Can be included as require 'factory_bot if necessary.

lookbook gem also contributes to the boot time, but it seems to be for development only.

bumbler --initializers has load_config_initializers decreased with these optimizations.

To be honest, to track it, I override the run_load_hooks method because bumbler --all didn't show much:

    def run_load_hooks(name, base = Object)
      @loaded[name] << base
      @load_hooks[name].each do |hook, options|
        t = Time.now
        execute_hook(name, base, options, hook)

        r = Time.now - t
        if r.to_i > 1
          p name, base, options, hook
          p r
        end
      end
    end
  1. Use memoized Rails.env instead of ENV['RAILS_ENV']

Gitlab::Environment.static_verification? is called very often. For example, when a method is overridden. So milliseconds matter in this case.

benchmark.rb

require 'benchmark'

Benchmark.bm do |x|
  x.report("ENV['RAILS_ENV']") {
    1_000.times { ENV['RAILS_ENV'] == 'production' }
  }

  x.report("Rails.env.production?") {
    1_000.times { Rails.env.production? }
  }
end
bundle exec rails runner benchmark.rb
                  user     system      total        real
ENV['RAILS_ENV']  2.737894   5.736041   8.473935 (  8.565413)
Rails.env.production?  0.000122   0.000047   0.000169 (  0.000153)

Merge request reports