public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/112666] New: Missed optimization: Value initialization zero-initializes members with user-defined constructor
@ 2023-11-22  8:58 paisanafc at gmail dot com
  2023-11-22 11:40 ` [Bug c++/112666] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: paisanafc at gmail dot com @ 2023-11-22  8:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112666

            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 <benchmark/benchmark.h>
#include <array>

struct A {
    A() = default;
    ~A() {
      benchmark::DoNotOptimize(c); // avoid inlining
    }
    std::array<char, 50000> member;
    char c;
};

struct B {
    B() {}  // user-defined ctor
    ~B() {
      benchmark::DoNotOptimize(c); // avoid inlining
    }
    std::array<char, 50000> 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 following
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 initialized
than B when value initialization (via {}) is used. However, my understanding 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 assembly
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 almost
as fast as B.

This potentially missed optimization in gcc is particularly nasty for structs
with large embedded storage (e.g. structs that contain C-arrays, std::arrays,
or static_vectors).

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-11-23 16:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-22  8:58 [Bug c++/112666] New: Missed optimization: Value initialization zero-initializes members with user-defined constructor paisanafc at gmail dot com
2023-11-22 11:40 ` [Bug c++/112666] " redi at gcc dot gnu.org
2023-11-23 15:05 ` paisanafc at gmail dot com
2023-11-23 15:08 ` paisanafc at gmail dot com
2023-11-23 15:37 ` paisanafc at gmail dot com
2023-11-23 16:34 ` redi at gcc dot gnu.org
2023-11-23 16:35 ` sjames at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).