Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: pylib/gyp/input.py

Issue 234813003: gyp: fix O(n^2) in dependency calculations. (Closed) Base URL: https://chromium.googlesource.com/external/gyp.git@master
Patch Set: flat_set: Now with comment Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 Google Inc. All rights reserved. 1 # Copyright (c) 2012 Google Inc. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from compiler.ast import Const 5 from compiler.ast import Const
6 from compiler.ast import Dict 6 from compiler.ast import Dict
7 from compiler.ast import Discard 7 from compiler.ast import Discard
8 from compiler.ast import List 8 from compiler.ast import List
9 from compiler.ast import Module 9 from compiler.ast import Module
10 from compiler.ast import Node 10 from compiler.ast import Node
(...skipping 1462 matching lines...) Expand 10 before | Expand all | Expand 10 after
1473 1473
1474 def __repr__(self): 1474 def __repr__(self):
1475 return '<DependencyGraphNode: %r>' % self.ref 1475 return '<DependencyGraphNode: %r>' % self.ref
1476 1476
1477 def FlattenToList(self): 1477 def FlattenToList(self):
1478 # flat_list is the sorted list of dependencies - actually, the list items 1478 # flat_list is the sorted list of dependencies - actually, the list items
1479 # are the "ref" attributes of DependencyGraphNodes. Every target will 1479 # are the "ref" attributes of DependencyGraphNodes. Every target will
1480 # appear in flat_list after all of its dependencies, and before all of its 1480 # appear in flat_list after all of its dependencies, and before all of its
1481 # dependents. 1481 # dependents.
1482 flat_list = [] 1482 flat_list = []
1483 # flat_set is to make "is in" checks of flat_list. At all times
1484 # set(flat_list) == flat_set.
1485 flat_set = set()
1483 1486
1484 # in_degree_zeros is the list of DependencyGraphNodes that have no 1487 # in_degree_zeros is the list of DependencyGraphNodes that have no
1485 # dependencies not in flat_list. Initially, it is a copy of the children 1488 # dependencies not in flat_list. Initially, it is a copy of the children
1486 # of this node, because when the graph was built, nodes with no 1489 # of this node, because when the graph was built, nodes with no
1487 # dependencies were made implicit dependents of the root node. 1490 # dependencies were made implicit dependents of the root node.
1488 in_degree_zeros = set(self.dependents[:]) 1491 in_degree_zeros = set(self.dependents[:])
1489 1492
1490 while in_degree_zeros: 1493 while in_degree_zeros:
1491 # Nodes in in_degree_zeros have no dependencies not in flat_list, so they 1494 # Nodes in in_degree_zeros have no dependencies not in flat_list, so they
1492 # can be appended to flat_list. Take these nodes out of in_degree_zeros 1495 # can be appended to flat_list. Take these nodes out of in_degree_zeros
1493 # as work progresses, so that the next node to process from the list can 1496 # as work progresses, so that the next node to process from the list can
1494 # always be accessed at a consistent position. 1497 # always be accessed at a consistent position.
1495 node = in_degree_zeros.pop() 1498 node = in_degree_zeros.pop()
1496 flat_list.append(node.ref) 1499 flat_list.append(node.ref)
1500 flat_set.add(node.ref)
1497 1501
1498 # Look at dependents of the node just added to flat_list. Some of them 1502 # Look at dependents of the node just added to flat_list. Some of them
1499 # may now belong in in_degree_zeros. 1503 # may now belong in in_degree_zeros.
1500 for node_dependent in node.dependents: 1504 for node_dependent in node.dependents:
1501 is_in_degree_zero = True 1505 is_in_degree_zero = True
1506 # TODO: We want to check through the
1507 # node_dependent.dependencies list but if it's long and we
1508 # always start at the beginning, then we get O(n^2) behaviour.
1502 for node_dependent_dependency in node_dependent.dependencies: 1509 for node_dependent_dependency in node_dependent.dependencies:
1503 if not node_dependent_dependency.ref in flat_list: 1510 if not node_dependent_dependency.ref in flat_set:
1504 # The dependent one or more dependencies not in flat_list. There 1511 # The dependent one or more dependencies not in flat_list. There
1505 # will be more chances to add it to flat_list when examining 1512 # will be more chances to add it to flat_list when examining
1506 # it again as a dependent of those other dependencies, provided 1513 # it again as a dependent of those other dependencies, provided
1507 # that there are no cycles. 1514 # that there are no cycles.
1508 is_in_degree_zero = False 1515 is_in_degree_zero = False
1509 break 1516 break
1510 1517
1511 if is_in_degree_zero: 1518 if is_in_degree_zero:
1512 # All of the dependent's dependencies are already in flat_list. Add 1519 # All of the dependent's dependencies are already in flat_list. Add
1513 # it to in_degree_zeros where it will be processed in a future 1520 # it to in_degree_zeros where it will be processed in a future
(...skipping 1301 matching lines...) Expand 10 before | Expand all | Expand 10 after
2815 ValidateRunAsInTarget(target, target_dict, build_file) 2822 ValidateRunAsInTarget(target, target_dict, build_file)
2816 ValidateActionsInTarget(target, target_dict, build_file) 2823 ValidateActionsInTarget(target, target_dict, build_file)
2817 2824
2818 # Generators might not expect ints. Turn them into strs. 2825 # Generators might not expect ints. Turn them into strs.
2819 TurnIntIntoStrInDict(data) 2826 TurnIntIntoStrInDict(data)
2820 2827
2821 # TODO(mark): Return |data| for now because the generator needs a list of 2828 # TODO(mark): Return |data| for now because the generator needs a list of
2822 # build files that came in. In the future, maybe it should just accept 2829 # build files that came in. In the future, maybe it should just accept
2823 # a list, and not the whole data dict. 2830 # a list, and not the whole data dict.
2824 return [flat_list, targets, data] 2831 return [flat_list, targets, data]
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698