https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128928
Bug ID: 128928
Summary: Quadratic compile time with many block-scope statics:
determine_local_discriminator scans a TU-wide list
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: denys.kachalenko at gmail dot com
Target Milestone: ---
Every function-scope `static` variable (and every non-lambda local class) is
registered by `determine_local_discriminator` (`cp/decl.cc`), which scans
`local_entities` linearly. `local_entities` is one list for the whole
translation unit and is not reset per function, so N function-template
instantiations that each declare a `static` take O(N^2) time. The time shows up
under `|name lookup` in `-ftime-report`.
Testcase (no headers):
```c++
// Minimal.cc
#ifndef N
#define N 10000
#endif
#ifndef STORAGE
#define STORAGE static
#endif
template<int K> int f() { STORAGE int v = K; return v; }
template<int Lo, int Hi> struct Range
{
static int run()
{
if constexpr (Hi - Lo == 1)
return f<Lo>();
else
return Range<Lo, (Lo + Hi) / 2>::run() + Range<(Lo + Hi) / 2,
Hi>::run();
}
};
int g() { return Range<0, N>::run(); }
```
```
g++ -std=c++17 -fsyntax-only -ftime-report -DN=<N> -DSTORAGE=static Minimal.cc
g++ -std=c++17 -fsyntax-only -ftime-report -DN=<N> -DSTORAGE= Minimal.cc
```
GCC 16.2.0, x86_64-w64-mingw32, median of 3 runs, seconds:
```
N static: name lookup TOTAL no static: name lookup TOTAL
10000 0.16 1.18 0.04 0.91
20000 0.71 4.05 0.07 1.38
40000 2.18 6.74 0.11 2.38
80000 9.28 16.86 0.23 4.83
```
Same on 11.5.0 and trunk 17.0.0 20261017 (N=40000: name lookup 2.01 / 2.83 s
with `static`, 0.16 / 0.14 s without), so not a regression.
`local_entities` is `GTY((deletable))` and is cleared only when the garbage
collector runs; the comment above `determine_local_discriminator` says there
are "generally very few of these in any particular function", which holds per
function but not for the whole TU.
This matters more with C++26 expansion statements: GCC 16 makes the artificial
`__for_range`, `__for_begin` and per-element `__for_iter` variables of a
constant iterating expansion statement `static`, so each `template for` over an
n-element `std::span` adds n + 2 statics. Jakub's patch "c++: Change iterating
expansion stmt not to use static for C++26" (gcc-patches, 2026-11-16) would
remove those for `-std=c++26`; the general case above would remain.
Possible direction (untested): restrict the scan to the current function, e.g.
truncate `local_entities` to its length at function start when the function
ends, or key the lookup on (`DECL_CONTEXT`, name) in a hash table.
The testcase and analysis were prepared with the help of an AI assistant; I
verified the results.
Output of `g++ -v`:
```
Using built-in specs.
COLLECT_GCC=C:\msys64\home\kacha\gcc-16.2.0-install\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/home/kacha/gcc-16.2.0-install/libexec/gcc/x86_64-w64-mingw32/16.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../src-16.2.0/configure
--prefix=/home/kacha/gcc-16.2.0-install
--with-native-system-header-dir=/ucrt64/include
--with-local-prefix=/ucrt64/local --build=x86_64-w64-mingw32
--host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --enable-languages=c,c++
--enable-threads=mcf --enable-shared --enable-static --enable-lto
--enable-plugin --enable-checking=release --disable-multilib --disable-nls
--disable-werror --disable-bootstrap --with-tune=native --with-system-zlib
--with-zstd
Thread model: mcf
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.0 (GCC)
```