Skip to content

Commit

Permalink
ActiveRecord#respond_to? No longer allocates strings
Browse files Browse the repository at this point in the history
This is an alternative to #34195

The active record `respond_to?` method needs to do two things if `super` does not say that the method exists. It has to see if the "name" being passed in represents a column in the table. If it does then it needs to pass it to `has_attribute?` to see if the key exists in the current object. The reason why this is slow is that `has_attribute?` needs a string and most (almost all) objects passed in are symbols.

The only time we need to allocate a string in this method is if the column does exist in the database, and since these are a limited number of strings (since column names are a finite set) then we can pre-generate all of them and use the same string. 

We generate a list hash of column names and convert them to symbols, and store the value as the string name. This allows us to both check if the "name" exists as a column, but also provides us with a string object we can use for the `has_attribute?` call. 

I then ran the test suite and found there was only one case where we're intentionally passing in a string and changed it to a symbol. (However there are tests where we are using a string key, but they don't ship with rails).

As re-written this method should never allocate unless the user passes in a string key, which is fairly uncommon with `respond_to?`.

This also eliminates the need to special case every common item that might come through the method via the `case` that was originally added in f80aa59 (by me) and then with an attempt to extend in #34195.

As a bonus this reduces 6,300 comparisons (in the CodeTriage app homepage) to 450 as we also no longer need to loop through the column array to check for an `include?`.
  • Loading branch information
schneems committed Oct 15, 2018
1 parent 134dab4 commit f45267b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
15 changes: 4 additions & 11 deletions activerecord/lib/active_record/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,14 @@ def column_for_attribute(name)
def respond_to?(name, include_private = false)
return false unless super

case name
when :to_partial_path
name = "to_partial_path"
when :to_model
name = "to_model"
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.
# We check defined?(@attributes) not to issue warnings if called on objects that
# have been allocated but not yet initialized.
if defined?(@attributes) && self.class.column_names.include?(name)
return has_attribute?(name)
if defined?(@attributes)
if name = self.class.symbol_column_to_string(name.to_sym)
return has_attribute?(name)
end
end

true
Expand Down
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/model_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ def column_names
@column_names ||= columns.map(&:name)
end

def symbol_column_to_string(name_symbol) # :nodoc:
@symbol_column_to_string_name_hash ||= column_names.index_by(&:to_sym)
@symbol_column_to_string_name_hash[name_symbol]
end

# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
# and columns used for single table inheritance have been removed.
def content_columns
Expand Down Expand Up @@ -477,6 +482,7 @@ def load_schema!
def reload_schema_from_cache
@arel_table = nil
@column_names = nil
@symbol_column_to_string_name_hash = nil
@attribute_types = nil
@content_columns = nil
@default_attributes = nil
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def assign_nested_attributes_for_one_to_one_association(association_name, attrib
existing_record.assign_attributes(assignable_attributes)
association(association_name).initialize_attributes(existing_record)
else
method = "build_#{association_name}"
method = :"build_#{association_name}"
if respond_to?(method)
send(method, assignable_attributes)
else
Expand Down

0 comments on commit f45267b

Please sign in to comment.