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

Greg Ward greg at gerg.ca
Thu Sep 13 20:19:30 CDT 2012


On 14 September 2012, Mads Kiilerich said:
> 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.

+1 in general... I think the standard logging module got this right.
Would be nice if ui supported that style.

However, in this case Bryan is saving more than just string
formatting. He's saving two attribute lookups and a function call. If
this is such a hot loop, it's probably worth the extra "if".

(Dontcha sometimes miss "#if DEBUG" just a little bit? ;-)

       Greg


More information about the Mercurial-devel mailing list