Skip to content

Eagerly preload framework base classes#752

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
Korri:Korri/preload-application-base-classes
May 1, 2026
Merged

Eagerly preload framework base classes#752
rafaelfranca merged 1 commit into
rails:mainfrom
Korri:Korri/preload-application-base-classes

Conversation

@Korri
Copy link
Copy Markdown
Contributor

@Korri Korri commented Apr 29, 2026

Spring::Application#serve calls Rails.application.reloaders.any?(&:updated?) before forking. ActionView's CacheExpiry::ViewReloader#updated? lazily builds a FileUpdateChecker from whatever view paths are registered at that moment (lazy-init contract from rails/rails#51308).

Engines register view paths via prepend_view_path inside on_load(:action_mailer) / :action_controller_base blocks. In a fresh preload nothing has referenced those framework classes yet, so dirs_to_watch is empty when the probe runs — pinning the watcher empty and triggering a full FileUpdateChecker rebuild on every prepend_view_path in each fork.

Preloading the framework classes (not Application*, per Rafael's suggestion, framework code is never reloaded, app namespace is) targets the on_load hooks directly and avoids coupling Spring's preload to user-reloadable code.

While rails/rails#57269 closes the bug part of this from the Rails side, this still makes sense on it's own.

Comment thread lib/spring/application.rb Outdated
@Korri Korri force-pushed the Korri/preload-application-base-classes branch from e1f2d9f to 742983d Compare April 30, 2026 13:48
@Korri Korri changed the title Eagerly preload application framework base classes Eagerly preload framework base classes Apr 30, 2026
@Korri Korri marked this pull request as ready for review May 1, 2026 14:03
@Korri Korri force-pushed the Korri/preload-application-base-classes branch 2 times, most recently from 932eb70 to 6e708f5 Compare May 1, 2026 14:45
@rafaelfranca rafaelfranca force-pushed the Korri/preload-application-base-classes branch from 6e708f5 to 70dbe8e Compare May 1, 2026 19:35
After `config/environment` is loaded, eagerly autoload framework base
classes (`ActionMailer::Base`, `ActionController::Base`,
`ActionController::API`) so their `ActiveSupport.on_load` hooks fire in
the parent process.

Those classes shouldn't change and sometimes have side effects when
loaded, although the biggest one is getting fixed in Rails
(see rails/rails#57269), it won't hurt to still
preload those classes so they don't get loaded on each fork again.
@rafaelfranca rafaelfranca force-pushed the Korri/preload-application-base-classes branch from 70dbe8e to 964e1a7 Compare May 1, 2026 19:36
@rafaelfranca rafaelfranca enabled auto-merge May 1, 2026 19:37
@rafaelfranca rafaelfranca merged commit 4de40e9 into rails:main May 1, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants