[PATCH 05 of 10] wireproto: don't format a debug string inside a hot loop

Mads Kiilerich mads at kiilerich.com
Thu Sep 13 17:30:39 CDT 2012


Bryan O'Sullivan wrote, On 09/13/2012 09:01 PM:
> # HG changeset patch
> # User Bryan O'Sullivan <bryano at fb.com>
> # Date 1347562629 25200
> # Node ID d311c3d3c70fabe73f1053d238fc319ebdee20cb
> # Parent  8a634678f1ddf810c61b66752b732e69abd265e8
> wireproto: don't format a debug string inside a hot loop
>
> This improves stream_out performance by about 5%.
>
> diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
> --- a/mercurial/wireproto.py
> +++ b/mercurial/wireproto.py
> @@ -547,13 +547,15 @@
>           yield '%d %d\n' % (len(entries), total_bytes)
>   
>           sopener = repo.sopener
> +        debugflag = repo.ui.debugflag
>   
>           try:
>               oldaudit = sopener.mustaudit
>               sopener.mustaudit = False
>   
>               for name, size in entries:
> -                repo.ui.debug('sending %s (%d bytes)\n' % (name, size))
> +                if debugflag:
> +                    repo.ui.debug('sending %s (%d bytes)\n' % (name, size))

This pattern could probably be useful in a lot of places ... except that 
we don't want to check the  debugflag everywhere.

In another Python project we avoid most of the debug overhead by using a 
debug function where the formatting is done by the debug function - but 
obviously only if debugging is enabled.

The current feature of ui.debug where multiple messages can be written 
at once is only used in a few places. We could perhaps instead do 
something like

--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -650,11 +650,13 @@
          if self.verbose:
              opts['label'] = opts.get('label', '') + ' ui.note'
              self.write(*msg, **opts)
-    def debug(self, *msg, **opts):
+    def debug(self, msg, *values, **opts):
          '''write debug message to output (if ui.debugflag is True)

          This adds an output label of "ui.debug".
          '''
+        if values:
+            msg = msg % values
          if self.debugflag:
              opts['label'] = opts.get('label', '') + ' ui.debug'
              self.write(*msg, **opts)
and
   repo.ui.debug('sending %s (%d bytes)\n', name, size)
... or introduce a ui.debug2().

/Mads




More information about the Mercurial-devel mailing list