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

            Bug ID: 82294
           Summary: Array of objects with constexpr constructors
                    initialized from space-inefficient memory image
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: headch at gmail dot com
  Target Milestone: ---

Consider the following code:

$ cat test.cpp
struct S {
        int x;

        explicit constexpr S();
};

constexpr S::S() : x{7} {}

struct T {
        S objects[256];

        explicit T();
};

T::T() {}


Compile it as follows:

$ g++ -Wall -Wextra -march=native -std=c++17 -Os -c test.cpp


Dumping the resulting object file reveals that T::T() is implemented by
memcpying (depending on the target architecture sometimes inline or sometimes
by an actual memcpy call) a kilobyte of data from .rodata into the array being
initialized. For two or three S objects that might be a good, efficient
solution. For 256 of them, a kilobyte of memory entirely filled with sevens to
serve as an initialization image is rather ridiculous compared to just using a
loop to store seven into the objects one by one. This is especially egregious
considering I was asking to optimize for size!

Making S::S() non-constexpr improves the situation, though it’s hardly an ideal
solution.

This might be related to any of #12245, #56671, #59659, #63728, #68399, or
#71165, but none of them describe quite the same problem. The first five are
about memory or CPU time usage during compilation (not about the generated
code), and #71165 is specifically about aggregate initialization and appears to
be an unrolled loop in code rather than a huge data blob.

Reply via email to