https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128902
Bug ID: 128902
Summary: constant evaluation of std::vector<T>(n) for trivial T
constructs and destroys each element through calls:
4.5 times the cost of new T[n]()
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: compile-time-hog
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: filip.sajdak at gmail dot com
Target Milestone: ---
Host: aarch64-unknown-linux-gnu
Target: aarch64-unknown-linux-gnu
Build: aarch64-unknown-linux-gnu
Created attachment 66239
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66239&action=edit
experiment (LLM-generated, reference only, NOT a patch submission), against
releases/gcc-16.2.0
In constant evaluation std::vector<T>(n) for a trivial T value-initialises and
later destroys each element through a chain of calls: about 4.5 times the cost
of new T[n]().
Seen on 16.2.0 and on trunk 9eb44775b9 (2026-11-19), aarch64-unknown-linux-gnu,
configured with --enable-languages=c,c++ --disable-bootstrap --disable-multilib
--enable-checking=release.
Reproducer:
#include <vector>
#include <cstddef>
consteval std::size_t c() { constexpr std::size_t n = 20001;
std::vector<unsigned char> v(n); return v.size(); }
constexpr auto r = c();
int main() { return r == 0; }
The twin has n = 1. The difference divided by 20000 is the cost of one element.
g++ -std=c++26 -fsyntax-only d_vector_20001.cpp
User-space instructions of cc1plus, least of three runs:
Compiler and library One
element of vector<unsigned char>(n)
16.2.0 86.3
k
trunk 9eb44775 87.2
k
For comparison, 16.2.0: new unsigned char[n]() and delete[] 19.0
k
16.2.0, one array placement new over the range 53.4
k
16.2.0, a placement new per element, without _Construct and construct_at 67.8
k
16.2.0, no destroy loop for a trivially destructible type 53.6
k
16.2.0, the array placement new and no destroy loop 19.3
k
What happens:
There are two loops of about equal cost.
1. __uninitialized_default_n takes __uninitialized_default_n_1<false> in
constant evaluation: per element std::_Construct(std::addressof(*__first)),
then construct_at, then placement new.
2. _Destroy(first, last) in bits/stl_construct.h walks a trivially destructible
range in constant evaluation too, calling destroy_at on every element.
The constant evaluator charges each call several thousand instructions, so the
call chains dominate.
The experiment:
(The experiment is attached as a diff against releases/gcc-16.2.0. It was
written with the help of an LLM, so with regard to the GCC AI policy it is NOT
offered as a patch submission: it is an analysis, and a measure of what a fix
can gain.)
The change in stl_uninitialized.h value-initialises the whole range with one
array placement new.
That is only valid where the range is a whole array object, as in vector(n)
right after allocation. It is not valid for resize into the middle of a larger
allocation, so a real patch needs a narrower path than this experiment's
is_pointer and __is_trivial test. GCC accepted it in every test below, which
may itself be worth checking.
The change in stl_construct.h drops the destroy loop for trivially destructible
types. That loses one diagnosis: a read through a saved pointer after clear()
then compiles, where 16.2.0 says "accesses an object outside its lifetime".
After pop_back() the read is still diagnosed. This may be why the loop exists.
Tests:
Run
Before After
libstdc++ conformance.exp on 21_strings, 23_containers/vector,
20_util/specialized_algorithms 1478 passes, 14 expected failures, 39
unsupported the same
What it is worth:
For a file of a C++26 UI library that uses few byte vectors, 1.0 % (54.09 G to
53.57 G).
The cost matters for code that decodes images or fonts at compile time, where
vectors of hundreds of KB are common.
Not checked:
- Whether the destroy loop is there on purpose, and which tests rely on it.
- Types that are trivial but not unsigned char, and vector<bool>.
- resize, insert and assign, which take other paths.
- Trunk: the experiment was built on 16.2.0 only. Trunk's allocate_at_least
change (acfdad706d) may give a capacity above n, which would make the array
placement new invalid even for vector(n).
This report was prepared with AI assistance (analysis and the experiment). The
builds, the measurements and the test runs were done on a machine of mine.