Patchwork [3,of,6] ancestor: unroll loop of parents in _lazyancestorsiter()

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 11, 2018, 11:02 p.m.
Message ID <acc190466e6d1e6db9bd.1536706928@mimosa>
Download mbox | patch
Permalink /patch/34505/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 11, 2018, 11:02 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1536584080 -32400
#      Mon Sep 10 21:54:40 2018 +0900
# Node ID acc190466e6d1e6db9bd99024e360b68c3c8d8cb
# Parent  dd6a0955ec0081c6133ce8cf42d38914a8e305ab
ancestor: unroll loop of parents in _lazyancestorsiter()

This change itself isn't major performance win, but it helps optimizing
the visit loop for contiguous chains. See the next patch.

Patch

diff --git a/mercurial/ancestor.py b/mercurial/ancestor.py
--- a/mercurial/ancestor.py
+++ b/mercurial/ancestor.py
@@ -274,20 +274,26 @@  def _lazyancestorsiter(parentrevs, initr
         visit = []
         heapq.heapify(visit)
         for r in initrevs:
-            for parent in parentrevs(r):
-                if parent not in seen:
-                    schedule(visit, -parent)
-                    see(parent)
+            p1, p2 = parentrevs(r)
+            if p1 not in seen:
+                schedule(visit, -p1)
+                see(p1)
+            if p2 not in seen:
+                schedule(visit, -p2)
+                see(p2)
 
     while visit:
         current = -nextitem(visit)
         if current < stoprev:
             break
         yield current
-        for parent in parentrevs(current):
-            if parent not in seen:
-                schedule(visit, -parent)
-                see(parent)
+        p1, p2 = parentrevs(current)
+        if p1 not in seen:
+            schedule(visit, -p1)
+            see(p1)
+        if p2 not in seen:
+            schedule(visit, -p2)
+            see(p2)
 
 class lazyancestors(object):
     def __init__(self, pfunc, revs, stoprev=0, inclusive=False):
diff --git a/tests/test-ancestor.py b/tests/test-ancestor.py
--- a/tests/test-ancestor.py
+++ b/tests/test-ancestor.py
@@ -178,9 +178,9 @@  def test_missingancestors(seed, rng):
 # |
 # o  0
 
-graph = {0: [-1], 1: [0], 2: [1], 3: [1], 4: [2], 5: [4], 6: [4],
-         7: [4], 8: [-1], 9: [6, 7], 10: [5], 11: [3, 7], 12: [9],
-         13: [8]}
+graph = {0: [-1, -1], 1: [0, -1], 2: [1, -1], 3: [1, -1], 4: [2, -1],
+         5: [4, -1], 6: [4, -1], 7: [4, -1], 8: [-1, -1], 9: [6, 7],
+         10: [5, -1], 11: [3, 7], 12: [9, -1], 13: [8, -1]}
 
 def genlazyancestors(revs, stoprev=0, inclusive=False):
     print(("%% lazy ancestor set for %s, stoprev = %s, inclusive = %s" %