templater: compute revset lazily
authorYuya Nishihara <yuya@tcha.org>
Sun, 04 Nov 2018 20:44:26 +0900
changeset 40568 d11e2c5b287e
parent 40567 2dd3a0201307
child 40569 3c4b9dace7de
templater: compute revset lazily This speeds up e.g. "{ifcontains(rev, revset('::.'), ...)}" in common cases where 'rev' is near the working parent. The templater API is ugly, but it helps here. 'f' can be either a generator or a function returning a generator.
mercurial/templatefuncs.py
mercurial/templatekw.py
--- a/mercurial/templatefuncs.py	Wed Oct 24 18:48:43 2018 +0300
+++ b/mercurial/templatefuncs.py	Sun Nov 04 20:44:26 2018 +0900
@@ -559,7 +559,6 @@
     if len(args) > 1:
         formatargs = [evalfuncarg(context, mapping, a) for a in args[1:]]
         revs = query(revsetlang.formatspec(raw, *formatargs))
-        revs = list(revs)
     else:
         cache = context.resource(mapping, 'cache')
         revsetcache = cache.setdefault("revsetcache", {})
@@ -567,7 +566,6 @@
             revs = revsetcache[raw]
         else:
             revs = query(raw)
-            revs = list(revs)
             revsetcache[raw] = revs
     return templatekw.showrevslist(context, mapping, "revision", revs)
 
--- a/mercurial/templatekw.py	Wed Oct 24 18:48:43 2018 +0300
+++ b/mercurial/templatekw.py	Sun Nov 04 20:44:26 2018 +0900
@@ -774,7 +774,10 @@
     """helper to generate a list of revisions in which a mapped template will
     be evaluated"""
     repo = context.resource(mapping, 'repo')
-    f = _showcompatlist(context, mapping, name, ['%d' % r for r in revs])
+    # revs may be a smartset; don't compute it until f() has to be evaluated
+    def f():
+        srevs = ['%d' % r for r in revs]
+        return _showcompatlist(context, mapping, name, srevs)
     return _hybrid(f, revs,
                    lambda x: {name: x, 'ctx': repo[x]},
                    pycompat.identity, keytype=int)