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

            Bug ID: 113835
           Summary: compiling std::vector with const size in C++20 is slow
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pobrn at protonmail dot com
  Target Milestone: ---

Consider the following code:

    #include <vector>
    const std::size_t N = 1'000'000;
    std::vector<int> x(N);
    int main() {}

Then:

    $ hyperfine 'g++ -std=c++20 -O2 x.cpp' 'g++ -std=c++17 -O2 x.cpp'
    Benchmark 1: g++ -std=c++20 -O2 x.cpp
      Time (mean ± σ):      4.945 s ±  0.116 s    [User: 4.676 s, System: 0.229
s]
      Range (min … max):    4.770 s …  5.178 s    10 runs

    Benchmark 2: g++ -std=c++17 -O2 x.cpp
      Time (mean ± σ):     491.3 ms ±  24.0 ms    [User: 440.9 ms, System: 46.3
ms]
      Range (min … max):   465.6 ms … 538.0 ms    10 runs

    Summary
      g++ -std=c++17 -O2 x.cpp ran
       10.07 ± 0.55 times faster than g++ -std=c++20 -O2 x.cpp

If you remove the `const` from `N`, the runtime will be closer to C++17 levels.

`-ftime-report` suggests that "constant expression evaluation" is the reason. I
imagine this is related to C++20 making std::vector constexpr.

Reply via email to