Commit ff4119ad by Serge van Ginderachter Committed by James Cammarata

Performance optimization in resolving host patterns

Avoid resolving a pattern that is a plain host. When matching a hostname in the
hosts_cache, just use the host object from there.

When running a task on say 750 hosts, this yields a huge improvement.
parent 539426f6
...@@ -213,6 +213,10 @@ class Inventory(object): ...@@ -213,6 +213,10 @@ class Inventory(object):
hosts = [] hosts = []
for p in patterns: for p in patterns:
# avoid resolving a pattern that is a plain host
if p in self._hosts_cache:
hosts.append(self.get_host(p))
else:
that = self.__get_hosts(p) that = self.__get_hosts(p)
if p.startswith("!"): if p.startswith("!"):
hosts = [ h for h in hosts if h not in that ] hosts = [ h for h in hosts if h not in that ]
...@@ -221,7 +225,6 @@ class Inventory(object): ...@@ -221,7 +225,6 @@ class Inventory(object):
else: else:
to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ] to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ]
hosts.extend(to_append) hosts.extend(to_append)
return hosts return hosts
def __get_hosts(self, pattern): def __get_hosts(self, pattern):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment