title: Adding a unique_ptr to a class in a vector results in 3x speedup
body: <h2>Background</h2>
  
  <p>I have a large graph (100k nodes), in which each node has to store a bit of information per outgoing edge. Instead of keeping this in a <code>std::vector&lt;bool&gt;</code>, I'm using <code>dynamic_bitset</code> from Boost 1.58 for the ability to perform bitwise operations. Each node also keeps a pointer to some polymorphic object. A minimal example looks like this,</p>
  
  <pre><code>struct Node
  {
      std::vector&lt;size_t&gt;     succ;
      boost::dynamic_bitset&lt;&gt; succ_flags;
      std::unique_ptr&lt;Object&gt; data;
  };
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>Consider this simple benchmark program, which creates and destroys a graph:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;boost/dynamic_bitset.hpp&gt;
  #include &lt;memory&gt;
  
  constexpr int N = 50000;
  
  struct Node
  {
      std::vector&lt;size_t&gt;     succ;
      boost::dynamic_bitset&lt;&gt; succ_flags;
    //std::unique_ptr&lt;int&gt;    data;
  
      Node(int i)
      {
          for (int j = i; j &lt; N; j += i) succ.emplace_back(j);
          succ_flags.resize(succ.size());
      }
  };
  
  int main()
  {
      std::vector&lt;Node&gt; nodes;
      for (int i = 1; i &lt;= N; i++) nodes.emplace_back(i);
      return 0;
  }
  </code></pre>
  
  <p>Running under the <code>time</code> command, a typical result is</p>
  
  <pre><code>real    0m0.055s
  user    0m0.043s
  sys     0m0.010s
  </code></pre>
  
  <p>However, uncommenting the <code>unique_ptr</code> line gives something more like</p>
  
  <pre><code>real    0m0.017s
  user    0m0.010s
  sys     0m0.003s
  </code></pre>
  
  <p><em>Conclusion:</em> making <code>Node</code> heavier by adding a <code>std::unique_ptr</code> data member causes <code>std::vector</code> to perform over 3x faster!</p>
  
  <h2>Question</h2>
  
  <p>Why is this happening, and what sort of gotcha is at work here?</p>