Patchwork [3,of,4,V2] revset: add helper to build balanced addsets from chained 'or' operations

mail settings
Submitter Yuya Nishihara
Date May 27, 2015, 3:14 p.m.
Message ID <492997815f5da6c673bd.1432739664@mimosa>
Download mbox | patch
Permalink /patch/9296/
State Accepted
Headers show


Yuya Nishihara - May 27, 2015, 3:14 p.m.
# HG changeset patch
# User Yuya Nishihara <>
# Date 1432444252 -32400
#      Sun May 24 14:10:52 2015 +0900
# Node ID 492997815f5da6c673bddcb61d7a29346063386d
# Parent  a8e85b25edc7f73b28199eecfb92a8dee5507b73
revset: add helper to build balanced addsets from chained 'or' operations

This function will be used by revset.orset() and scmutil.revrange() to reduce
the stack depth from O(n) to O(log(n)).

We've bikeshed the interface of this function, but we couldn't come to an
agreement. So we decided to attempt to make it move forward.

 - new factory function isn't necessary for balanced addsets
 - addset.__init__ can just recurse, should handle "len(subsets) == 2+"

 - want to write all "len(subsets) == 0, 1, 2, 3+" cases in the same function
 - no recursion in __init__ for cosmetic reason: can't return, can't call
   __init__ directly

I've changed it to a private function so that nobody would be tempted to
utilize it.


diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -2944,6 +2944,20 @@  class filteredset(abstractsmartset):
     def __repr__(self):
         return '<%s %r>' % (type(self).__name__, self._subset)
+# this function will be removed, or merged to addset or orset, when
+# - scmutil.revrange() can be rewritten to not combine calculated smartsets
+# - or addset can handle more than two sets without balanced tree
+def _combinesets(subsets):
+    """Create balanced tree of addsets representing union of given sets"""
+    if not subsets:
+        return baseset()
+    if len(subsets) == 1:
+        return subsets[0]
+    p = len(subsets) // 2
+    xs = _combinesets(subsets[:p])
+    ys = _combinesets(subsets[p:])
+    return addset(xs, ys)
 def _iterordered(ascending, iter1, iter2):
     """produce an ordered iteration from two iterators with the same order