Bug 122913 - [16/17 Regression] pr12392.cpp -Ofast compile time went up 15% due to switching to -std=gnu++20 by default
Summary: [16/17 Regression] pr12392.cpp -Ofast compile time went up 15% due to switchi...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 16.0
: P3 normal
Target Milestone: 16.3
Assignee: Not yet assigned to anyone
URL:
Keywords: compile-time-hog
Depends on:
Blocks:
 
Reported: 2025-11-30 00:51 UTC by Drea Pinski
Modified: 2026-08-07 08:01 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2026-02-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Drea Pinski 2025-11-30 00:51:20 UTC
With a --enable-checking=yes build we get:

```
[apinski@xeond2 gcc]$ time ~/upstream-gcc/bin/gcc -std=c++20  t.cc -Ofast -fno-checking -S

real    0m59.201s
user    0m58.610s
sys     0m0.405s
[apinski@xeond2 gcc]$ time ~/upstream-gcc/bin/gcc -std=c++17  t.cc -Ofast -fno-checking -S

real    0m35.656s
user    0m35.249s
sys     0m0.295s
```

The problem does not look like the C++ parsing time changes:
std=c++20:
```
 phase setup                        :   0.01 (  0%)  1766k (  0%)
 phase parsing                      :   1.54 (  3%)   151M ( 21%)
 phase lang. deferred               :   0.23 (  0%)    17M (  2%)

```
vs -std=c++17:
```
 phase setup                        :   0.01 (  0%)  1764k (  0%)
 phase parsing                      :   1.25 (  3%)   116M ( 24%)
 phase lang. deferred               :   0.17 (  0%)    16M (  3%)
```

The biggest difference is:
std=c++20:
```
 PRE                                :  12.62 ( 21%)  2437k (  0%)
```
vs -std=c++17:
```
 PRE                                :   2.84 (  8%)  1510k (  0%)
```


So this looks like a difference in the IR going through the compiler which is causing the difference. I don't know if it is due to libstdc++ being different between 17 and 20 or the front-end producing different IR.
https://lnt.opensuse.org/db_default/v4/CPP/graph?plot.0=174.585.8


I also noticed leela_r at `-O2 -flto` lost the small speedup which was gained from my cleanupcfg patches too from moving to C++20 too:
https://lnt.opensuse.org/db_default/v4/SPEC/graph?plot.0=298.397.0
I don't know if this is related though.
Comment 1 Drea Pinski 2025-11-30 00:54:37 UTC
Note `-O0 -g` compile time seems faster too:
https://lnt.opensuse.org/db_default/v4/CPP/graph?plot.0=468.585.8

Which makes me wonder what IR changes caused both a decrease in compile time at -O0 (and -O2) but an increase at -Ofast.
Comment 2 Drea Pinski 2025-11-30 01:20:57 UTC
Ok, adding:
```
#include <bits/c++config.h>
#undef _GLIBCXX_EXTERN_TEMPLATE
#define _GLIBCXX_EXTERN_TEMPLATE 0
```
at the begining of the file caues -std=c++17 just become as slow as c++20.
Comment 3 Sam James 2025-12-08 13:14:04 UTC
IMO it should be a P1 until evaluated more, even if it gets downgraded later.
Comment 4 Drea Pinski 2026-02-01 22:14:15 UTC
>I also noticed leela_r at `-O2 -flto` lost the small speedup which was gained from my cleanupcfg patches too from moving to C++20 too:

That was fixed either by the ch improvement or final_cleanupcfg/make_forwarders_with_degenerate_phis improvements.


So it is just the compile time problem left which did have a slight improvement due to ch related patches.
Comment 5 Richard Biener 2026-02-06 14:24:38 UTC
Confirmed.

But:

> ~/install/gcc-16/usr/local/bin/g++ -S t.C -std=c++20 -Ofast -fno-checking -E -dD | grep _GLIBCXX_EXTERN_TEMPLATE
#define _GLIBCXX_EXTERN_TEMPLATE 1
> ~/install/gcc-16/usr/local/bin/g++ -S t.C -std=c++17 -Ofast -fno-checking -E -dD | grep _GLIBCXX_EXTERN_TEMPLATE
#define _GLIBCXX_EXTERN_TEMPLATE 1

so not sure what comment#2 wants to tell us?

C++20:

   text    data     bss     dec     hex filename
 796421       0       0  796421   c2705 t.o

vs. C++17:

   text    data     bss     dec     hex filename
 571556       0       0  571556   8b8a4 t.o

so there's also 72% more code generated.

-fopt-info differs a lot, the only thing I can say that C++20 optimizes a lot less, 16894 "optimized" vs. 21277 "optimized" using C++17.  I see substantial
less inlining happening.

> nm t.o.C++20 | wc -l
267
> nm t.o.C++17 | wc -l
264

Are there possibly new "patterns" with C++20 we've not yet seen and thus
fail to optimize?
Comment 6 Drea Pinski 2026-03-25 01:17:46 UTC
(In reply to Sam James from comment #3)
> IMO it should be a P1 until evaluated more, even if it gets downgraded later.

I think it should be re-evaluated. The problem is definitely not fixable for GCC 16. And the leela_r mis-optimization has been fixed already.

(In reply to Richard Biener from comment #5)

>Are there possibly new "patterns" with C++20 we've not yet seen and thus
fail to optimize?

With C++20, things become constexpr (more); e.g. std::string is almost all constexpr so it gets instantiated now. With C++17 they don't have as std::string is declared with `extern template`. So without the `extern template` you get similar behavior between C++17 and the behavior now in C++20.
I noticed gnu_inline was added to the std::exception methods (r16-6547-gf8c32184b85ce7). Maybe it needs to be added to std::string too; I don't know.
Comment 7 Richard Biener 2026-04-22 07:26:46 UTC
Some data points on x86_64-linux, both GCC 15 and GCC 16 build with LTO/PGO and release checking.  GCC 15 is 15.2.0, GCC 16 is r16-8711.

> /usr/bin/time g++-15 -S -std=gnu++17 pr12392.cpp -Ofast 
10.68user 0.11system 0:10.80elapsed 99%CPU (0avgtext+0avgdata 492208maxresident)k
0inputs+6712outputs (0major+73968minor)pagefaults 0swaps

> /usr/bin/time g++-15 -S -std=gnu++20 pr12392.cpp -Ofast 
16.71user 0.18system 0:16.94elapsed 99%CPU (0avgtext+0avgdata 763820maxresident)k
0inputs+7392outputs (0major+86662minor)pagefaults 0swaps

> /usr/bin/time g++-16 -S -std=gnu++17 pr12392.cpp -Ofast 
11.33user 0.15system 0:11.49elapsed 99%CPU (0avgtext+0avgdata 481908maxresident)k
0inputs+6824outputs (0major+80575minor)pagefaults 0swaps

> /usr/bin/time g++-16 -S -std=gnu++20 pr12392.cpp -Ofast 
13.16user 0.13system 0:13.35elapsed 99%CPU (0avgtext+0avgdata 531888maxresident)k
0inputs+7024outputs (0major+72086minor)pagefaults 0swaps

while switching the default from -std=gnu++17 to -std=gnu++20 results in
a perceived compile-time regression, the actual -std=gnu++20 compile-time
improved between GCC 15 and GCC 16 (and the -std=gnu++17 compile-time
regressed a bit).

The GCC 16 figure for -std=gnu++17 -> -std=gnu++20 is "just" 15%.

GCC 14.3.0 and GCC 13.4.0 numbers:

> /usr/bin/time g++-14 -S -std=gnu++17 pr12392.cpp -Ofast 
9.41user 0.14system 0:09.63elapsed 99%CPU (0avgtext+0avgdata 482548maxresident)k
82632inputs+6536outputs (34major+74558minor)pagefaults 0swaps

> /usr/bin/time g++-14 -S -std=gnu++20 pr12392.cpp -Ofast 
10.55user 0.13system 0:10.69elapsed 99%CPU (0avgtext+0avgdata 494552maxresident)k
352inputs+6608outputs (0major+83594minor)pagefaults 0swaps

> /usr/bin/time g++-13 -S -std=gnu++17 pr12392.cpp -Ofast 
10.87user 0.15system 0:11.04elapsed 99%CPU (0avgtext+0avgdata 432152maxresident)k
69816inputs+6504outputs (23major+81221minor)pagefaults 0swaps

> /usr/bin/time g++-13 -S -std=gnu++20 pr12392.cpp -Ofast 
10.38user 0.14system 0:10.53elapsed 99%CPU (0avgtext+0avgdata 447148maxresident)k
384inputs+6912outputs (0major+72452minor)pagefaults 0swaps


I'm keeping this as "regression", but I'm classifying this towards the standard library.  Looking at above numbers the C++20 compile time significantly
regressed from 14 to 15 and improved again a bit in 16, so I'd make it
a 15/16 regression for C++20 mode compile-time speed, but that would be
a different bug I guess.
Comment 8 Jonathan Wakely 2026-04-22 11:20:43 UTC
(In reply to Richard Biener from comment #5)
> But:
> 
> > ~/install/gcc-16/usr/local/bin/g++ -S t.C -std=c++20 -Ofast -fno-checking -E -dD | grep _GLIBCXX_EXTERN_TEMPLATE
> #define _GLIBCXX_EXTERN_TEMPLATE 1
> > ~/install/gcc-16/usr/local/bin/g++ -S t.C -std=c++17 -Ofast -fno-checking -E -dD | grep _GLIBCXX_EXTERN_TEMPLATE
> #define _GLIBCXX_EXTERN_TEMPLATE 1
> 
> so not sure what comment#2 wants to tell us?

The std::string extern templates are guarded by:

# if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0

which is why -std=c++20 makes a difference.

We changed that on trunk with r16-8510-g395e5cef29d935 so that it's now:

# if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
Comment 9 Jonathan Wakely 2026-04-22 11:24:57 UTC
I expect that the reason the difference is now 15% not 66% is because of r16-8510-g395e5cef29d935 and I expect that the remaining 15% is just because the standard library is much, much larger in C++20 (ranges, and range algos, and format, new member functions in std::string and std::vector). I'm surprised it's only 15%!
Comment 10 Richard Biener 2026-04-30 08:33:18 UTC
GCC 16.1 is being released, retargeting bugs to GCC 16.2
Comment 11 Jakub Jelinek 2026-08-07 08:01:16 UTC
GCC 16.2 is being released, retargeting bugs to GCC 16.3.