<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94360>94360</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc++] std::vector::push_back results in absurd amount of code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
gerben-stavenga
</td>
</tr>
</table>
<pre>
From https://godbolt.org/z/18oPj9cWs it's obvious that a massive amount of code is generated for each invocation of push_back. Given that push_back might very well be among the most ubiquitous used function of the c++ standard library it would make sense to invest in a more careful implementation that results in equal or faster performance and hugely reduced code size.
It seems that a bit of type erasure for relocation together with an outline fallback function would not hurt performance much, and possibly gains performance due to reduces icache pressure. Furthermore all fallback functions should be "static" member functions that take base, cap and size by value and return resized buffer by value. This prevents "this" from escaping producing superior code for local std::vector's. I have the suspicion that current implementations inline everything due to fact that adding [[noinline]] annotation on some fallbacks massively hurt performance due to aliasing analysis failing due to escaped this-pointers.
Furthermore for POD-like types the fallback could just memcpy completely eliminating any need for type-erased specialized code.
I prototyped a pmr implementation (without the overhead of an extra pointer in the struct) that produces the code commensurate with what I expect a decent implementation should produce.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxkVU2P2zgP_jXOhZjAUT4mOeTQeQd50dP2sMAeF7TE2GplyRUpT9Nfv6CSSefjkhgGLT58Pihk9n0kOjbbp2b7vMAiQ8rHnnJH8YEFZ4o9LrrkLsdTTiMMIhM36y-NOTXm1CfXpSDLlPvGnH435rTap2_fD_YfBi-NeWRI3exTYZABBRBG7TgT4JhKFEhnsMkReIaeImUUcnBOGQjtAD7OyaL4FLVwKjz826H9sYT_-5ni9cj7Wxh9PwjMlC_wQiFAV7vEHmQgGBMLlM7_LF4UTmFtVKJ9PV2LbGOeGvMELBgdZgfBdxnzBbzASyrBwYg_CJgiE0hSfMQCPupgKRNYzHQuAfw4BRopyhV8BZqJS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