From 94643fde8f5f2a47faede2c1bdd1b4b786e2685f Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 13 Dec 2021 15:43:39 -0800 Subject: [PATCH] Reuse view_context_class when possible The generated view context classes tend to be fairly complex and use a lot of memory. Similar to how we only generate new helper classes when necessary (see https://github.com/rails/rails/pull/40204) we should be doing the same for view context classes. --- actionview/lib/action_view/rendering.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index ae30f5435a..e93f398876 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -48,7 +48,18 @@ def _routes def _helpers end + def inherit_view_context_class? + superclass.respond_to?(:view_context_class) && + supports_path? == superclass.supports_path? && + _routes.equal?(superclass._routes) && + _helpers.equal?(superclass._helpers) + end + def build_view_context_class(klass, supports_path, routes, helpers) + if inherit_view_context_class? + return superclass.view_context_class + end + Class.new(klass) do if routes include routes.url_helpers(supports_path)