Tweak vector::_M_realloc_insert for code size
Marc Glisse
marc.glisse@inria.fr
Sat Nov 11 18:14:00 GMT 2017
Hello,
operator new can clobber memory, it is hard to teach the compiler
otherwise since it is replaceable. Here I cache a couple values before the
call to the allocator. I checked the result on this simple example:
#include <vector>
void f(std::vector<int>&v){ v.push_back(0); }
The patch does not affect the fast path where no reallocation is needed.
Compiling with -O3 (everything gets inlined into f), I see a nice decrease
in code size on x86_64
$ size old.o new.o
text data bss dec hex filename
462 0 0 462 1ce old.o
376 0 0 376 178 new.o
Even at -O2 where _M_realloc_insert is not inlined, I get a slight
decrease in code size (490 -> 470). On x86, that's 531 -> 519 (465 ->
387 at -O3).
I'm not going to modify every function like that, I just happened to be
looking at this example for other reasons, and the size gain is larger
than I expected, so I am posting the patch.
Bootstrap+regtest on powerpc64le-unknown-linux-gnu.
2017-11-13 Marc Glisse <marc.glisse@inria.fr>
* include/bits/vector.tcc (vector::_M_realloc_insert): Cache old
values before the allocation.
--
Marc Glisse
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vector.patch
Type: text/x-diff
Size: 2927 bytes
Desc:
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20171111/b70c0d0e/attachment.bin>
More information about the Libstdc++
mailing list