From a6dc75ee3d0eb686d8c3dab9c37b8a4a4865a6d9 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 4 May 2018 10:40:36 -0400 Subject: [PATCH] Merge pull request #17325 from NickLaMuro/flatten_arranged_rels_optimization Remove N+1 obj creation in flatten_arranged_rels (cherry picked from commit 32fadeb91c0e63e34efa4c9321ec52a6bdb176ae) https://bugzilla.redhat.com/show_bug.cgi?id=1593798 --- app/models/relationship.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 062e410073b..6a9b8f0817d 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -76,10 +76,15 @@ def resource_pair # def self.flatten_arranged_rels(relationships) - relationships.each_with_object([]) do |(rel, children), a| - a << rel - a.concat(flatten_arranged_rels(children)) + result = relationships.keys + remaining_children = relationships.values + until remaining_children.empty? + remaining_children.pop.each do |rel, kids| + result << rel + remaining_children << kids + end end + result end def self.arranged_rels_to_resources(relationships, initial = true)