3 ms·
This reminds me a little bit of the C++ compilers (HP's?) that would build a database of instantiated STL types to avoid rebuilding std::basic_string<char> for
by twoodfin 9y ago
This reminds me a little bit of the C++ compilers (HP's?) that would build a database of instantiated STL types to avoid rebuilding std::basic_string<char> for every translation unit.
- colanderman 9y agoIn fact, starting with C++11, you can do this in your projects. Make a common header file with contents like this: extern template class std::basic_string<char>; extern template class std::vector<int>; // etc... and a corresponding .cc file like this: template class std::basic_string<char>; template class std::vector<int>; // etc... The template instantiations you list will be compiled in that .cc's object file. Anyone who includes the header will assume those templates were instantiated elsewhere. Net effect is the listed templates are only compiled once. (Of course this doesn't alleviate the problem of parsing the template header files over and over.) See the section on explicit instantiation here: http://en.cppreference.com/w/cpp/language/class_template http://en.cppreference.com/w/cpp/language/class_template
- corysama 9y agoNow I'm wondering what is a good way to measure my project's top 100 redundantly instantiated templates.
- colanderman 9y agofind build/ -name \*.o -exec nm -C \{} \; | egrep ' W .*<' | sort | uniq -c | sort -n is what I use. This lists all templated, implicitly instantiated, non-inlined symbols in increasing order of number of uses. Note that templates internal to libstdc++ will also show up and you may want to instantiate these too. Note that explicit instantiation won't decrease the size of your object files, only the compilation time. (By default, the linker throws away duplicate instantiations.)
- fnord123 9y agogcc has -ftime-report which is kinda opaque but still interesting.
- petters 9y agoDoesn't this prevent a lot of inlining? Or perhaps the compiler is smart enough in most cases?
- colanderman 9y agoNo change to inlining. The definitions are still in the headers and the compiler will still eagerly inline them if they are defined in the class declaration (as typical) or defined outside the class declaration and explicitly marked inline (less typical). (Whether inlining of non-trivial methods is desirable is - and in fact recently was - a discussion for another HN story.)