Skip to content

Commit

Permalink
Decrease string allocations on AR#respond_to?
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
schneems committed Jul 30, 2015
1 parent 10e994c commit f80aa59
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion activerecord/lib/active_record/attribute_methods.rb
Expand Up @@ -230,7 +230,15 @@ def column_for_attribute(name)
# person.respond_to(:nothing) # => false
def respond_to?(name, include_private = false)
return false unless super
name = name.to_s

case name
when :to_partial_path
name = "to_partial_path".freeze
when :to_model
name = "to_model".freeze
else
name = name.to_s
end

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

0 comments on commit f80aa59

Please sign in to comment.