[Bug c++/105838] [10/11/12/13 Regression] g++ 12.1.0 runs out of memory or time when building const std::vector of std::strings
jakub at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Tue Jun 14 13:37:11 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105838
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jason at gcc dot gnu.org,
| |redi at gcc dot gnu.org
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Note, for say
#include <string>
#include <vector>
void foo (const std::vector<std::string> &);
int main ()
{
const std::vector<std::string> lst = {
"aahing", "aaliis", "aarrgh", "abacas", "abacus", "abakas", "abamps",
"abands", "abased", "abaser", "abases", "abasia" };
foo (lst);
}
one gets terrible code from both g++ and clang++, in both cases it is serial
code calling many std::string ctors with the string literal arguments
that perhaps later on are inlined. Over 21000 times in a row. That also means
over 21000 memory allocations etc.
For your game, the obvious first question would be if you really need
std::vector of std::string in this case and if a normal array of const char *
strings wouldn't be better, that can be initialized at compile time.
Or, if you really need std::vector<std::string>, if it wouldn't be better to
use array of const char * and build the
vector from it (sizeof (arr) / sizeof (arr[0]) to reserve that many elts in the
vector, then a loop that will construct
the std::string objects and move them into the list).
On the compiler side, a question is if we shouldn't detect such kind of
initializers and if they have over some param determined number of elements
which have the same type / kind (or at least a large sequence of such), don't
emit those
std::allocator<char>::allocator (&D.37541);
try
{
std::__cxx11::basic_string<char>::basic_string<> (_4,
"aahing", &D.37541);
D.37581 = D.37581 + 32;
D.37582 = D.37582 + -1;
_5 = D.37581;
try
{
std::allocator<char>::allocator (&D.37543);
try
{
std::__cxx11::basic_string<char>::basic_string<> (_5, "aaliis", &D.37543);
D.37581 = D.37581 + 32;
D.37582 = D.37582 + -1;
_6 = D.37581;
try
{
...
but a loop. Doesn't have to be just for the STL types, if we have
struct S { S (int); ... };
const S s[] = { 1, 3, 22, 42, 132, -12, 18, 19, 32, 0, 25, ... };
then again there should be some upper limit over which we'd just emit:
const S s[count];
static const int stemp[count] = { 1, 3, 22, 42, 132, -12, 18, 19, 32, 0, 25,
... };
for (size_t x = 0; x < count; ++x) S (&s[x], stemp[x]);
or so (of course, with destruction possibility if some ctor may throw).
More information about the Gcc-bugs
mailing list