https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115832
Bug ID: 115832 Summary: Very slow compilation with certain flags and std::sort Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: faisal.irl2004 at gmail dot com Target Milestone: --- Created attachment 58611 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58611&action=edit compiler output of g++ -v C++ compilation time seems to blow up unreasonably when compiling with the flags: -D_GLIBCXX_DEBUG -fsanitize=undefined -O2 In particular, calling something even simple like std::sort() will blow up compilation time by 5x with these flags. Using any proper subset of these flags seems to speed up compilation time by at least 5x as well. Using precompiled headers doesn't help with compilation time much either. I have provided a minimal test case to illustrate the problem: A.cpp: #include <vector> #include <algorithm> #define MAXN (int) 10 /* time g++ A.cpp -D_GLIBCXX_DEBUG -fsanitize=undefined -O2 Removing any one of these 3 flags speeds up compilation by atleast 5x */ int main() { std::vector<int> arr(MAXN); for (int i = 0; i < MAXN; i++) { arr[i] = i + 10; } // std::sort(arr.begin(), arr.end()); /* Uncomment this to blow up compile time */ return arr[0]; } Without std::sort time g++ A.cpp -D_GLIBCXX_DEBUG -fsanitize=undefined -O2 real 0m0.308s user 0m0.272s sys 0m0.035s With std::sort time g++ A.cpp -D_GLIBCXX_DEBUG -fsanitize=undefined -O2 real 0m4.175s user 0m4.062s sys 0m0.108s Using precompiled headers also only brings it down to ~4s. I have attached the preprocessed file and the compiler output of: g++ -v -save-temps A.cpp -D_GLIBCXX_DEBUG -fsanitize=undefined -O2 &> compiler_output (This is run with std::sort present)