From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 36303385C33E; Tue, 14 Jun 2022 13:37:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 36303385C33E From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [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 Date: Tue, 14 Jun 2022 13:37:11 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: compile-time-hog, memory-hog X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 10.4 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Jun 2022 13:37:12 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105838 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jason at gcc dot gnu.org, | |redi at gcc dot gnu.org --- Comment #6 from Jakub Jelinek --- Note, for say #include #include void foo (const std::vector &); int main () { const std::vector lst =3D { "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 m= eans 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, 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::allocator (&D.37541); try { std::__cxx11::basic_string::basic_string<> (_= 4, "aahing", &D.37541); D.37581 =3D D.37581 + 32; D.37582 =3D D.37582 + -1; _5 =3D D.37581; try { std::allocator::allocator (&D.37543); try { =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20 std::__cxx11::basic_string::basic_string<> (_5, "aaliis", &D.37543); D.37581 =3D D.37581 + 32; D.37582 =3D D.37582 + -1; _6 =3D 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[] =3D { 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] =3D { 1, 3, 22, 42, 132, -12, 18, 19, 32, 0= , 25, ... }; for (size_t x =3D 0; x < count; ++x) S (&s[x], stemp[x]); or so (of course, with destruction possibility if some ctor may throw).=