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 eg 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.)


    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 typedef s!


    Precompiled header

    If your code changes are not in the header this might actually help with reducing the compilation time. (src)

    链接地址: http://www.djcxy.com/p/2462.html

    上一篇: 为什么GCC不能优化`std :: sqrt`?

    下一篇: 在c ++ / gcc中优化模板编译时间