Note that there are some explanatory texts on larger screens.

plurals
  1. POLocality and shared access to objects
    primarykey
    data
    text
    <p>Profiling my code, i see a lot of cache misses and would like to know whether there is a way to improve the situation. Optimization is not really needed, I'm more curious about whether there exist general approaches to this problem (this is a follow up question).</p> <pre><code>// class to compute stuff class A { double compute(); ... // depends on other objects std::vector&lt;A*&gt; dependencies; } </code></pre> <p>I have a container class that stores pointers to all created objects of class <code>A</code>. I do not store copies as I want to have shared access. Before I was using <code>shared_ptr</code>, but as single <code>A</code>s are meaningless without the container, raw pointers are fine. </p> <pre><code>class Container { ... void compute_all(); std::vector&lt;A*&gt; objects; ... } </code></pre> <p>The vector <code>objects</code> is insertion sorted in a way that the full evaluation can be done by simply iterating and calling <code>A.compute()</code>, all dependencies in A are resolved.</p> <p>With <code>a_i</code> objects of class <code>A</code>, the evaluation might look like this:</p> <pre><code>a_1 =&gt; a_2 =&gt; a_3 --&gt; a_2 --&gt; a_1 =&gt; a_4 =&gt; .... </code></pre> <p>where => denotes iteration in <code>Container</code> and --> iteration over <code>A::dependencies</code></p> <p>Moreover, the Container class is created only once and compute_all() is called many times, so rearranging the whole structure after creation <em>is an option</em> and wouldn't harm efficiency much.</p> <p>Now to the observations/questions: </p> <ol> <li><p>Obviously, iterating over <code>Container::objects</code> is cache efficient, but accessing the pointees is definitely not.</p></li> <li><p>Moreover, as each object of type <code>A</code> has to iterate over <code>A::dependencies</code>, which again can produces cache misses. </p></li> </ol> <p>Would it help to create a separate <code>vector&lt;A*&gt;</code> from all needed object in evaluation order such that dependencies in <code>A</code> are inserted as copies?</p> <p>Something like this:</p> <pre><code>a_1 =&gt; a_2 =&gt; a_3 =&gt; a_2_c =&gt; a_1_c =&gt; a_4 -&gt; .... </code></pre> <p>where a_i_c are copies from a_i.</p> <p>Thanks for your help and sorry if this question is confusing, but I find it rather difficult to extrapolate from simple examples to large applications.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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