Patchwork [2,of,5,V2] revset: add _getlengthhint function

login
register
mail settings
Submitter Durham Goode
Date Sept. 30, 2014, 6:02 p.m.
Message ID <f224c6c8c7a1f0fa5a31.1412100171@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/6046/
State Changes Requested
Headers show

Comments

Durham Goode - Sept. 30, 2014, 6:02 p.m.
# HG changeset patch
# User Durham Goode <durham@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.
Pierre-Yves David - Oct. 1, 2014, 8:22 p.m.
On 09/30/2014 01:02 PM, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@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):
>               funcs.add(tree[1][1])
>           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)

I'm not sure it is a good idea to use __len__ here, multiple smartset 
have a super expensive __len_ implementation right now.

> +    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.
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
>

Patch

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):
             funcs.add(tree[1][1])
         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.