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

Isaac Jurado diptongo at gmail.com
Fri Sep 14 01:58:40 CDT 2012


On Fri, Sep 14, 2012 at 12:30 AM, Mads Kiilerich <mads at kiilerich.com> wrote:
> 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.

That is, in fact, how the Python standard logging package does it:

http://docs.python.org/library/logging.html#logging.Logger.debug

It does the string formatting internally, so you can save the
operation (and the resulting new string) in case that message has to
be omitted.

Nevertheless, as Bryan points out, when performance is really critical
a conditional check before calling the function is always cheaper,
even when the called function is only a "pass" statement.

-- 
Isaac Jurado

"The noblest pleasure is the joy of understanding"
Leonardo da Vinci


More information about the Mercurial-devel mailing list