Skip to content

Commit c8e5b0b

Browse files
seanpdoylebyroot
andcommitted
Avoid definition of methods in CurrentAttributes at runtime
Replacing on the fly a `method_missing` by a generated method sound like a nice trick, but it's not as good as it sound for optimization, as the method will be generated by the first request to use it, preventing the ISeq from being is shared memory. Instead we can eagerly define a delegator when instance methods are defined, and keep a regular `method_missing + send` for the very rare cases not covered. Co-Authored-By: Jean Boussier <[email protected]>
1 parent dcd13fc commit c8e5b0b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

activesupport/lib/active_support/current_attributes.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,18 @@ def current_instances_key
164164
end
165165

166166
def method_missing(name, ...)
167-
# Caches the method definition as a singleton method of the receiver.
168-
#
169-
# By letting #delegate handle it, we avoid an enclosure that'll capture args.
170-
singleton_class.delegate name, to: :instance
171-
172-
send(name, ...)
167+
instance.public_send(name, ...)
173168
end
174169

175170
def respond_to_missing?(name, _)
176-
super || instance.respond_to?(name)
171+
instance.respond_to?(name) || super
172+
end
173+
174+
def method_added(name)
175+
return if name == :initialize
176+
return unless public_method_defined?(name)
177+
return if respond_to?(name, true)
178+
singleton_class.delegate(name, to: :instance, as: self)
177179
end
178180
end
179181

0 commit comments

Comments
 (0)