revlog: add fast path to ancestor
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Thu, 03 Dec 2009 01:01:49 +0100
changeset 9991 a7d11deb47dd
parent 9990 c1d940d31aea
child 9992 e97dd3a8e8d7
revlog: add fast path to ancestor
mercurial/revlog.py
--- a/mercurial/revlog.py	Tue Dec 01 16:19:53 2009 -0500
+++ b/mercurial/revlog.py	Thu Dec 03 01:01:49 2009 +0100
@@ -1118,10 +1118,19 @@
     def ancestor(self, a, b):
         """calculate the least common ancestor of nodes a and b"""
 
+        # fast path, check if it is a descendant
+        a, b = self.rev(a), self.rev(b)
+        start, end = sorted((a, b))
+        for i in self.descendants(start):
+            if i == end:
+                return self.node(start)
+            elif i > end:
+                break
+
         def parents(rev):
             return [p for p in self.parentrevs(rev) if p != nullrev]
 
-        c = ancestor.ancestor(self.rev(a), self.rev(b), parents)
+        c = ancestor.ancestor(a, b, parents)
         if c is None:
             return nullid