Cleanup: Don't search stack for cycle elements twice.
authorNico Weber <nicolasweber@gmx.de>
Wed, 1 Apr 2015 15:00:27 +0000 (08:00 -0700)
committerNico Weber <nicolasweber@gmx.de>
Wed, 1 Apr 2015 15:00:27 +0000 (08:00 -0700)
The common case is that there is no cycle.  In that case,
CheckDependencyCycle() searched the stack for a dupe from the back,
wouldn't find one, and return false.

If there was a cycle, it would then search again from the front
(probably because the push_back() that used to be here would invalidate
the ri iterator).

Since the push_back() is gone, just search from the front once. No
intended behavior change.

src/build.cc

index c51ce53..9f40d2d 100644 (file)
@@ -318,12 +318,10 @@ bool Plan::AddSubTarget(Node* node, vector<Node*>* stack, string* err) {
 
 bool Plan::CheckDependencyCycle(Node* node, const vector<Node*>& stack,
                                 string* err) {
-  vector<Node*>::const_reverse_iterator ri =
-      find(stack.rbegin(), stack.rend(), node);
-  if (ri == stack.rend())
+  vector<Node*>::const_iterator start = find(stack.begin(), stack.end(), node);
+  if (start == stack.end())
     return false;
 
-  vector<Node*>::const_iterator start = find(stack.begin(), stack.end(), node);
   *err = "dependency cycle: ";
   for (vector<Node*>::const_iterator i = start; i != stack.end(); ++i) {
     err->append((*i)->path());