Repository navigation
Load routes on demand for Devise.mappings instead of reloading them before eager load - #5883
Open
tricknotes wants to merge 2 commits into
Open
tricknotes wants to merge 2 commits into
tricknotes wants to merge 2 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Devise reloads the application routes in a
before_eager_loadhook so that the mappings declared bydevise_forexist while the application code is eager loaded. This has a cost on every eager-loaded boot that grows with the application:AbstractController::Base.eager_load!). Once the routes are loaded, every controller has the url helpers module in its ancestors, sopublic_instance_methodsandUrlFor::ClassMethods#action_methods(which subtractshelper_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.reload_routes!loads the routes and then marks the reloader as not loaded, so the routes reloader hook loads them a second time.Devise.mappingsalready 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 whatreload_routes = truedid unconditionally. With that, the unconditional reload is no longer needed andconfig.reload_routesnow defaults tofalse; setting it totruekeeps 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 readingDevise.mappingsat load time, which is now covered on demand.The second commit rewrites the
reload_routescomment 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 serverto the first 200 response went from 55s to 26s withreload_routes = false.