Skip to content

Commit f80aa59

Browse files
committed
Decrease string allocations on AR#respond_to?
When a symbol is passed in, we call `to_s` on it which allocates a string. The two hardcoded symbols that are used internally are `:to_partial_path` and `:to_model`. This change buys us 71,136 bytes of memory and 1,777 fewer objects per request.
1 parent 10e994c commit f80aa59

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,15 @@ def column_for_attribute(name)
230230
# person.respond_to(:nothing) # => false
231231
def respond_to?(name, include_private = false)
232232
return false unless super
233-
name = name.to_s
233+
234+
case name
235+
when :to_partial_path
236+
name = "to_partial_path".freeze
237+
when :to_model
238+
name = "to_model".freeze
239+
else
240+
name = name.to_s
241+
end
234242

235243
# If the result is true then check for the select case.
236244
# For queries selecting a subset of columns, return false for unselected columns.

0 commit comments

Comments
 (0)