diff mercurial/revsetlang.py @ 41222:8aca89a694d4

revset: introduce an API that avoids `formatspec` input serialization Instead of having the data fully serialized, the input can be directly inserted in the tree at a later stage. Just using it for simple "%ld" case provide a significant boost. For example here are the impact on a sample discovery run between two pypy repositories with arbitrary differences (using hg perfdiscovery). $ hg perfdiscovery before: ! wall 0.700435 comb 0.710000 user 0.700000 sys 0.010000 (median of 15) after: ! wall 0.501305 comb 0.510000 user 0.490000 sys 0.020000 (median of 20)
author Boris Feld <boris.feld@octobus.net>
date Fri, 04 Jan 2019 13:41:21 +0100
parents 73203cdfe3fe
children 26b0a7514f01
line wrap: on
line diff
--- a/mercurial/revsetlang.py	Fri Jan 04 05:26:13 2019 +0100
+++ b/mercurial/revsetlang.py	Fri Jan 04 13:41:21 2019 +0100
@@ -333,7 +333,7 @@
     elif op == 'negate':
         s = getstring(x[1], _("can't negate that"))
         return _analyze(('string', '-' + s))
-    elif op in ('string', 'symbol'):
+    elif op in ('string', 'symbol', 'smartset'):
         return x
     elif op == 'rangeall':
         return (op, None)
@@ -373,7 +373,7 @@
         return 0, x
 
     op = x[0]
-    if op in ('string', 'symbol'):
+    if op in ('string', 'symbol', 'smartset'):
         return 0.5, x # single revisions are small
     elif op == 'and':
         wa, ta = _optimize(x[1])
@@ -535,7 +535,8 @@
 def foldconcat(tree):
     """Fold elements to be concatenated by `##`
     """
-    if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
+    if (not isinstance(tree, tuple)
+        or tree[0] in ('string', 'symbol', 'smartset')):
         return tree
     if tree[0] == '_concat':
         pending = [tree]
@@ -691,6 +692,28 @@
             raise error.ProgrammingError("unknown revspec item type: %r" % t)
     return b''.join(ret)
 
+def spectree(expr, *args):
+    """similar to formatspec but return a parsed and optimized tree"""
+    parsed = _parseargs(expr, args)
+    ret = []
+    inputs = []
+    for t, arg in parsed:
+        if t is None:
+            ret.append(arg)
+        elif t == 'baseset':
+            newtree = ('smartset', smartset.baseset(arg))
+            inputs.append(newtree)
+            ret.append("$")
+        else:
+            raise error.ProgrammingError("unknown revspec item type: %r" % t)
+    expr = b''.join(ret)
+    tree = _parsewith(expr, syminitletters=_aliassyminitletters)
+    tree = parser.buildtree(tree, ('symbol', '$'), *inputs)
+    tree = foldconcat(tree)
+    tree = analyze(tree)
+    tree = optimize(tree)
+    return tree
+
 def _parseargs(expr, args):
     """parse the expression and replace all inexpensive args