I imagine it would already help a lot if it stopped instantiating the same templates again and again and again. The mind boggles that the work by the zapcc compiler was never picked up by any mainstream compiler, despite it being considerably faster than any other C++ compiler.
C++ is bound to a lot of design decisions made for C in the 1970s when the compiler was designed for microcomputer with extreme RAM constraints.
Compile times are mostly about all the passes required. Of course, templates makes this slower, but that's only because the generated code bloats up all the other passes.
The D language for instance goes insane with metaprogramming and maintains blazingly fast compiles. But it's largely because the author decided it was worth breaking compatibility with C/C++ (he used to be a C++ compiler dev)
C++ is bound to a lot of design decisions made for C in the 1970s when the compiler was designed for microcomputer with extreme RAM constraints.
Is it?
C++20 introduces modules, which completely do away with the C model of translation units and #include files.
Compile times are mostly about all the passes required.
I would note here that each pass is, at best, O(N) in the size of its input -- if sub-linear its input should be trimmed.
There are multiple potential "big" inputs generators in C++:
#include: it is not unusual for a single .cpp file to expand to MBs of pre-processed code.
macros: some macros can expand to a large amount of code.
templates: some templates can expand to a large amount of code -- unfortunately compilers do not have an option to show post-template code.
#include is very specific to C++, and modules will nix that issue (mostly), whereas macros/templates and other code generation abilities are present in many languages.
And then there are language issues compounding the difficulties that are somewhat specific to C++:
Parsing ambiguities, with templates. Modules will alleviate a lot of the impact by avoiding parsing headers over and over.
Overload resolution. One of the most complex beast in C++, determining which function/method a symbol resolves to is costly, and has to be done again for each instantiation of a template.
Concepts may help a bit with overload resolution, when previously SFINAE was used. Maybe.
The D language for instance goes insane with metaprogramming and maintains blazingly fast compiles.
Walter Bright is a compiler developer first, and it shows in the design of the D language.
Still, there are inherent bottlenecks: metaprogramming makes it easy to "accidentally" generate the equivalent of MBs of code.
I think we need better feedback from compilers, here, to more easily pinpoint those metaprogramming accidents.
39
u/johannes1971 May 10 '20
I imagine it would already help a lot if it stopped instantiating the same templates again and again and again. The mind boggles that the work by the zapcc compiler was never picked up by any mainstream compiler, despite it being considerably faster than any other C++ compiler.
Source.