[PATCH 2 of 5 V2] revset: add _getlengthhint function

Durham Goode durham at fb.com
Tue Sep 30 13:02:51 CDT 2014

# HG changeset patch
# User Durham Goode <durham at fb.com>
# Date 1412097821 25200
#      Tue Sep 30 10:23:41 2014 -0700
# Node ID f224c6c8c7a1f0fa5a3151b1cb045367f99da28e
# Parent  c6129c781a9f11be26191e64dcebf8d539c8c76a
revset: add _getlengthhint function

Adds a function that tries to get a length hint from the set when possible. If
not possible, it returns None. This will be used later to optimize revsets such
as 'X & Y' to iterate over the smaller of the two sets.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -16,6 +16,7 @@ import encoding
 import obsolete as obsmod
 import pathutil
 import repoview
+import util
 def _revancestors(repo, revs, followfirst):
     """Like revlog.ancestors(), but supports followfirst."""
@@ -2194,6 +2195,17 @@ def funcsused(tree):
         return funcs
+def _getlengthhint(revs):
+    """Returns the length hint for the given set. If no length hint is avaiable,
+    it returns None.
+    """
+    if util.safehasattr(revs, '__length_hint__'):
+        return revs.__length_hint__()
+    elif util.safehasattr(revs, '__len__'):
+        return len(revs)
+    else:
+        return None
 class baseset(list):
     """Basic data structure that represents a revset and contains the basic
     operation that it should be able to perform.

More information about the Mercurial-devel mailing list