Note that there are some explanatory texts on larger screens.

plurals
  1. POoptimization of access to members in c++
    text
    copied!<p>I'm running into an inconsistent optimization behavior with different compilers for the following code:</p> <pre><code>class tester { public: tester(int* arr_, int sz_) : arr(arr_), sz(sz_) {} int doadd() { sm = 0; for (int n = 0; n &lt; 1000; ++n) { for (int i = 0; i &lt; sz; ++i) { sm += arr[i]; } } return sm; } protected: int* arr; int sz; int sm; }; </code></pre> <p>The <code>doadd</code> function simulates some intensive access to members (ignore the overflows in addition for this question). Compared with similar code implemented as a function:</p> <pre><code>int arradd(int* arr, int sz) { int sm = 0; for (int n = 0; n &lt; 1000; ++n) { for (int i = 0; i &lt; sz; ++i) { sm += arr[i]; } } return sm; } </code></pre> <p>The <strong><code>doadd</code> method runs about 1.5 times slower than the <code>arradd</code> function</strong> when compiled in Release mode with Visual C++ 2008. When I modify the <code>doadd</code> method to be as follows (aliasing all members with locals):</p> <pre><code>int doadd() { int mysm = 0; int* myarr = arr; int mysz = sz; for (int n = 0; n &lt; 1000; ++n) { for (int i = 0; i &lt; mysz; ++i) { mysm += myarr[i]; } } sm = mysm; return sm; } </code></pre> <p>Runtimes become roughly the same. Am I right in concluding that this is a missing optimization by the Visual C++ compiler? <code>g++</code> seems to do it better and run both the member function and the normal function at the same speed when compiling with <code>-O2</code> or <code>-O3</code>.</p> <hr> <p><em>The benchmarking is done by invoking the <code>doadd</code> member and <code>arradd</code> function on some sufficiently large array (a few millions of integers in size).</em></p> <hr> <p><strong>EDIT:</strong> Some fine-grained testing shows that the main culprit is the <code>sm</code> member. Replacing all others by local versions still makes the runtime long, but once I replace <code>sm</code> by <code>mysm</code> the runtime becomes equal to the function version.</p> <hr> <p><img src="https://i.stack.imgur.com/1nGlX.jpg" alt="alt text"></p> <h1>Resolution</h1> <p>Dissapointed with the answers (sorry guys), I shaked off my laziness and dove into the disassembly listings for this code. My <a href="https://stackoverflow.com/questions/3930813/optimization-of-access-to-members-in-c/4002917#4002917">answer below</a> summarizes the findings. In short: it has nothing to do with aliasing, it has all to do with loop unrolling, and with some strange heuristics MSVC applies when deciding which loop to unroll.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload