<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - libc++'s string::assign isn't O(1) when assigning the string to itself if the system's memmove() doesn't check for that"
   href="https://llvm.org/bugs/show_bug.cgi?id=25413">25413</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>libc++'s string::assign isn't O(1) when assigning the string to itself if the system's memmove() doesn't check for that
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>nicolasweber@gmx.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider this program:

int main() {
  string s(8 * 1024 * 1024, ' ');

  string d; d.reserve(s.size());

  for (int i = 0; i < s.size(); i += 4096) {
    d.append(&s[i], 4096);
    const char* p = d.data();
    const char* end = p + d.size();
    d.assign(p, end - p);
  }
}


It might look silly, but it's reduced from real-world code that reads chunks of
a pipe, processes completely read packets, and uses assign() to move chunks of
incomplete packets to the front of d. If a packet spans several chunks, that's
the code that effectively runs (from <a href="http://crbug.com/547387">http://crbug.com/547387</a>).

In all standard library implementations that aren't libc++, this runs quickly,
because they all check if p overlaps with the string stored in d and do
something intelligent in that case. libc++'s generic char_traits::move() does
that too from what I can tell, but the char specialization of char_traits just
calls memmove, and then we're at the mercy of the system C library. On most
systems, memmove seems to do something smart, but not on all of them: On OS X
versions 10.8 or older, this program takes a long time to run. With libstdc++
it's fast there too.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>