Patchwork [6,of,6,V3] revset: optimize out sort() according to ordering policy

login
register
mail settings
Submitter Yuya Nishihara
Date June 17, 2016, 2:45 p.m.
Message ID <e5243019c82eb1c22bdc.1466174732@mimosa>
Download mbox | patch
Permalink /patch/15539/
State Superseded
Delegated to: Martin von Zweigbergk
Headers show

Comments

Yuya Nishihara - June 17, 2016, 2:45 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1462250172 -32400
#      Tue May 03 13:36:12 2016 +0900
# Node ID e5243019c82eb1c22bdcad3bab6d775d7946a311
# Parent  249e5cd2f9a97e17872e78b6f1e16e709e01bca3
revset: optimize out sort() according to ordering policy

See the previous patch of reverse() for why. Unlike reverse(), we validate
the arguments by _getsortargs().

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2517,6 +2517,8 @@  def _optimize(x, small, order):
         wa, ta = _optimize(x[2], small, d)
         if f == 'reverse' and order != _defineorder:
             return wa, ta
+        if f == 'sort' and order != _defineorder:
+            return wa, _getsortargs(ta)[0]
         if f in ("author branch closed date desc file grep keyword "
                  "outgoing user"):
             w = 10 # slow
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -1191,8 +1191,7 @@  Test order of revisions in compound expr
   (see hg help "revsets.x or y")
   [255]
 
- BROKEN: sort() will need different workaround since it modifies the flag
- of input set:
+ sort() should be optimized out if order doesn't matter:
 
   $ try --optimize '0:2 & sort(all(), -rev)'
   (and
@@ -1214,23 +1213,24 @@  Test order of revisions in compound expr
       ('symbol', '2')
       ('string', 'define'))
     (func
-      ('symbol', '_reorder')
-      (func
-        ('symbol', 'sort')
-        (list
-          (func
-            ('symbol', 'all')
-            None)
-          ('string', '-rev')))))
+      ('symbol', 'all')
+      None))
   * set:
   <filteredset
-    <spanset- 0:2>,
-    <filteredset
-      <spanset- 0:2>,
-      <spanset+ 0:9>>>
+    <spanset+ 0:2>,
+    <spanset+ 0:9>>
+  0
+  1
   2
-  1
-  0
+
+ invalid argument for sort() to be optimized out:
+
+  $ log '0:2 & sort()'
+  hg: parse error: sort requires one or two arguments
+  [255]
+  $ log '0:2 & sort(all(), -invalid)'
+  hg: parse error: unknown sort key '-invalid'
+  [255]
 
  for 'A & f(B)', 'B' should not be affected by the order of 'A':