Alex Rivera | Logout

Are there optimized c++ compilers for template use?

Asked 2009-02-24T15:54:39.527
24

C++ templates have been a blessing in my everyday work because of its power. But one cannot ignore the (very very very very long) compilation time that results from the heavy use of templates (hello meta-programming and Boost libraries). I have read and tried quite a lot of possibilities to manually reorganize and modify template code to make it compile as fast as possible.

Now I am wondering if there are any c++ compilers that try and minimize the needed time to interpret template classes. I might be wrong, but i feel that the compilers i do know have only added template interpretation to their previous versions.

My questions are :

  • Is c++ template code so difficult to interpret that there is not much to optimize ? (i highly doubt that)
  • Are there c++ compilers that truly optimize "c++ templates" interpretation ?
  • Are there projects to develop a new generation of c++ compilers that would optimize this ?
  • If you were to participate in such a project, what would your guidelines be ?
Edit
Report

2 Answers

14

I expect that compiling templated code will be speed up with having variadic templates / rvalue references. Today, if we want to write template code that does something at compile time, we abuse rules of the language. We create dozens of overloads and template specializations that results in what we want, but not in a way that tells the compiler our intention. So there is little to shortcut for the compiler at build-time. See Motivation for variadic templates

Are there projects to develop a new generation of c++ compilers that would optimize this ?

Yes, there is CLang which is a C Language Frontend for the LLVM Compiler infrastructure. Both CLang and LLVM are coded using C++. Among the developers of CLang is Douglas Gregor, author of several C++1x Language Proposals like variadic templates and Concepts. For reference, see this test by Douglas Gregor of clang against GCC

Here are some quick-n-dirty performance results for template instantiation in Clang and GCC 4.2. The test is very simple: measure compilation time (-fsyntax-only) for a translation unit that computes the Nth Fibonacci number via a template metaprogram. Clang appears to be scaling linearly (or close to it) with the number of instantiations. And, although you can't see it in the chart, Clang is a little over 2x faster than GCC at the beginning (Fibonacci<100>).

CLang is still in its early days but i think it's got good chances to become a great C++ Compiler.

answered 2009-02-25T01:51:21.403
0

I think templates themselves are not so complex in themselves. We'll see when concepts are introduced in c++0x, etc., but at the moment, templates are just (almost) as macros, so the real problem is not that if you have optimized c++ compilers for templates. The problem is that templates do generate such a huge amount of code when compiling, that make compilation slower.

answered 2009-02-24T16:56:41.203

Your Answer