optimize std::vector::push_back
commit1d82fc2e6824bf83159389729c31a942f7b91b04
authorJan Hubicka <jh@suse.cz>
Tue, 21 Nov 2023 14:17:16 +0000 (21 15:17 +0100)
committerJan Hubicka <jh@suse.cz>
Tue, 21 Nov 2023 14:17:16 +0000 (21 15:17 +0100)
tree1b94e5afa7c06e1b43e519e7a34a04a1a45724e0
parent1250858ac9c1426da06116823bd3e1fca64c7d56
optimize std::vector::push_back

this patch speeds up the push_back at -O3 significantly by making the
reallocation to be inlined by default.  _M_realloc_insert is general
insertion that takes iterator pointing to location where the value
should be inserted.  As such it contains code to move other entries around
that is quite large.

Since appending to the end of array is common operation, I think we should
have specialized code for that.  Sadly it is really hard to work out this
from IPA passes, since we basically care whether the iterator points to
the same place as the end pointer, which are both passed by reference.
This is inter-procedural value numbering that is quite out of reach.

I also added extra check making it clear that the new length of the vector
is non-zero.  This saves extra conditionals.  Again it is quite hard case
since _M_check_len seem to be able to return 0 if its parameter is 0.
This never happens here, but we are not able to propagate this early nor
at IPA stage.

libstdc++-v3/ChangeLog:

PR libstdc++/110287
PR middle-end/109811
PR middle-end/109849
* include/bits/stl_vector.h (_M_realloc_append): New member function.
(push_back): Use it.
* include/bits/vector.tcc: (emplace_back): Use it.
(_M_realloc_insert): Let compiler know that new vector size is non-zero.
(_M_realloc_append): New member function.
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/bits/vector.tcc