Skip to content

Commit 2fd3427

Browse files
committed
Eager load controllers view_context_class
These classes are relatively small, however they include lots of modules as helpers. And if any of the included module hold constants including it cause the global constant cache to be invalidated which is really bad for performance. So when eager loading is enabled we create all the possible classes as part of the application boot.
1 parent 62b4ca9 commit 2fd3427

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

actionmailer/lib/action_mailer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def self.eager_load!
5858

5959
require "mail"
6060
Mail.eager_autoload!
61+
62+
Base.descendants.each do |mailer|
63+
mailer.eager_load! unless mailer.abstract?
64+
end
6165
end
6266
end
6367

actionmailer/lib/action_mailer/railtie.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ class Railtie < Rails::Railtie # :nodoc:
7474
end
7575
end
7676

77-
initializer "action_mailer.eager_load_actions" do
78-
ActiveSupport.on_load(:after_initialize) do
79-
ActionMailer::Base.descendants.each(&:action_methods) if config.eager_load
80-
end
81-
end
82-
8377
config.after_initialize do |app|
8478
options = app.config.action_mailer
8579

actionpack/lib/abstract_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ module AbstractController
2424
def self.eager_load!
2525
super
2626
AbstractController::Caching.eager_load!
27+
AbstractController::Base.descendants.each do |controller|
28+
unless controller.abstract?
29+
controller.eager_load!
30+
end
31+
end
2732
end
2833
end

actionpack/lib/abstract_controller/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def method_added(name)
127127
super
128128
clear_action_methods!
129129
end
130+
131+
def eager_load! # :nodoc:
132+
action_methods
133+
nil
134+
end
130135
end
131136

132137
abstract!

actionpack/lib/action_controller/railtie.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Railtie < Rails::Railtie # :nodoc:
1414
config.action_controller.log_query_tags_around_actions = true
1515
config.action_controller.wrap_parameters_by_default = false
1616

17+
config.eager_load_namespaces << AbstractController
1718
config.eager_load_namespaces << ActionController
1819

1920
initializer "action_controller.assets_config", group: :all do |app|
@@ -99,12 +100,6 @@ class Railtie < Rails::Railtie # :nodoc:
99100
end
100101
end
101102

102-
initializer "action_controller.eager_load_actions" do
103-
ActiveSupport.on_load(:after_initialize) do
104-
ActionController::Metal.descendants.each(&:action_methods) if config.eager_load
105-
end
106-
end
107-
108103
initializer "action_controller.query_log_tags" do |app|
109104
query_logs_tags_enabled = app.config.respond_to?(:active_record) &&
110105
app.config.active_record.query_log_tags_enabled &&

actionview/lib/action_view/rendering.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ def build_view_context_class(klass, supports_path, routes, helpers)
7272
end
7373
end
7474

75+
def eager_load!
76+
super
77+
view_context_class
78+
nil
79+
end
80+
7581
def view_context_class
7682
klass = ActionView::LookupContext::DetailsKey.view_context_class(ActionView::Base)
7783

railties/test/application/configuration_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def change
291291
assert_instance_of Pathname, Rails.public_path
292292
end
293293

294-
test "does not eager load controller actions in development" do
294+
test "does not eager load controllers state actions in development" do
295295
app_file "app/controllers/posts_controller.rb", <<-RUBY
296296
class PostsController < ActionController::Base
297297
def index;end
@@ -302,9 +302,10 @@ def show;end
302302
app "development"
303303

304304
assert_nil PostsController.instance_variable_get(:@action_methods)
305+
assert_nil PostsController.instance_variable_get(:@view_context_class)
305306
end
306307

307-
test "eager loads controller actions in production" do
308+
test "eager loads controllers state in production" do
308309
app_file "app/controllers/posts_controller.rb", <<-RUBY
309310
class PostsController < ActionController::Base
310311
def index;end
@@ -320,6 +321,7 @@ def show;end
320321
app "production"
321322

322323
assert_equal %w(index show).to_set, PostsController.instance_variable_get(:@action_methods)
324+
assert_not_nil PostsController.instance_variable_get(:@view_context_class)
323325
end
324326

325327
test "does not eager load mailer actions in development" do

0 commit comments

Comments
 (0)