Patchwork [7,of,9] dagop: factor out generator of ancestor nodes

login
register
mail settings
Submitter Yuya Nishihara
Date June 25, 2017, 3:26 a.m.
Message ID <63b200dd478b6be7665d.1498361176@mimosa>
Download mbox | patch
Permalink /patch/21688/
State Accepted
Headers show

Comments

Yuya Nishihara - June 25, 2017, 3:26 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1498314651 -32400
#      Sat Jun 24 23:30:51 2017 +0900
# Node ID 63b200dd478b6be7665d02c40a7c7ed62e2df6d1
# Parent  9b603e7117c1718858eb2b6df2c410e74403c6cf
dagop: factor out generator of ancestor nodes

  # ancestors(tip) using hg repo
  1) 0.068976
  2) 0.068527

Patch

diff --git a/mercurial/dagop.py b/mercurial/dagop.py
--- a/mercurial/dagop.py
+++ b/mercurial/dagop.py
@@ -23,11 +23,14 @@  generatorset = smartset.generatorset
 # possible maximum depth between null and wdir()
 _maxlogdepth = 0x80000000
 
-def _genrevancestors(repo, revs, followfirst, startdepth, stopdepth):
-    if followfirst:
-        cut = 1
-    else:
-        cut = None
+def _walkrevtree(pfunc, revs, startdepth, stopdepth):
+    """Walk DAG using 'pfunc' from the given 'revs' nodes
+
+    'pfunc(rev)' should return the parent revisions of the given 'rev'.
+
+    Scan ends at the stopdepth (exlusive) if specified. Revisions found
+    earlier than the startdepth are omitted.
+    """
     if startdepth is None:
         startdepth = 0
     if stopdepth is None:
@@ -37,13 +40,6 @@  def _genrevancestors(repo, revs, followf
     if stopdepth < 0:
         raise error.ProgrammingError('negative stopdepth')
 
-    cl = repo.changelog
-    def pfunc(rev):
-        try:
-            return cl.parentrevs(rev)[:cut]
-        except error.WdirUnsupported:
-            return (pctx.rev() for pctx in repo[rev].parents()[:cut])
-
     # load input revs lazily to heap so earlier revisions can be yielded
     # without fully computing the input revs
     revs.sort(reverse=True)
@@ -74,6 +70,19 @@  def _genrevancestors(repo, revs, followf
                 if prev != node.nullrev:
                     heapq.heappush(pendingheap, (-prev, pdepth))
 
+def _genrevancestors(repo, revs, followfirst, startdepth, stopdepth):
+    if followfirst:
+        cut = 1
+    else:
+        cut = None
+    cl = repo.changelog
+    def pfunc(rev):
+        try:
+            return cl.parentrevs(rev)[:cut]
+        except error.WdirUnsupported:
+            return (pctx.rev() for pctx in repo[rev].parents()[:cut])
+    return _walkrevtree(pfunc, revs, startdepth, stopdepth)
+
 def revancestors(repo, revs, followfirst, startdepth=None, stopdepth=None):
     """Like revlog.ancestors(), but supports additional options, includes
     the given revs themselves, and returns a smartset