Patchwork [4,of,5,V2] revset: add _optimizeand function

login
register
mail settings
Submitter Durham Goode
Date Sept. 30, 2014, 6:02 p.m.
Message ID <afa88d660f03f3e19a4f.1412100173@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/6045/
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 1412036832 25200
#      Mon Sep 29 17:27:12 2014 -0700
# Node ID afa88d660f03f3e19a4f9c1cba2442446a4db733
# Parent  7d4c0bc4debf9efcc673c24e880d3d831bd06ef9
revset: add _optimizeand function

Adds an _optimizeand function that takes two sets (the iterate set, and the
check set) and reverses them if the iterate set is bigger than the check set.
Pierre-Yves David - Oct. 1, 2014, 8:25 p.m.
On 09/30/2014 01:02 PM, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1412036832 25200
> #      Mon Sep 29 17:27:12 2014 -0700
> # Node ID afa88d660f03f3e19a4f9c1cba2442446a4db733
> # Parent  7d4c0bc4debf9efcc673c24e880d3d831bd06ef9
> revset: add _optimizeand function
>
> Adds an _optimizeand function that takes two sets (the iterate set, and the
> check set) and reverses them if the iterate set is bigger than the check set.
>
> diff --git a/mercurial/revset.py b/mercurial/revset.py
> --- a/mercurial/revset.py
> +++ b/mercurial/revset.py
> @@ -2206,6 +2206,22 @@ def _getlengthhint(revs):
>       else:
>           return None
>
> +def _optimizeand(iterate, check):
> +    """Return the given sets in the optimal order for iterating and checking
> +    containment. The iterate set is the first return arg, the check set is the
> +    second.
> +    """
> +    iteratelen = _getlengthhint(iterate)
> +    checklen = _getlengthhint(check)
> +    if iteratelen is not None and checklen is not None:
> +        # Don't reorder if the `check` is a set. Many revsets assume the result
> +        # will be a smart class, and if we reorder such that there is a set on
> +        # the left of a X & Y expression, the result is a set, which breaks
> +        # things.
> +        if iteratelen > checklen and not isinstance(check, set):

isinstance set if not going to be enought. There is plenty of similar 
smartset class that are actually not smartclass (hopefully their will be 
all smartclass when I'll be done refactoring for the order stuff). you 
have to check for smartclass specific attribute like "isascending".

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2206,6 +2206,22 @@  def _getlengthhint(revs):
     else:
         return None
 
+def _optimizeand(iterate, check):
+    """Return the given sets in the optimal order for iterating and checking
+    containment. The iterate set is the first return arg, the check set is the
+    second.
+    """
+    iteratelen = _getlengthhint(iterate)
+    checklen = _getlengthhint(check)
+    if iteratelen is not None and checklen is not None:
+        # Don't reorder if the `check` is a set. Many revsets assume the result
+        # will be a smart class, and if we reorder such that there is a set on
+        # the left of a X & Y expression, the result is a set, which breaks
+        # things.
+        if iteratelen > checklen and not isinstance(check, set):
+            return check, iterate
+    return iterate, check
+
 class baseset(list):
     """Basic data structure that represents a revset and contains the basic
     operation that it should be able to perform.