ancestors: prefetch method outside of the loop
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 24 Jun 2015 12:37:55 -0500
changeset 25639 7125225a5287
parent 25635 7fdd1782fc4e
child 25640 39f0064a3079
ancestors: prefetch method outside of the loop 15412bba5a68 is yet another example where this is worthwhile when it comes to performance, we blindly do it for all 'lazyancestors' methods.
mercurial/ancestor.py
--- a/mercurial/ancestor.py	Sun Sep 28 20:18:43 2014 -0700
+++ b/mercurial/ancestor.py	Wed Jun 24 12:37:55 2015 -0500
@@ -316,11 +316,14 @@
         stoprev = self._stoprev
         visit = collections.deque(revs)
 
+        see = seen.add
+        schedule = visit.append
+
         while visit:
             for parent in parentrevs(visit.popleft()):
                 if parent >= stoprev and parent not in seen:
-                    visit.append(parent)
-                    seen.add(parent)
+                    schedule(parent)
+                    see(parent)
                     yield parent
 
     def __contains__(self, target):
@@ -337,6 +340,7 @@
         stoprev = self._stoprev
         heappop = heapq.heappop
         heappush = heapq.heappush
+        see = seen.add
 
         targetseen = False
 
@@ -347,7 +351,7 @@
                 # We need to make sure we push all parents into the heap so
                 # that we leave it in a consistent state for future calls.
                 heappush(visit, -parent)
-                seen.add(parent)
+                see(parent)
                 if parent == target:
                     targetseen = True