Boost C++ Libraries: Ticket #9165: small buffer optimization for vector or new container https://svn.boost.org/trac10/ticket/9165 <p> It is already requested may times (e.g. see <a class="ext-link" href="http://stackoverflow.com/questions/18530512/stl-boost-equivalent-of-llvm-smallvector"><span class="icon">​</span>http://stackoverflow.com/questions/18530512/stl-boost-equivalent-of-llvm-smallvector</a>). In programs one uses vector's a lot; unfortunately for small containers every time a heady allocation is involved. According to stackoverflow (<a class="ext-link" href="http://stackoverflow.com/questions/8190950/may-stdvector-make-use-of-small-buffer-optimization"><span class="icon">​</span>http://stackoverflow.com/questions/8190950/may-stdvector-make-use-of-small-buffer-optimization</a>), the current standard forbids it. But why not make another container class then? We already have: </p> <ul><li>vector </li><li>stable_vector </li><li>static_vector </li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9165 Trac 1.4.3 Ion Gaztañaga Fri, 27 Sep 2013 08:19:48 GMT status changed https://svn.boost.org/trac10/ticket/9165#comment:1 https://svn.boost.org/trac10/ticket/9165#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> This is an interesting feature that was in the Boost.Container to-do list for a long time. Note that LLVM <a class="missing wiki">SmallVector</a>&lt;T, N&gt; is also more flexible than allowing small buffer optimization, as it can be converted to a base <a class="missing wiki">SmallVectorImpl</a>&lt;T&gt; class that is independent from N, allowing capacity-independent code: </p> <p> <a class="ext-link" href="http://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h"><span class="icon">​</span>http://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h</a> </p> <div class="wiki-code"><div class="code"><pre><span class="c1">// BAD: Clients cannot pass e.g. SmallVector&lt;Foo, 4&gt;.</span> <span class="n">hardcodedSmallSize</span><span class="p">(</span><span class="n">SmallVector</span><span class="o">&lt;</span><span class="n">Foo</span><span class="p">,</span> <span class="mi">2</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">Out</span><span class="p">);</span> <span class="c1">// GOOD: Clients can pass any SmallVector&lt;Foo, N&gt;.</span> <span class="n">allowsAnySmallSize</span><span class="p">(</span><span class="n">SmallVectorImpl</span><span class="o">&lt;</span><span class="n">Foo</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">Out</span><span class="p">);</span> <span class="kt">void</span> <span class="nf">someFunc</span><span class="p">()</span> <span class="p">{</span> <span class="n">SmallVector</span><span class="o">&lt;</span><span class="n">Foo</span><span class="p">,</span> <span class="mi">8</span><span class="o">&gt;</span> <span class="n">Vec</span><span class="p">;</span> <span class="n">hardcodedSmallSize</span><span class="p">(</span><span class="n">Vec</span><span class="p">);</span> <span class="c1">// Error.</span> <span class="n">allowsAnySmallSize</span><span class="p">(</span><span class="n">Vec</span><span class="p">);</span> <span class="c1">// Works.</span> <span class="p">}</span> </pre></div></div><p> Small buffer optimization can be achieved (without LLVM capacity-independent Impl class), allowing extended allocator requirements. Currently the STL says for allocator.requirements that: </p> <div class="wiki-code"><div class="code"><pre><span class="c1">//Shall not exit via an exception.</span> <span class="n">X</span> <span class="nf">a1</span><span class="p">(</span><span class="n">a</span><span class="p">);</span> <span class="c1">//post: a1 == a</span> </pre></div></div><p> For SBO allocators (allocators holding an internal buffer) that would no longer be true, so boost::container::vector could support this extended allocators. We'd need to see how this affects to exception safety (swap could throw for throwing copy/move constructors) and it might require extending boost::container::allocator_traits and how vector behaves in copy/move constructor and assignments (¿should this SBO allocator be propagated?). </p> <p> If this is successful, SBO could be applied to any Boost.Container container. </p> <p> Implementing LLVM-like capacity-independent features is much tougher, as it might require a really big surgery or writing new classes from the scratch. </p> <p> I'll start exploring extended allocator requirements for Boost 1.56 or 1.57. Thanks for the ticket and the links. </p> Ticket crazy2be Tue, 24 Nov 2015 22:59:13 GMT <link>https://svn.boost.org/trac10/ticket/9165#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9165#comment:2</guid> <description> <p> Hasn't this now been implemented as [boost::small_vector](<a href="http://www.boost.org/doc/libs/master/doc/html/boost/container/small_vector.html">http://www.boost.org/doc/libs/master/doc/html/boost/container/small_vector.html</a>)? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 24 Dec 2015 00:21:44 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9165#comment:3 https://svn.boost.org/trac10/ticket/9165#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Closing as small vector was introduced in Boost 1.58 </p> Ticket