From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 14DF73858D38; Wed, 22 Nov 2023 08:58:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14DF73858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700643527; bh=UscW8sefUNBDvsVernUgPJRDM1ronLRmdwip1JPFhIg=; h=From:To:Subject:Date:From; b=Svshpwh2Ty6O1gs2Oz38+h0tr1M0seiB7qsXrzR1Nod+R/hbl5FWN2B5XyRxXZqR3 U0K2JExw6UakvAHnKJMmBPqZvgEFftf0lnsh8eM4JBjm18EIiFbrtcaFMgQpGlSLCY mUZNKOVLzXPgD+KtEKv/X5YUb+rTjLRoCy+6n+TI= From: "paisanafc at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112666] New: Missed optimization: Value initialization zero-initializes members with user-defined constructor Date: Wed, 22 Nov 2023 08:58:46 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.4.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: paisanafc at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112666 Bug ID: 112666 Summary: Missed optimization: Value initialization zero-initializes members with user-defined constructor Product: gcc Version: 11.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: paisanafc at gmail dot com Target Milestone: --- Looking for the presence of "memset" instructions in the generated assembly= , it seems that gcc is zero-initializing class members with user-defined constructors that shouldn't need to be zero-initialized. I share below the example benchmark and a godbolt link for convenience (https://godbolt.org/z/158q6sfen). I used the benchmark library as I didn't know an easy way to reproduce the instruction `benchmark::DoNotOptimize`. I hope that's ok. --- #include #include struct A { A() =3D default; ~A() { benchmark::DoNotOptimize(c); // avoid inlining } std::array member; char c; }; struct B { B() {} // user-defined ctor ~B() { benchmark::DoNotOptimize(c); // avoid inlining } std::array member; char c; }; struct C { // no user-defined ctor B b; int dummy; }; // The benchmark code: static void ACreation(benchmark::State& state) { for (auto _ : state) { A a{}; benchmark::DoNotOptimize(a); } } BENCHMARK(ACreation); static void BCreation(benchmark::State& state) { for (auto _ : state) { B b{}; benchmark::DoNotOptimize(b); } } BENCHMARK(BCreation); static void CCreation(benchmark::State& state) { for (auto _ : state) { C c{}; benchmark::DoNotOptimize(c); } } BENCHMARK(CCreation); BENCHMARK_MAIN(); --- When I run this with https://github.com/google/benchmark, I get the followi= ng results (with gcc++11.4 and above): ----------------------------------------------------- Benchmark Time CPU Iterations ----------------------------------------------------- ACreation 736 ns 736 ns 933741 BCreation 3.62 ns 3.62 ns 191180154 CCreation 755 ns 754 ns 944906 The struct "C" which is just "B" and an int is much slower at being initial= ized than B when value initialization (via {}) is used. However, my understandin= g of the C++ standard is that members with a user-defined default constructor do= not need to be zero-initialized in this situation. Looking at the godbolt assem= bly output, I see that both `A a{}` and `C c{}` generate a memset instruction, while `B b{}` doesn't. Clang, on the other hand, seems to initialize C almo= st as fast as B. This potentially missed optimization in gcc is particularly nasty for struc= ts with large embedded storage (e.g. structs that contain C-arrays, std::array= s, or static_vectors).=