[PATCH 4 of 5 V2] revset: add _optimizeand function

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

# HG changeset patch
# User Durham Goode <durham at 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):
         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.

More information about the Mercurial-devel mailing list