diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-08 18:02:06 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-08 18:02:06 -0700 |
commit | 155fd955ac380c7877785f1b74b61ad86fd40772 (patch) | |
tree | 72fdaba51003edd203a389a74e09963224d04631 /activerecord | |
parent | 86640e1ddca949913360247f473dc917484f6f9c (diff) | |
download | rails-155fd955ac380c7877785f1b74b61ad86fd40772.tar.gz rails-155fd955ac380c7877785f1b74b61ad86fd40772.tar.bz2 rails-155fd955ac380c7877785f1b74b61ad86fd40772.zip |
reduce number of comparisons and array allocations
Just inspecting the children nodes will reduce the number of comparisons
we have to do (since it is limited to the child size)
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency.rb | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index d11a9db1f5..bfaac87e49 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -137,7 +137,7 @@ module ActiveRecord records = result_set.map { |row_hash| primary_id = type_caster.type_cast row_hash[primary_key] parent = parents[primary_id] ||= join_base.instantiate(row_hash) - construct(parent, assoc, join_associations, row_hash, result_set) + construct(parent, assoc, row_hash, result_set) parent }.uniq @@ -232,27 +232,26 @@ module ActiveRecord Node.new part end - def construct(parent, nodes, join_parts, row, rs) + def construct(parent, nodes, row, rs) nodes.sort_by { |k| k.name.to_s }.each do |node| association_name = node.name assoc = node.children - association = construct_scalar(parent, association_name, join_parts, row, rs) - construct(association, assoc, join_parts, row, rs) if association + association = construct_scalar(parent, association_name, row, rs, nodes) + construct(association, assoc, row, rs) if association end end - def construct_scalar(parent, associations, join_parts, row, rs) + def construct_scalar(parent, associations, row, rs, nodes) name = associations.to_s - join_part = join_parts.detect { |j| - j.reflection.name.to_s == name && - j.parent_table_name == parent.class.table_name + node = nodes.detect { |j| + j.name.to_s == name && + j.join_part.parent_table_name == parent.class.table_name } - raise(ConfigurationError, "No such association") unless join_part + raise(ConfigurationError, "No such association") unless node - join_parts.delete(join_part) - construct_association(parent, join_part, row, rs) + construct_association(parent, node.join_part, row, rs) end def construct_association(record, join_part, row, rs) |