From b00bf02129c70ce99f8aaf09d3d46c09b2668712 Mon Sep 17 00:00:00 2001 From: Stoned Elipot Date: Mon, 18 Nov 2013 03:48:55 +0100 Subject: [PATCH] Improve Group.get_hosts() performance. - reduce hosts group list to unique elements faster - add a cache of already computed hosts group list --- lib/ansible/inventory/group.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/ansible/inventory/group.py b/lib/ansible/inventory/group.py index a4f30957155..6fc812dc0c1 100644 --- a/lib/ansible/inventory/group.py +++ b/lib/ansible/inventory/group.py @@ -18,7 +18,7 @@ class Group(object): ''' a group of ansible hosts ''' - __slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth' ] + __slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth', '_hosts_cache' ] def __init__(self, name=None): @@ -28,6 +28,7 @@ class Group(object): self.vars = {} self.child_groups = [] self.parent_groups = [] + self.clear_hosts_cache() if self.name is None: raise Exception("group name is required") @@ -41,26 +42,44 @@ class Group(object): self.child_groups.append(group) group.depth = max([self.depth+1, group.depth]) group.parent_groups.append(self) + self.clear_hosts_cache() def add_host(self, host): self.hosts.append(host) host.add_group(self) + self.clear_hosts_cache() def set_variable(self, key, value): self.vars[key] = value + def clear_hosts_cache(self): + + self._hosts_cache = None + for g in self.parent_groups: + g.clear_hosts_cache() + def get_hosts(self): + if self._hosts_cache is None: + self._hosts_cache = self._get_hosts() + + return self._hosts_cache + + def _get_hosts(self): + hosts = [] + seen = {} for kid in self.child_groups: kid_hosts = kid.get_hosts() for kk in kid_hosts: - if kk not in hosts: + if kk not in seen: + seen[kk] = 1 hosts.append(kk) for mine in self.hosts: - if mine not in hosts: + if mine not in seen: + seen[mine] = 1 hosts.append(mine) return hosts