3 ms·
The major reason of the awfully slow compilation (and linking) of the bigger C++ projects is all the templates in the stl or (deity forbids) boost that are used
by acqq 12y ago
The major reason of the awfully slow compilation (and linking) of the bigger C++ projects is all the templates in the stl or (deity forbids) boost that are used in every compilation unit. So I'd like to know if using this new "sugar" can make the compilation of the existing programs faster once the compiler and linker are made to recognize that sugar. Skimming through the pdf, I wasn't able to figure out from where the speed up is supposed to come. "Compiled once" in the object code sense doesn't matter for stl or boost since everything is in headers: there's simply no object code until you start to compile the cpps which include the headers. Can anybody explain what they are trying to do? Are they attempting to solve the templates problem or are they effectively just making a new form of the "lib" files?
- maxlybbert 12y agoI won't say that's been solved, but extern template is supposed to address it (well, exporting templates was supposed to address it, but that never really got off the ground). Borland came up with an approach that instantiated templates multiple times, and then weeded out duplicates. That was relatively easy to implement, so everyone else followed suit. Extern template makes it possible to make sure some instantiations are instantiated only once, so the compiler doesn't have to weed out extra instantiations (I wish I could use "instantiate" fewer times in that description, but I can't see how to pull that off).
- mattnewport 12y agoAt a minimum this can give you a much better and more modular version of precompiled headers. Because modules are guaranteed not to be affected by macros or ordering of module imports in the importing translation units you can get a modularized version of precompiled headers. Don't underestimate the cost of just reading in all that text and parsing it. Modules allow you to cache some pre-parsed AST or similar internal representation of any exported template definitions which should greatly reduce the cost of instantiating them in the importing translation unit due to savings in IO and parsing time. Importantly, importing translation units never even have to look at definitions they don't use whereas currently they have to fully parse everything that gets dragged in by the headers they include even if they only use a tiny subset of the functionality. A module enabled STL with well implemented compiler and linker support should greatly reduce build times for codebases that make heavy use of STL by simply replacing #include <header> with import std.header. The proposal eliminates all the unnecessary overhead of templates (re-parsing huge amounts of text) and leaves only the necessary / desirable overhead (instantiation from some internal representation with the specific types used). It's that final instantiation that gives templates their runtime performance advantages over other models (like C# generics) so you wouldn't want to lose that.
- lomnakkus 12y agoExactly. Essentially this reduces the algorithmic complexity of compilation which is a huge boon given that even simple programs these days result in the (repeated) parsing of millions of lines of code. Instantiation of templates obviously won't change, but recursive traversal and repeated re-parsing of includes will be completely unnecessary as the compiler can trivially[1] cache the intermediate version. Which is as it should have been from the start, but then hindsight is 20-20. It's really an elegant solution considering the constraints they're working under. [1] Preprocessor macros don't leak from includers to includees, so headers become independent of each other.