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

            Bug ID: 118809
           Summary: Excessive memory usage with global
                    std::vector<std::vector<...>> in C++20 mode
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daklishch at gmail dot com
  Target Milestone: ---

Created attachment 60434
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60434&action=edit
Minified reproducer

We've been running a competitive programming contest where there are tight
limits on compile time time and memory usage (10s/512Mb) to protect testing
system from DoS. During the contest, I've noticed that one of the submissions
that wasn't doing anything extraordinary is getting the "compile time memory
limit exceeded" verdict. The culprit was the following global variable:
`std::vector r(1, std::vector<int>(1e5));`. Notice how GCC uses up almost 440Mb
when trying to compile translation unit with just this single vector:

$ cat orig.cpp
#include <vector>
std::vector r(1, std::vector<int>(1e5));
$ /bin/time ~/gcc-15-trunk/bin/g++ -c -std=c++20 orig.cpp -o /dev/null
2.90user 0.04system 0:02.94elapsed 99%CPU (0avgtext+0avgdata
437876maxresident)k
0inputs+0outputs (0major+7005minor)pagefaults 0swaps

compared to ~70Mb with C++17:

$ /bin/time ~/gcc-15-trunk/bin/g++ -c -std=c++17 orig.cpp -o /dev/null
0.15user 0.01system 0:00.17elapsed 99%CPU (0avgtext+0avgdata 66716maxresident)k
0inputs+0outputs (0major+3475minor)pagefaults 0swaps

What makes me believe this is a bug and not a consequence of constexpr changes
in C++20 is the fact that commenting out lines 77-78 (check for allocation size
overflow) in the minified reproducer drops memory usage from ~400Mb to ~60Mb.
And to that extend, doing almost any further non-trivial manual inlining in the
reproducer makes peak memory usage go down significantly.

Reply via email to