[PATCH 6 of 6 RFC] perf: define formatter locally if Mercurial is earlier than 2.2

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Jun 17 13:04:48 EDT 2016


The code itself seems mostly good to me (I need a second pass) but I'm a
bit confused about why we want to make perf.py backward compatible? Old
version of Mercurial can use the old version of the perf extension,
isn't that enough? I'm curious about your use case here.

On 06/10/2016 09:20 PM, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
> # Date 1465585986 -32400
> #      Sat Jun 11 04:13:06 2016 +0900
> # Node ID ab257e0c53440b80edd52e312f6ac8859e6c65b5
> # Parent  b5487e8e3142fc650b5fa3d0fbe3e8ff4e24da3e
> perf: define formatter locally if Mercurial is earlier than 2.2
> 
> Before this patch, using ui.formatter() prevents perf.py from
> measuring performance with Mercurial earlier than 2.2 (or
> ae5f92e154d3), because ui.formatter() isn't available in such
> Mercurial, even though there are some code paths for Mercurial earlier
> than 2.2.
> 
> For example, setting "_prereadsize" attribute in perfindex() and
> perfnodelookup() is effective only with hg earlier than 1.8 (or
> 61c9bc3da402).
> 
> This patch defines formatter class locally, and use it instead of the
> value returned by ui.formatter(), if Mercurial is earlier than 2.2 at
> runtime.
> 
> In this case, we don't need to think about -T/--template option for
> formatter, because previous patch made -T/--template disabled for
> perf.py with Mercurial earlier than 3.2 (or 7a7eed5176a4).
> 
> diff --git a/contrib/perf.py b/contrib/perf.py
> --- a/contrib/perf.py
> +++ b/contrib/perf.py
> @@ -94,7 +94,36 @@ def gettimer(ui, opts=None):
>      ui = ui.copy()
>      ui.fout = ui.ferr
>      # get a formatter
> -    fm = ui.formatter('perf', opts)
> +    if util.safehasattr(ui, 'formatter'):
> +        fm = ui.formatter('perf', opts)
> +    else:
> +        # ui.formatter has been available since 2.2 (or ae5f92e154d3)
> +        from mercurial import node
> +        class defaultformatter(object):
> +            """Minimized composition of baseformatter and plainformatter
> +            """
> +            def __init__(self, ui, topic, opts):
> +                self._ui = ui
> +                if ui.debugflag:
> +                    self.hexfunc = node.hex
> +                else:
> +                    self.hexfunc = node.short
> +            def __nonzero__(self):
> +                return False
> +            def startitem(self):
> +                pass
> +            def data(self, **data):
> +                pass
> +            def write(self, fields, deftext, *fielddata, **opts):
> +                self._ui.write(deftext % fielddata, **opts)
> +            def condwrite(self, cond, fields, deftext, *fielddata, **opts):
> +                if cond:
> +                    self._ui.write(deftext % fielddata, **opts)
> +            def plain(self, text, **opts):
> +                self._ui.write(text, **opts)
> +            def end(self):
> +                pass
> +        fm = defaultformatter(ui, 'perf', opts)
>      # stub function, runs code only once instead of in a loop
>      # experimental config: perf.stub
>      if ui.configbool("perf", "stub"):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
> 

-- 
Pierre-Yves DAvid


More information about the Mercurial-devel mailing list