Reputation: 26468
Suppose the following template class is heavily used in a project with mostly int as typename and linker speed is noticeably slower since the introduction of this class.
template <typename T>
class MyClass
{
void Print()
{
std::cout << m_tValue << std::endl;;
}
T m_tValue;
}
Will defining a class specialization benefit compilation speed? eg.
template <>
class MyClass<int>
{
void Print()
{
std::cout << m_tValue << std::endl;;
}
int m_tValue;
}
Or does explicit instantiation offers a better solution? eg.
template class MyClass<int>;
Upvotes: 2
Views: 1085
Reputation: 44623
We found that template can greatly increase the compilation and link time. One of the problem is that each file that include the header declaring the template will have to parse it, check its validity, and if it is used by the compilation unit, the generated object file will contains the code, that will later be removed by the linker (if used by more than one file).
In our project, we had some large template, that were included in almost every file, but of which only two instantiation ever existed. We greatly improved the compilation time by using explicit instantiation, and separation of the template code in multiple files.
For your exemple, that would give you :
// MyClass.h
template < typename T >
class MyClass
{
void Print();
T m_tValue;
}
// MyClass.inl
#ifdef MY_CLASS_METHODS_ARE_NOT_INLINE
# define MY_CLASS_INLINE
#else
# define MY_CLASS_INLINE inline
#endif
template < typename T >
MY_CLASS_INLINE void MyClass< T >::Print()
{
std::cout << m_tValue << std::endl;
}
#undef MY_CLASS_INLINE
// MyClass.cpp
#include "MyClass.h"
#define MY_CLASS_METHODS_ARE_NOT_INLINE
#include "MyClass.inl"
template class MyClass< int >;
template void MyClass< int >::Print();
#undef MY_CLASS_METHODS_ARE_NOT_INLINE
Upvotes: 1
Reputation: 3442
in C++0x you will be able to use extern templates so that instantiation of templates happens only once just like extern variables.
This should help in compile times since the compiler would not have to instantiate the template with its arguments every time it sees it.
I haven't yet figured out exactly how I will be using this feature in my own projects, but anything that can help compile times is a plus for me.
http://www2.research.att.com/~bs/C++0xFAQ.html#extern-templates
Upvotes: 2
Reputation: 26060
It strictly depends on your toolchain (in this case at least linker and compiler, maybe more).
Just try, but be aware that results may vary a lot by changing even a single piece of your toolchain.
Upvotes: 0
Reputation: 48176
By making the program more complex - longer, and requiring more disambiguation at a call-site - in general, a compiler will become slower not faster.
However, it's not likely to be a big issue unless you're automatically generating huge numbers of such specializations. Also, performance varies between compilers - you can always test it yourself (I expect the difference to be too small to be apparent without rigorous tests and a large number of test runs).
It's possible linker speed is reduced since the template is being declared "inline" (though not necessarily inlined) in all compilation modules in which it's referenced. When that occurs, you're duplicating a method all over the place and the linker gets more work. By comparison, a "normal" function with just a declaration in a header and the definition in just one spot will result in just one compiled function and thus fewer functions for the linker (and the compiler). Depending on options such as link-time code generation, this could matter quite a bit or hardly at all.
Upvotes: 1