Skip to content

Load routes on demand for Devise.mappings instead of reloading them before eager load - #5883

Open
tricknotes wants to merge 2 commits into
heartcombo:mainfrom
tricknotes:load-routes-on-demand-for-mappings
Open

tricknotes wants to merge 2 commits into
heartcombo:mainfrom
tricknotes:load-routes-on-demand-for-mappings

Conversation

@tricknotes

Copy link
Copy Markdown
Contributor

Devise reloads the application routes in a before_eager_load hook so that the mappings declared by devise_for exist while the application code is eager loaded. This has a cost on every eager-loaded boot that grows with the application:

  • Right after eager loading, Rails computes the action methods of every controller (AbstractController::Base.eager_load!). Once the routes are loaded, every controller has the url helpers module in its ancestors, so public_instance_methods and UrlFor::ClassMethods#action_methods (which subtracts helper_names) walk thousands of methods per controller. In an application with ~2,300 controllers and ~9,600 url helpers this took 20+ seconds, more than half of the boot time.
  • On Rails 8, reload_routes! loads the routes and then marks the reloader as not loaded, so the routes reloader hook loads them a second time.

Devise.mappings already loads lazy routes on demand once the application is initialized (#5728). This PR extends that to the initialization phase: when the mappings are read while the application is still initializing and they are empty, the routes are loaded once, exactly what reload_routes = true did unconditionally. With that, the unconditional reload is no longer needed and config.reload_routes now defaults to false; setting it to true keeps the previous behavior.

Note that Devise's own mapping-dependent setup (configure_warden!, regenerate_helpers!) runs when the routes are finalized, so it does not depend on when the routes are loaded. The preload only mattered for application code reading Devise.mappings at load time, which is now covered on demand.

The second commit rewrites the reload_routes comment and the generator template: they said disabling it "can reduce the time taken to boot the app" and warned the app would not boot properly if it needs the mappings during boot, which no longer applies.

Measured on a production-mode boot of a large application (Rails 8.0, Puma 2 workers): time from rails server to the first 200 response went from 55s to 26s with reload_routes = false.

…ager load

Devise reloads the application routes before eager loading so that the
mappings declared by `devise_for` exist while the application code is
loaded. That has a cost on every eager-loaded boot that grows with the
application: right after eager loading, Rails computes the action methods of
every controller, and once the routes are loaded each controller has the
url helpers module in its ancestors, so that computation walks thousands of
methods per controller. On Rails 8 the routes are also loaded a second time
by the routes reloader hook. In an application with 2,300 controllers and
9,600 url helpers this accounted for more than half of the boot time.

`Devise.mappings` already loads lazy routes on demand once the application
is initialized. Extend that to the initialization phase: when the mappings
are read while the application is still initializing and they are empty,
load the routes once, exactly as `reload_routes = true` did unconditionally.
With that, the unconditional reload is not needed anymore and
`reload_routes` defaults to false. Setting it to true keeps the previous
behavior.
The comment on `reload_routes` only said that disabling it "can reduce the
time taken to boot the app", without saying why, and warned that the
application would not boot properly if it needs the mappings during boot.
Since `Devise.mappings` loads the routes on demand, that warning no longer
applies. Describe the actual cost instead: the action methods of every
controller are computed right after eager loading, and that computation is
far slower when the routes' url helpers are already in each controller's
ancestors. Update the generator template to show the new default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant