logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: LOG4CXX macros
Date Thu, 12 Aug 2004 16:14:10 GMT
In GCC, you can use __builtin_expect to hint to the optimizer the 
expected value of a expression 
  So if LOG4CXX_DEBUG evaluated to:

if (__builtin_expect(log.isDebugEnabled(), 0)) {
    log.forceLog(Level::DEBUG, msg);

Then the compiler would know that it would be relatively unlikely for 
the body of the if to be executed and could optimize appropriately.  If 
it has an impact on the performance, it should make the code run faster 
when the debug messages are disabled and slower when they are enabled.  
__builtin_expect doesn't actually call anything.

I'd probably implement this by defining a LOG4CXX_UNLIKELY(x) macro 
that would be used in  LOG4CXX_DEBUG macro.  If the compiler did not 
support branch hints, then the definition of LOG4CXX_UNLIKELY would be:

#define LOG4CXX_UNLIKELY(x) x

For GCC it would be:

#define LOG4CXX_UNLIKELY(x) __builtin_expect(x, 0)

On Aug 12, 2004, at 10:04 AM, Christophe de VIENNE wrote:

> Curt Arnold wrote:
>> Several compilers including gcc have a mechanism to hint that one  
>> branch of an if statement is more likely to be executed.  It may be  
>> desirable that the LOG4CXX_DEBUG hint that isDebugEnabled will likely 
>>  return false which would allow better optimization for the normal 
>> case.   Any comments on the benefits or consequences of providing 
>> branch  hints?
> I'm not sure of what you mean by branch hint. Do you have a small and 
> concrete example ?
> Christophe

View raw message