From ee2117aedf921251040565fe832e0aee3e9d95aa Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Fri, 8 Jun 2018 15:31:56 +0200 Subject: [PATCH] optimize accessing ancestors and descendants when scheduling --- .../work_packages/schedule_dependency.rb | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rb index bd3433d105..775f391f5a 100644 --- a/app/services/work_packages/schedule_dependency.rb +++ b/app/services/work_packages/schedule_dependency.rb @@ -58,7 +58,9 @@ class WorkPackages::ScheduleDependency attr_accessor :work_packages, :dependencies, - :known_work_packages + :known_work_packages, + :known_work_packages_by_id, + :known_work_packages_by_parent_id private @@ -69,8 +71,13 @@ class WorkPackages::ScheduleDependency def load_all_following(work_packages) following = load_following(work_packages) + # Those variables are pure optimizations. + # We want to reuse the already loaded work packages as much as possible + # and we want to have them readily available as hashes. self.known_work_packages += following known_work_packages.uniq! + self.known_work_packages_by_id = known_work_packages.group_by(&:id) + self.known_work_packages_by_parent_id = known_work_packages.group_by(&:parent_id) new_dependencies = add_dependencies(following) @@ -184,23 +191,23 @@ class WorkPackages::ScheduleDependency def ancestors_from_preloaded(work_package) if work_package.parent_id - parent = known_work_packages.detect { |c| work_package.parent_id == c.id } + parent = known_work_packages_by_id[work_package.parent_id] if parent - [parent] + ancestors_from_preloaded(parent) + parent + ancestors_from_preloaded(parent.first) end end || [] end def descendants_from_preloaded(work_package) - children = known_work_packages.select { |c| c.parent_id == work_package.id } + children = known_work_packages_by_parent_id[work_package.id] || [] children + children.map { |child| descendants_from_preloaded(child) }.flatten end - def known_work_packages - schedule_dependency.known_work_packages - end + delegate :known_work_packages, + :known_work_packages_by_id, + :known_work_packages_by_parent_id, to: :schedule_dependency def scheduled_work_packages schedule_dependency.work_packages + schedule_dependency.dependencies.keys