title: C++ Eigen Library: Performance overhead of Ref<>
body: <p>I am writing a general purpose library using Eigen for computational mechanics, 
  dealing mostly with 6x6 sized matrices and 6x1 sized vectors.
  I consider using the <code>Eigen::Ref&lt;&gt;</code> template to make it usable also for segments and blocks, as documented in <a href="http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html" rel="nofollow noreferrer">http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html</a> and <a href="https://stackoverflow.com/questions/21132538/correct-usage-of-the-eigenref-class">Correct usage of the Eigen::Ref&lt;&gt; class</a></p>
  
  <p>However, a small performance comparison reveals that <code>Eigen::Ref</code> has a considerable overhead for such small functions compared to standard c++ references:</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include "Eigen/Core"
  
  
  Eigen::Matrix&lt;double, 6, 6&gt; testRef(const Eigen::Ref&lt;const Eigen::Matrix&lt;double, 6, 6&gt;&gt;&amp; A)
  {
      Eigen::Matrix&lt;double, 6, 6&gt; temp = (A * A) * A;
      temp.diagonal().setOnes();
      return temp;
  }
  
  Eigen::Matrix&lt;double, 6, 6&gt; testNoRef(const Eigen::Matrix&lt;double, 6, 6&gt;&amp; A)
  {
      Eigen::Matrix&lt;double, 6, 6&gt; temp = (A * A) * A; 
      temp.diagonal().setOnes();
      return temp;
  }
  
  
  int main(){
  
    using namespace std;
  
    int cycles = 10000000;
    Eigen::Matrix&lt;double, 6, 6&gt; testMat;
    testMat = Eigen::Matrix&lt;double, 6, 6&gt;::Ones();
  
    clock_t begin = clock();
  
    for(int i = 0; i &lt; cycles; i++)
        testMat = testRef(testMat);
  
    clock_t end = clock();
  
  
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  
    std::cout &lt;&lt; "Ref: " &lt;&lt; elapsed_secs &lt;&lt; std::endl;
  
    begin = clock();
  
    for(int i = 0; i &lt; cycles; i++)
        testMat = testNoRef(testMat);
    end = clock();
  
    elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  
    std::cout &lt;&lt; "noRef : " &lt;&lt; elapsed_secs &lt;&lt; std::endl;
  
  
      return 0;
  }
  </code></pre>
  
  <p>Output with <code>gcc -O3</code>:</p>
  
  <pre><code>Ref: 1.64066
  noRef : 1.1281
  </code></pre>
  
  <p>So it seems that <code>Eigen::Ref</code> has considerable overhead, at least in cases with low actual computational effort.
  On the other hand, the approach using <code>const Eigen::Matrix&lt;double, 6, 6&gt;&amp; A</code> leads to unnecessary copies if blocks or segments are passed:</p>
  
  <pre><code>#include &lt;Eigen/Core&gt;
  #include &lt;iostream&gt;
  
  
  void test( const Eigen::Vector3d&amp; a)
  {
      std::cout &lt;&lt; "addr in function " &lt;&lt; &amp;a &lt;&lt; std::endl;
  }
  
  int main () {
  
      Eigen::Vector3d aa;
      aa &lt;&lt; 1,2,3;
      std::cout &lt;&lt; "addr outside function " &lt;&lt; &amp;aa &lt;&lt; std::endl;
  
      test ( aa ) ;
      test ( aa.head(3) ) ;
  
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>addr outside function 0x7fff85d75960
  addr in function 0x7fff85d75960
  addr in function 0x7fff85d75980
  </code></pre>
  
  <p>So this approach is excluded for the general case.</p>
  
  <p>Alternatively, one could make function templates using <code>Eigen::MatrixBase</code>, as described in the documentation. However, this seems to be inefficient for large libraries, and it cannot be adapted to fixed size matrices (6x6, 6x1) as in my case.</p>
  
  <p>Is there any other alternative? 
  What is the general recommendation for large general purpose libraries?</p>
  
  <p>Thank you in advance!</p>
  
  <p>edit: Modified the first benchmark example according to the recommendations in the comments</p>