[cfe-dev] yet another question about modules

Mikael Persson mikael.s.persson at gmail.com
Fri Jan 23 11:26:26 PST 2015


Hi,

> BTW: is there any other trick to force instantiation, without generating
any text?

I guess the main C++11 trick for this is to use "extern templates". I've
used extern templates quite a bit to cut down on compilation times (and
memory usage) of template-heavy code, but its impact is limited by the fact
that building the AST (within the instantiation) is still a pretty big
deal. For example, I had some code that needed about 6GB and lots of time
to compile (with GCC), and by putting some of the main template
instantiations into a separate cpp file, I would end up with about 4GB to
compile each of the two separate cpp files, in other words, 6GB turned into
4GB + 4GB, roughly, and about the same for compilation time, meaning that
this was a net loss in overall compilation costs, but with benefits like
actually being able to compile it (without overwhelming the system) and
reducing the need for recompilations (e.g., recompiling only one of the cpp
files, instead of everything). BTW, with Clang, this is significantly
faster to compile compared to GCC (1/2 of the time, and 1/3 of the memory,
approximately).

But the point is that it seems to me (I might be wrong) that even with an
existing instantiation (extern or not), the compilation time/memory needed
to deal with it is still significant.

I have one related question to the clang devs:
When you have an extern template declaration in a header file that is part
of a module, is the complete AST (or whatever else) of that template
instantiation included in the module?

> In principle, we could extend the modules system with a template
> instantiation repository to cache the results of instantiating
> templates from modules, but I don't think anyone is working on, or
> planning, such a system for Clang at the moment.

That sounds to me like automatically making all template instantiations
"extern", right? This could actually be pretty awesome. But I'm worried
about the implications for the standard rules (it looks to me like it could
be allowed). That could be part of the rules for modules, that any
instantiation encountered for a template declared/defined inside a module
would implicitly be considered "extern" and cached somewhere alongside the
module's cache.

> from my experience, it was rather the template instantiation that was
causing long build times and not the preprocessing.

That's right. Last time I ran my code through GCC with compilation
profiling enabled, it showed that template instantiations accounted for
about 98% of the compilation time. For template-heavy code, pre-processing,
parsing, AST building and all the "normal" compilation stuff is negligible.
And for code that isn't template-heavy, compilation times are rarely a big
problem.

Cheers,
Mikael.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150123/e77c3f08/attachment.html>


More information about the cfe-dev mailing list