Patchwork [1,of,2] templatekw: add utility function numrevs

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date Jan. 18, 2018, 3:14 a.m.
Message ID <701f8a9defdc09bb63f2.1516245269@Iris>
Download mbox | patch
Permalink /patch/26838/
State Deferred, archived
Headers show

Comments

Jordi Gutiérrez Hermoso - Jan. 18, 2018, 3:14 a.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1516242948 18000
#      Wed Jan 17 21:35:48 2018 -0500
# Node ID 701f8a9defdc09bb63f2596e2fc426f2e78da313
# Parent  0e369eca888fc80ee980fe8200c59dc7b0024dae
templatekw: add utility function numrevs

len(repo) calls incur noticeable overhead if called for each revision
a template keyword is evaluated. We obviously can't cache __len__
functions in general because many commands modify the repo size. For
template keywords, however, the repo is read-only so we can benefit
from a cache.
Yuya Nishihara - Jan. 18, 2018, 12:03 p.m.
On Wed, 17 Jan 2018 22:14:29 -0500, Jordi Gutiérrez Hermoso wrote:
> # HG changeset patch
> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> # Date 1516242948 18000
> #      Wed Jan 17 21:35:48 2018 -0500
> # Node ID 701f8a9defdc09bb63f2596e2fc426f2e78da313
> # Parent  0e369eca888fc80ee980fe8200c59dc7b0024dae
> templatekw: add utility function numrevs
> 
> len(repo) calls incur noticeable overhead if called for each revision
> a template keyword is evaluated.

Do you have some number? It's just a proxy to revlog.__len__(), so I think it
is super fast.

> --- a/mercurial/templatekw.py
> +++ b/mercurial/templatekw.py
> @@ -852,6 +852,10 @@ def showrev(repo, ctx, templ, **args):
>      """Integer. The repository-local changeset revision number."""
>      return scmutil.intrev(ctx)
>  
> +@util.cachefunc
> +def numrevs(repo):
> +    return len(repo)

It leaks repo forever as the cache is keyed by arguments. args['cache'] can
be used instead.
Jordi Gutiérrez Hermoso - Jan. 18, 2018, 3:34 p.m.
On Thu, 2018-01-18 at 21:03 +0900, Yuya Nishihara wrote:
> On Wed, 17 Jan 2018 22:14:29 -0500, Jordi Gutiérrez Hermoso wrote:
> > # HG changeset patch
> > # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> > # Date 1516242948 18000
> > #      Wed Jan 17 21:35:48 2018 -0500
> > # Node ID 701f8a9defdc09bb63f2596e2fc426f2e78da313
> > # Parent  0e369eca888fc80ee980fe8200c59dc7b0024dae
> > templatekw: add utility function numrevs
> > 
> > len(repo) calls incur noticeable overhead if called for each
> > revision
> > a template keyword is evaluated.

> Do you have some number? It's just a proxy to revlog.__len__(), so I
> think it
> is super fast.

The Python function calls leading up to it are not that fast. Without
this caching, it takes 7.2 seconds on average on my machine to to do
`hg log -T '{negrev}\n' > /dev/null`. With the caching, it takes 6.4
seconds on average.

Not a huge difference, but a difference. I wanted to match the speed
of `hg log -T '{rev}\n' > /dev/null`, and this caching helped me match
it.

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -852,6 +852,10 @@  def showrev(repo, ctx, templ, **args):
     """Integer. The repository-local changeset revision number."""
     return scmutil.intrev(ctx)
 
+@util.cachefunc
+def numrevs(repo):
+    return len(repo)
+
 def showrevslist(name, revs, **args):
     """helper to generate a list of revisions in which a mapped template will
     be evaluated"""