avoid creating lots of dictionaries in traverse_edges()

- This is an optimization to the way traverse_edges iterates over
  successors.

- Previous version called dependencies_dict(), which involved a lot of
  redundant work (creating dicts and calling caonical_deptype)
This commit is contained in:
Todd Gamblin 2017-10-13 16:49:51 -07:00
parent 9ccaf6474d
commit f58c503091

View File

@ -1337,27 +1337,26 @@ def return_val(dspec):
# Edge traversal yields but skips children of visited nodes # Edge traversal yields but skips children of visited nodes
if not (key in visited and cover == 'edges'): if not (key in visited and cover == 'edges'):
visited.add(key)
# This code determines direction and yields the children/parents # This code determines direction and yields the children/parents
if direction == 'children': if direction == 'children':
successors = self.dependencies_dict(deptype) where = self._dependencies
succ = lambda s: s.spec succ = lambda dspec: dspec.spec
elif direction == 'parents': elif direction == 'parents':
successors = self.dependents_dict(deptype) where = self._dependents
succ = lambda s: s.parent succ = lambda dspec: dspec.parent
else: else:
raise ValueError('Invalid traversal direction: %s' % direction) raise ValueError('Invalid traversal direction: %s' % direction)
visited.add(key) for name, dspec in sorted(where.items()):
for name, dspec in sorted(successors.items()): dt = dspec.deptypes
child = successors[name] if dt and not any(d in deptype for d in dt):
children = succ(child).traverse_edges( continue
visited,
d=(d + 1), for child in succ(dspec).traverse_edges(
deptype=deptype, visited, d + 1, deptype, dspec, **kwargs):
dep_spec=dspec, yield child
**kwargs)
for elt in children:
yield elt
# Postorder traversal yields after successors # Postorder traversal yields after successors
if yield_me and order == 'post': if yield_me and order == 'post':