Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize templates compilation time in c++/gcc

In a large project we have a lot of classes (thousands), and for each of them a special smart pointer type is defined using typedef. This smart pointer type is a template class. When I compile with "gcc -Q" I see that a lot of time is spent compiling these smart pointers for each class. That is I see smartptr<class1>::methods, then smartptr<class2>::methods... smartptr<class2000>::methods scrolling on the screen as gcc processes them.

Is there a trick to speedup this process? These classes are all the same from the smartptr point of view, no enable_if tricks, etc.

What I am trying right now:

  • maybe make a non-template base class with few common methods
  • use extern template class to reduce link symbols (and instantiation time? not sure yet)

But all of the above is not a complete solution. I wonder if there's another way to optimize compilation time, a trick to make gcc know that e.g. if it parsed smartptr once it could apply the same knowledge over and over again when seeing other specializations, because the generate code is the same.

Yes I know that it is not quite the same of course... But that's just a crazy idea.

Or maybe there're other tricks that I'm not aware of, that could speed up compilation. (Just to give the idea of what I'm talking, we could optimize another template by eliminating its static member data instantiation, which greatly reduced compilation time. This was not obvious at all.)

like image 549
queen3 Avatar asked Dec 27 '12 20:12

queen3


People also ask

Are templates resolved at compile-time?

All the template parameters are fixed+known at compile-time. If there are compiler errors due to template instantiation, they must be caught at compile-time!

Are templates instantiated at compile-time?

Templates are instantiated in the process of converting each translated translation unit into an instantiation unit. A translation unit is essentially a source file. A translated translation unit (try to say that three times fast) is the output from compilation without templates instantiated.

Are C++ templates compile-time?

7.2.The C++ compiler uses compile-time instantiation, which forces instantiations to occur when the reference to the template is being compiled.


1 Answers

Not specifically GCC, but I think that the idea of deriving smartptr from a non-template base class sounds like a good bet. A smart pointer is a good candidate for this approach because much of the code which is being repeatedly generated doesn't care that the pointer isn't void*. (I would shift as much code over as you can so that the class template does little more than cast to and from void* where necessary.)

Also, if you have thousands of classes which are relying heavily on smartptr, then nipping the problem in the bud this way should be considered first. Only when that fails would I move on to smartptr's client code (at which point, techniques for avoiding general header bloat become worth considering).

As for extern template declarations, I have no experience of these but it sounds like you would need to add an extern declaration per typedef. Notably, the opposite effect of forcing complete instantiation is performed like this:

template class smartptr<MyClass>;

I would double check that this line doesn't accompany any of your typedefs!

like image 87
John McFarlane Avatar answered Nov 15 '22 20:11

John McFarlane