1

Migrating from C++11 to C++14, I have a source file that usually compiles in ~20s in C++11 mode, but when compiling the same file in C++14 mode the compile time increase to ~340s. That's an increase of about 17 times. The size of the generated object code doubles.

So the clarify my question, I'm trying to understand both what in the code and why the compilers takes that much longer for the same code by changing the -std=c++11/c++14 flag for g++.

To make a fair comparison ran the pre-processor over the code (in C++11 mode) and compiled the output with both the C++11 and C++14 flags. E.g.,

g++ -E -std=c++11 -g -O2 -o spaghetti.E spaghetti.cpp
g++ -c -std=c++11 -g -O2 -o spaghetti-11.o -x c++ spaghetti.E
g++ -c -std=c++14 -g -O2 -o spaghetti-14.o -x c++ spaghetti.E

The file in question does have lots of fixed size arrays of objects in template form like

template<typename T, std::size_t N>
struct Object {
    std::array<T,N> storage{};
    std::vector<T> counters;
};

Symbols involving Object do double when compiled with C++14 over C++11 (among others).

So, what motivates the compiler to take 17 times longer to compile the same file? And what should change to reduce this?

I should note that if I change the Object implementation

template<typename T, std::size_t N>
struct Object {
    std::vector<T> storage{};  // was std::array above, N used in other members
    std::vector<T> counters;
};

It will compile quickly under both C++11 and C++14.

Bo R
  • 2,334
  • 1
  • 9
  • 17
  • Probably worth posting this as a bug report to the gcc... mailing list? I have no idea how to get in contact with those people. – JohnFilleau Dec 17 '21 at 16:36
  • You could also compile gcc in debug mode and step through your compilation ;) – JohnFilleau Dec 17 '21 at 16:37
  • I wonder if this may be due to aggregate initialization of the `storage` array: https://stackoverflow.com/q/37260097/260313 – rturrado Dec 17 '21 at 16:50
  • Please provide a [mcve] and the GCC version in question. – rustyx Dec 17 '21 at 17:04
  • @rustyx, file too big and proprietary for quick reduction. Tried both g++ 11.3.0 and 12.0.0 20211212 (experimental) with the same similar hit to compile time. – Bo R Dec 17 '21 at 17:18
  • I always knew that `spaghetti` code could be hard to modernize. – prapin Dec 17 '21 at 19:28

1 Answers1

5

Seems like this is known bug in gcc but isn't getting much traction.
Did you try to switch over to clang++?

PS from comment: Removing the {} behind the storage{} and manually initializing works.

  • @oilver, yes, tried with clang++ (v12.0.1) and did not see this problem here. – Bo R Dec 17 '21 at 17:09
  • Maybe you can initialize it in a proper constructor and remove the `{}` behind the `storage{}`. This at least fixed the related issue for me https://stackoverflow.com/questions/37260097/stdarray-with-aggregate-initialization-on-g-generates-huge-code – Oliver Tale-Yazdi Dec 17 '21 at 17:23
  • 2
    Thank you, yes, compile time problem was gone when removing the `{}` after `storage`. – Bo R Dec 17 '21 at 17:32