Log4cxx
  1. Log4cxx
  2. LOGCXX-13

Add branch optimization hint to LOG4CXX_DEBUG macro

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 0.9.7
    • Fix Version/s: 0.10.0
    • Component/s: None
    • Labels:
      None

      Description

      Issue raised in thread containing:
      http://nagoya.apache.org/eyebrowse/ReadMsg?listName=log4cxx-user@logging.apache.org&msgNo=310

      Content-Type: text/plain; charset=US-ASCII; format=flowed
      From: Curt Arnold <carnold@apache.org>
      Subject: LOG4CXX macros
      Date: Thu, 12 Aug 2004 11:14:10 -0500

      In GCC, you can use __builtin_expect to hint to the optimizer the
      expected value of a expression
      (http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins).
      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 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

      For GCC it would be:

      #define LOG4CXX_UNLIKELY __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
      >

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Curt Arnold
            Reporter:
            Curt Arnold
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development