Skip to content
Browse files

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)
  • Loading branch information...
1 parent 86640e1 commit 155fd955ac380c7877785f1b74b61ad86fd40772 @tenderlove tenderlove committed
Showing with 10 additions and 11 deletions.
  1. +10 −11 activerecord/lib/active_record/associations/join_dependency.rb
View
21 activerecord/lib/active_record/associations/join_dependency.rb
@@ -137,7 +137,7 @@ def instantiate(result_set)
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 @@ def build_join_association(reflection, parent, join_type)
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)

0 comments on commit 155fd95

Please sign in to comment.
Something went wrong with that request. Please try again.