Skip to content

Commit 59e0333

Browse files
committed
Improve shape friendliness of ActionController::Serialization
Lazily defining instance variable can significantly increase the number of shapes on Ruby 3.2+ which isn't good for performance.
1 parent 07da9d0 commit 59e0333

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/action_controller/serialization.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def serialization_scope(scope)
4545
end
4646
end
4747

48+
def initialize(*)
49+
super
50+
@namespace_for_serializer = nil
51+
end
52+
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
53+
4854
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
4955
define_method renderer_method do |resource, options|
5056
serializer = build_json_serializer(resource, options)
@@ -60,7 +66,8 @@ def serialization_scope(scope)
6066
private
6167

6268
def namespace_for_serializer
63-
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
69+
@namespace_for_serializer ||= namespace_for_class(self.class)
70+
@namespace_for_serializer unless @namespace_for_serializer == Object
6471
end
6572

6673
def namespace_for_class(klass)

0 commit comments

Comments
 (0)