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

            Bug ID: 100466
           Summary: compilation of assignment from initialization list to
                    std::array<T, N> with non-trivial constructor of T is
                    very slow
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xry111 at mengyan1223 dot wang
  Target Milestone: ---

Code:

#include <array>

struct t
{
        int a;
        t() : a(0) {}
};

std::array<t, X> g;

int main()
{
        g = {};
}

$ for i in 10 100 1000 10000 100000; do
>   time g++ t.cc -DX=$i
> done
g++ t.cc -DX=$i  0.74s user 0.06s system 99% cpu 0.798 total
g++ t.cc -DX=$i  0.74s user 0.05s system 99% cpu 0.794 total
g++ t.cc -DX=$i  0.78s user 0.06s system 99% cpu 0.833 total
g++ t.cc -DX=$i  1.08s user 0.08s system 99% cpu 1.167 total
g++ t.cc -DX=$i  4.55s user 0.25s system 99% cpu 4.809 total

With -O2, it's much worse:

$ for i in 10 100 1000 10000 100000; do
>   time g++ t.cc -DX=$i -O2
> done
g++ t.cc -DX=$i -O2  0.07s user 0.01s system 99% cpu 0.089 total
g++ t.cc -DX=$i -O2  0.08s user 0.01s system 99% cpu 0.087 total
g++ t.cc -DX=$i -O2  0.49s user 0.01s system 99% cpu 0.503 total
g++ t.cc -DX=$i -O2  6.34s user 0.03s system 99% cpu 6.376 total
g++ t.cc -DX=$i -O2  68.91s user 0.18s system 99% cpu 1:09.09 total

And the resulted code seems "unrolling a loop" too excessively:

  401040:       48 c7 05 95 aa 0e 00    movq   $0x0,0xeaa95(%rip)        #
4ebae0 <g>
  401047:       00 00 00 00 
  40104b:       31 c0                   xor    %eax,%eax
  40104d:       48 c7 05 90 aa 0e 00    movq   $0x0,0xeaa90(%rip)        #
4ebae8 <g+0x8>
  401054:       00 00 00 00 
  401058:       48 c7 05 8d aa 0e 00    movq   $0x0,0xeaa8d(%rip)        #
4ebaf0 <g+0x10>
  40105f:       00 00 00 00 
  401063:       48 c7 05 8a aa 0e 00    movq   $0x0,0xeaa8a(%rip)        #
4ebaf8 <g+0x18>
  40106a:       00 00 00 00 
  ... ...
  4874a7:       48 c7 05 a6 60 0c 00    movq   $0x0,0xc60a6(%rip)        #
54d558 <g+0x61a78>
  4874ae:       00 00 00 00

Reply via email to