<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 --- - raw_svector_ostream will heap allocate without need"
href="https://llvm.org/bugs/show_bug.cgi?id=23395">23395</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>raw_svector_ostream will heap allocate without need
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</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>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>yaron.keren@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>chandlerc@gmail.com, chisophugis@gmail.com, dexonsmith@apple.com, llvmbugs@cs.uiuc.edu, rafael.espindola@gmail.com
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>The code line
OS.reserve(OS.size() + 64);
called from raw_svector_ostream::write_impl called from
raw_svector_ostream::~raw_svector_ostream will heap allocate without need. The
issue is already documented:
raw_svector_ostream::~raw_svector_ostream() {
// FIXME: Prevent resizing during this flush().
flush();
}
And a solution is provided in raw_svector_ostream::init():
// Set up the initial external buffer. We make sure that the buffer has at
// least 128 bytes free; raw_ostream itself only requires 64, but we want to
// make sure that we don't grow the buffer unnecessarily on destruction (when
// the data is flushed). See the FIXME below.
OS.reserve(OS.size() + 128);
This solution may be worse than the problem. In total:
* If the SmallString was less than 128 bytes init() will always(!) heap
allocate.
* If it's more or equal to 128 bytes but upon destruction less than 64 bytes
are left unused it will heap allocate for no reason.
A careful programmer who did size the SmallString to match its use + small
reserve will find either of these heap allocations surprising, negating the
SmallString advantage and then paying memcpy cost.</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>