rank: compute property incrementally
authorpacien <pacien.trangirard@pacien.net>
Fri, 28 Jan 2022 11:54:44 +0100
changeset 48765 580660518459
parent 48764 f7d7facd7b9f
child 48766 d9a7131648a3
rank: compute property incrementally This replaces the naive rank computation with a more efficient incremental method, avoiding computing the whole ancestor set when possible. Differential Revision: https://phab.mercurial-scm.org/D12143
mercurial/revlog.py
--- a/mercurial/revlog.py	Fri Jan 28 11:45:33 2022 +0100
+++ b/mercurial/revlog.py	Fri Jan 28 11:54:44 2022 +0100
@@ -2464,7 +2464,16 @@
 
         rank = RANK_UNKNOWN
         if self._format_version == CHANGELOGV2:
-            rank = len(list(self.ancestors([p1r, p2r], inclusive=True))) + 1
+            if (p1r, p2r) == (nullrev, nullrev):
+                rank = 1
+            elif p1r != nullrev and p2r == nullrev:
+                rank = 1 + self.fast_rank(p1r)
+            elif p1r == nullrev and p2r != nullrev:
+                rank = 1 + self.fast_rank(p2r)
+            else:  # merge node
+                pmin, pmax = sorted((p1r, p2r))
+                rank = 1 + self.fast_rank(pmax)
+                rank += sum(1 for _ in self.findmissingrevs([pmax], [pmin]))
 
         e = revlogutils.entry(
             flags=flags,