From 1bc8c3f8ec2e5f59333cf7a59b4b6b25e9763eb5 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 14 May 2018 11:56:31 -0500 Subject: [PATCH] Add uniq on datacenters in #host_to_folder The more stripped down version of this method, prior to the change, is as follows: def host_to_folder(src) sources = src[:host].nil? ? allowed_hosts_obj : [src[:host]] datacenters = sources.collect { |h| find_datacenter_for_ci(h) end.compact datacenters.each_with_object({}) do |dc, folders| folders.merge!(get_ems_folders(dc)) end end The `find_datacenter_for_ci` method basically looks up the datacenter for the matching host object in the xml tree that has been generated. In most (all) cases though, their will be more than one host per datacenter, and that wasn't taken into account for this method since the next block blindly takes those datacenters and merges the folder structure it gets from `get_ems_folders` for each datacenter. By adding a `.uniq` prior to the assignment of datacenters, this saves a significant number of unnecessary CPU cycles and object allocations to fetch the same data and apply it to the `folders` variable, without changing the end result. Note: Ideally we would want a better algorithm for finding the datacenters for a collection of hosts, one that allowed for ejecting early from find_datacenter_for_ci when we knew an existing datacenter for a given host had already been found. But the time taken to get the datacenters from the hosts is far less in CPU time than the time taken to call `#get_ems_folders` per datacenter object and merge the hash with the existing results. --- app/models/miq_request_workflow.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/miq_request_workflow.rb b/app/models/miq_request_workflow.rb index f6207b56db2..e1c21298b84 100644 --- a/app/models/miq_request_workflow.rb +++ b/app/models/miq_request_workflow.rb @@ -1195,7 +1195,7 @@ def host_to_folder(src) result = find_datacenter_for_ci(h) rails_logger("host_to_folder for host #{h.name}", 1) result - end.compact + end.compact.uniq datacenters.each_with_object({}) do |dc, folders| rails_logger("host_to_folder for dc #{dc.name}", 0) folders.merge!(get_ems_folders(dc))