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 :
The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.
8. Which are done by compiler for templates? Explanation: The compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.
The Zapcc is the fastest compiler in our compile test.
Template compilation requires the C++ compiler to do more than traditional UNIX compilers have done. The C++ compiler must generate object code for template instances on an as-needed basis. It might share template instances among separate compilations using a template repository.
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.
This really isn't an answer to your question. It's more of a side observation.
I'm not an C++ language lawyer either , and so I could be way off base with some of the details.
But, the rough idea should be correct.
The main reason that C++ compilers take such a long time to compile template meta-programs is because of the way template meta-programs are specified.
They aren't specified directly as code that you want the compiler to run at compile time. Take the example of computing the length of a type list.
If you could write code like this:
compile_time size_t GetLength(TypeList * pTypeList) { return DoGetLength(pTypeList, 0); } compile_time size_t DoGetLength(TypeList * pTypeList, size_t currentLength) { if (pTypeList) { return DoGetLength(pTypeList->Next, ++currentLength); } else { return currentLength; } }
That was some how compiled separately from the code where it was used, and was exposed to the language via some syntax, then the compiler would be able to execute it very quickly.
It would just be a simple recursive function call.
It's possible to design language that allows those sorts of things. Most of the ones that do this (like lisp) are dynamically typed, but it is possible to do with static typing. However, it's not likely to ever be something you would see implemented in C++.
The problem in C++, however, is that the code is written like:
template <typename First, typename Second> struct TypeList { typedef First Head; typedef Second Tail; }; template <> struct ListSize<NullType> { enum { size = 0 }; }; template <typename Head, typename Tail> struct ListSize<TypeList<Head, Tail> > { enum { size = 1 + ListSize<Tail>::size }; };
In order for the compiler to "execute" the meta-program it has to:
This is much more expensive than just simply running a O(N) recursive algorithm.
The worst case would be something like O(N * M * L), with N equal to the length of the list, M being the level of scope nesting , and L being the number of symbols in each scope.
My advice would be to minimize the amount of C++ template meta-programming that you use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With