title: What are the mechanics of short string optimization in libc++?
body: <p><a href="https://stackoverflow.com/a/10319672/1805388">This answer</a> gives a nice high-level overview of short string optimization (SSO). However, I would like to know in more detail how it works in practice, specifically in the libc++ implementation:</p>
  
  <ul>
  <li><p>How short does the string have to be in order to qualify for SSO?
  Does this depend on the target architecture?</p></li>
  <li><p>How does the implementation distinguish between short and long
  strings when accessing the string data? Is it as simple as <code>m_size &lt;= 16</code> or is it a flag that is part of some other member variable? (I
  imagine that <code>m_size</code> or part of it might also be used to store
  string data).</p></li>
  </ul>
  
  <p>I asked this question specifically for libc++ because I know that it uses SSO, this is even mentioned on the <a href="http://libcxx.llvm.org/" rel="noreferrer">libc++ home page</a>.</p>
  
  <p>Here are some observations after looking at <a href="http://llvm.org/svn/llvm-project/libcxx/trunk/include/string" rel="noreferrer">the source</a>:</p>
  
  <p>libc++ can be compiled with two slightly different memory layouts for the string class, this is governed by the <code>_LIBCPP_ALTERNATE_STRING_LAYOUT</code> flag. Both of the layouts also distinguish between little-endian and big-endian machines which leaves us with a total of 4 different variants. I will assume the "normal" layout and little-endian in what follows.</p>
  
  <p>Assuming further that <code>size_type</code> is 4 bytes and that <code>value_type</code> is 1 byte, this is what the first 4 bytes of a string would look like in memory:</p>
  
  <pre><code>// short string: (s)ize and 3 bytes of char (d)ata
  sssssss0;dddddddd;dddddddd;dddddddd
         ^- is_long = 0
  
  // long string: (c)apacity
  ccccccc1;cccccccc;cccccccc;cccccccc
         ^- is_long = 1
  </code></pre>
  
  <p>Since the size of the short string is in the upper 7 bits, it needs to be shifted when accessing it:</p>
  
  <pre><code>size_type __get_short_size() const {
      return __r_.first().__s.__size_ &gt;&gt; 1;
  }
  </code></pre>
  
  <p>Similarly, the getter and setter for the capacity of a long string uses <code>__long_mask</code> to work around the <code>is_long</code> bit.</p>
  
  <p>I am still looking for an answer to my first question, i.e. what value would <code>__min_cap</code>, the capacity of short strings, take for different architectures?</p>
  
  <p><strong>Other standard library implementations</strong></p>
  
  <p><a href="https://stackoverflow.com/a/28003328/1805388">This answer</a> gives a nice overview of <code>std::string</code> memory layouts in other standard library implementations.</p>