Skip to content

Speed up boot time in production

Stan Hu requested to merge sh-avoid-extra-route-reload into master

In production mode, eager loading is enabled, and Devise is configured by default to reload routes before eager loading (https://github.com/plataformatec/devise/blob/962cea2039c72a92691af734ebbd8495dd5c0501/lib/devise/rails.rb#L17). However, in config/application.rb we also attempted to reload routes (https://gitlab.com/gitlab-org/gitlab/blob/e88480a79f130c33c12ecf5efa8bc2ec0a97a867/config/application.rb#L304), which added another 5-10 seconds of delay. In development mode, eager loading is disabled, so we don't see this extra overhead.

To speed boot times, we only reload the routes if eager loading is disabled.

Verification

To confirm that reloading of routes is unnecessary, I used this diff:

diff --git a/config/application.rb b/config/application.rb
index f4def097627..21d3ea28e80 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -301,10 +301,17 @@ module Gitlab
     end
 
     config.after_initialize do
-      # Devise (see initializers/8_devise) already reloads routes if
-      # eager loading is enabled, so don't do this twice since it's
-      # expensive.
-      Rails.application.reload_routes! unless config.eager_load
+      routes_before = Rails.application.routes.routes.map do |route|
+        { alias: route.name, path: route.path.spec.to_s, controller: route.defaults[:controller], action: route.defaults[:action] }
+      end
+
+      Rails.application.reload_routes!
+
+      routes_after = Rails.application.routes.routes.map do |route|
+        { alias: route.name, path: route.path.spec.to_s, controller: route.defaults[:controller], action: route.defaults[:action] }
+      end
+
+      puts "Routes same? #{routes_before == routes_after}, count before is #{routes_before.count}, count after is #{routes_after.count}"

 
       project_url_helpers = Module.new do
         extend ActiveSupport::Concern

Production

Routes same? true, count before is 1632, count after is 1632

Development

Routes same? false, count before is 0, count after is 1632

Relates to #213992

Edited by Stan Hu

Merge request reports