title: Why does my logging library cause performance tests to run faster?
body: <p>I have spent the past year developing a logging library in C++ with performance in mind. To evaluate performance I developed <a href="https://github.com/mattiasflodin/reckless/blob/de548af1f5ea9f716f76b691402b52107ef701e3/doc/performance.md" rel="noreferrer">a set of benchmarks</a> to compare my code with other libraries, including a base case that performs no logging at all.</p>
  
  <p>In my last benchmark I measure the total running time of a CPU-intensive task while logging is active and when it is not. I can then compare the time to determine how much overhead my library has. This bar chart shows the difference compared to my non-logging base case.</p>
  
  <p><img src="https://i.stack.imgur.com/vxtc4.png" alt="performance chart"></p>
  
  <p>As you can see, my library ("reckless") adds <em>negative overhead</em> (unless all 4 CPU cores are busy). The program runs about half a second faster when logging is enabled than when it is disabled.</p>
  
  <p>I know I should try to isolate this down to a simpler case rather than asking about a 4000-line program. But there are so many venues for what to remove, and without a hypothesis I will just make the problem go away when I try to isolate it. I could probably spend another year just doing this. I'm hoping that the collective expertise of Stack Overflow will make this a much more shallow problem or that the cause will be obvious to someone who has more experience than me.</p>
  
  <p>Some facts about my library and the benchmarks:</p>
  
  <ul>
  <li>The library consists of a front-end API that pushes the log arguments onto a lockless queue (Boost.Lockless) and a back-end thread that performs string formatting and writes the log entries to disk.</li>
  <li>The timing is based on simply calling <code>std::chrono::steady_clock::now()</code> at the beginning and end of the program, and printing the difference.</li>
  <li>The benchmark is run on a 4-core Intel CPU (i7-3770K).</li>
  <li>The benchmark program computes a 1024x1024 Mandelbrot fractal and logs statistics about each pixel, i.e. it writes about one million log entries.</li>
  <li>The total running time is about 35 seconds for the single worker-thread case. So the speed increase is about 1.5%.</li>
  <li>The benchmark produces an output file (this is not part of the timed code) that contains the generated Mandelbrot fractal. I have verified that the same output is produced when logging is on and off.</li>
  <li>The benchmark is run 100 times (with all the benchmarked libraries, this takes about 10 hours). The bar chart shows the average time and the error bars show the interquartile range.</li>
  <li><a href="https://github.com/mattiasflodin/reckless/blob/de548af1f5ea9f716f76b691402b52107ef701e3/benchmarks/mandelbrot.cpp" rel="noreferrer">Source code for the Mandelbrot computation</a></li>
  <li><a href="https://github.com/mattiasflodin/reckless/blob/de548af1f5ea9f716f76b691402b52107ef701e3/benchmarks/benchmark_mandelbrot.cpp" rel="noreferrer">Source code for the benchmark</a>.</li>
  <li><a href="https://github.com/mattiasflodin/reckless/tree/de548af1f5ea9f716f76b691402b52107ef701e3" rel="noreferrer">Root of the code repository and documentation</a>.</li>
  </ul>
  
  <p>My question is, how can I explain the apparent speed increase when my logging library is enabled?</p>
  
  <p><strong>Edit</strong>: This was solved after trying the suggestions given in comments. My log object is created on <a href="https://github.com/mattiasflodin/reckless/blob/de548af1f5ea9f716f76b691402b52107ef701e3/benchmarks/benchmark_mandelbrot.cpp#L24" rel="noreferrer">line 24 of the benchmark test</a>. Apparently when LOG_INIT() touches the log object it triggers a page fault that causes some or all pages of the image buffer to be mapped to physical memory. I'm still not sure why this improves the performance by almost half a second; even without the log object, the first thing that happens in the mandelbrot_thread() function is a write to the bottom of the image buffer, which should have a similar effect. But, in any case, clearing the buffer with a memset() before starting the benchmark makes everything more sane. Current benchmarks are <a href="https://github.com/mattiasflodin/reckless/blob/master/doc/performance.md" rel="noreferrer">here</a></p>
  
  <p>Other things that I tried are:</p>
  
  <ul>
  <li>Run it with the oprofile profiler. I was never able to get it to register <em>any</em> time in the locks, even after enlarging the job to make it run for about 10 minutes. Almost all the time was in the inner loop of the Mandelbrot computation. But maybe I would be able to interpret them differently now that I know about the page faults. I didn't think to check whether the image write was taking a disproportionate amount of time.</li>
  <li>Removing the locks. This did have a significant effect on performance, but results were still weird and anyway I couldn't do the change in any of the multithreaded variants.</li>
  <li>Compare the generated assembly code. There were differences but the logging build was clearly doing more things. Nothing stood out as being an obvious performance killer.</li>
  </ul>