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

            Bug ID: 92385
           Summary: extremely long and memory intensive compilation for
                    brace construction of array member
           Product: gcc
           Version: 7.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: edquist at cs dot wisc.edu
  Target Milestone: ---

Created attachment 47178
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47178&action=edit
preprocessed source that exhibits bad compilation performance

Greetings!

I stumbled upon strange compiler-performance behavior when initializing an
array member of non-POD type in a struct constructor with the (C++11)
brace-initialization syntax, rather than with parens.

For example, if you have the following types:

        struct item {
                int i;
                item();
        };

        struct item_array {
                item a[SIZE];
                item_array();
        };

I am referring to defining the item_array constructor as:

        item_array::item_array() : a{} {}

Instead of:

        item_array::item_array() : a() {}


In the case with the brace initializer, especially if SIZE is non-trivial (eg,
100s of thousands), the compiler takes an inordinately long time and much
memory to compile a trivial piece of code, where with the parens instead it
compiles instantly and with very little memory used.

I've boiled it down to a very simple program, which does not #include any
headers.  The (not attached) source file "good.cpp" uses the parens, and
compiles instantly, where the (not attached) "bad.cpp", which differs only in
that it uses brace initialization rather than parens, takes 58 seconds to
compile, with the max resident set size of g++ at nearly 3GB.  (This is with
SIZE=512*1024)

(I guess I can only attach a single file, and that is supposed to be the
preprocessed "bad.ii", so that's the one attached.)

If I increase the array length from 512k to 1024k (which has 4MB total size,
with sizeof (int) == 4), then g++ runs furiously for some time and eventually
crashes.

Again, with parens instead of braces, the problem goes away.  But I have no
idea why this would be, as I thought the behavior would be equivalent.


Is this (kind of terrible) performance/behavior expected?

Thanks!
Carl


Bug report details:


Compile with:

        g++ -O3 -Wall -std=c++11  bad.cpp -o bad

I've also tried compiling with and without the options mentioned on the
"Reporting Bugs" page (-Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fsanitize=undefined) as well as with and
without optimization (-O3), none of which cause the bad compilation performance
to go away.

When it does compile though, it compiles without any output, even with "-Wall
-Wextra".

If I collect the "time" and rusage data from the above compilation step, I get:

        rtime: 58.421
        utime: 57.607
        stime: 0.798
        maxrss: 2925112k


Compared to compiling "good.cpp" (which only changes "a{}" to "a()"), I get:

        rtime: 0.076
        utime: 0.047
        stime: 0.007
        maxrss: 23120k


I've also compiled with "-save-temps" and am attaching the requested (*.i*)
preprocessed file, which is "bad.ii".


Version detail:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

Reply via email to