<story><title>Std::string half of all allocations in the Chrome browser process</title><url>https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ</url></story><parent_chain></parent_chain><comment><author>humanrebar</author><text>The problem with std::string is that it&amp;#x27;s named wrong. It should be called std::string_buffer, because that is what it is. Its performance characteristics are closer to a std::vector than a std::array (now available since C++11).&lt;p&gt;Many projects cannot copy around std::vector&amp;lt;char&amp;gt; in good conscience. They really want a copy-on-write string, an immutable string, a rope, a reference-counted string, or an always-in-place string. Or some combination of the above depending on the circumstance.&lt;p&gt;The problem is that std::string is &lt;i&gt;not&lt;/i&gt; a good type to use as a parameter for various reasons. In addition to its aggressive allocation behavior, it&amp;#x27;s also fairly inflexible. What are the alternatives?&lt;p&gt;1. boost::string_ref - available now, so use it&lt;p&gt;2. std::string_view - available starting in C++14 and works roughly like boost::string_ref&lt;p&gt;3. pass around pairs of iterators instead of single objects&lt;p&gt;3) is actually the most flexible, though it requires different kinds of overhead. The most obvious way would be to template all your string-accepting functions on two parameters: the type of your begin iterator and the type of your end iterator. But the benefit is that you can pass around any of the above to your heart&amp;#x27;s content, plus more, like elements in tries.&lt;p&gt;std::string still has an important place, but it should generally be used as a private member variable, not as something you require in your interface. Pretty much the same thing goes for char* unless you are implementing a C ABI  (plus a size, please). Even then, you can immediately convert to&amp;#x2F;from a boost::string_ref and still have yourself a self-contained reference to a bounded character sequence.</text></comment>
<story><title>Std::string half of all allocations in the Chrome browser process</title><url>https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ</url></story><parent_chain></parent_chain><comment><author>ctur</author><text>folly::fbstring, a drop-in replacement for std::string, is part of the folly library that we (Facebook) open sourced a while back.  It allocates small strings in-line and larger strings on the heap and has optimizations for medium and large strings, too.  It&amp;#x27;s proven quite effective for us, particularly when used with jemalloc, which it conspires with for more optimal memory management.  We use it &lt;i&gt;as&lt;/i&gt; std::string for our C++ applications and libraries, completely replacing std::string both for our own code and third-party code.&lt;p&gt;&lt;a href=&quot;https://github.com/facebook/folly/blob/master/folly/docs/FBString.md&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;facebook&amp;#x2F;folly&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;folly&amp;#x2F;docs&amp;#x2F;FBS...&lt;/a&gt;&lt;p&gt;In addition, it is worth noting folly::StringPiece (from folly&amp;#x2F;Range.h), which is generally a better interface for working with in-memory ranges of bytes.  Hardly a new idea (it&amp;#x27;s inspired by similar libraries, such as in re2), but it permeates the APIs of many of our larger C++ systems, and folly itself, and often avoids passing std::string objects around at all.&lt;p&gt;Finally, there is also folly::fbvector, which offers similar improvements over std::vector.</text></comment>