Boost C++ Libraries: Ticket #8831: Reuse capacity from user containers in order to prevent superfluous allocations https://svn.boost.org/trac10/ticket/8831 <p> Inside boost::algorithm::split, new variable of container type is created, and then swapped with result when fully filled. </p> <p> See <a href="http://www.boost.org/doc/libs/1_54_0/boost/algorithm/string/iter_find.hpp">http://www.boost.org/doc/libs/1_54_0/boost/algorithm/string/iter_find.hpp</a> , there is code like: </p> <pre class="wiki">SequenceSequenceT Tmp(itBegin, itEnd); Result.swap(Tmp); </pre><p> Maybe that was done in pursuit of strong exception safety guarantee - but I don't see much value for it in that case, because split is supposed to replace values in original container - <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/5915"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/5915</a> . I think basic guarantee would be enough. </p> <p> Often SequenceSequenceT is container like std::vector, which already has capacity from previous usages, which can be reused avoiding costly allocations. For example: </p> <pre class="wiki">Result.assign(itBegin, itEnd); </pre><p> Maybe that would require stricter requirements on SequenceSequenceT, or maybe overload or traits specialization can be used for common things like std::vector and boost::container::vector or as customization point. </p> <p> Here is proof-of-concept which avoids allocations showing speed difference: <a class="ext-link" href="http://coliru.stacked-crooked.com/view?id=bf5dd9f2d9d20d61470e73a6b2940333-9a9914b3e2b7ed07c206d6accecccdb6"><span class="icon">​</span>http://coliru.stacked-crooked.com/view?id=bf5dd9f2d9d20d61470e73a6b2940333-9a9914b3e2b7ed07c206d6accecccdb6</a> </p> <p> On my machine I have following results: </p> <pre class="wiki">start end 0.85 s 64000000 start end 1.55 s 64000000 </pre><p> I.e. version with allocations is ~1.8x slower. </p> <p> Maybe other algorithms have similar issues - I haven't checked. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8831 Trac 1.4.3 Evgeny Panasyuk <evgeny.panasyuk@…> Tue, 16 Jul 2013 20:09:12 GMT attachment set https://svn.boost.org/trac10/ticket/8831 https://svn.boost.org/trac10/ticket/8831 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_string_algo_reuse_capacity.cpp</span> </li> </ul> Ticket