only show_vars when showing vars (#69365)

* only show_vars when showing vars

avoid processing function params that can be very expensive
and might not be used at all in called function.

fixes #69357

* Update changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml

Co-authored-by: Sloane Hertel <shertel@redhat.com>

Co-authored-by: Sloane Hertel <shertel@redhat.com>
This commit is contained in:
Brian Coca 2020-05-13 15:03:30 -04:00 committed by GitHub
parent 3b3c811be5
commit f0b6f76bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Avoid running subfunctions that are passed to show_vars function when it will be a noop.

View file

@ -240,9 +240,8 @@ class InventoryCLI(CLI):
@staticmethod
def _show_vars(dump, depth):
result = []
if context.CLIARGS['show_vars']:
for (name, val) in sorted(dump.items()):
result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth))
for (name, val) in sorted(dump.items()):
result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth))
return result
@staticmethod
@ -261,9 +260,11 @@ class InventoryCLI(CLI):
if group.name != 'all':
for host in sorted(group.hosts, key=attrgetter('name')):
result.append(self._graph_name(host.name, depth))
result.extend(self._show_vars(self._get_host_variables(host), depth + 1))
if context.CLIARGS['show_vars']:
result.extend(self._show_vars(self._get_host_variables(host), depth + 1))
result.extend(self._show_vars(self._get_group_variables(group), depth))
if context.CLIARGS['show_vars']:
result.extend(self._show_vars(self._get_group_variables(group), depth))
return result