Redundant template instantiations in different TUs are a significant contributor to build time for larger C++ projects. This can be greater contributor to build-time than actually compiling the instantiated template due to template metaprogramming libraries. Clang also has a CrossTranslationUnit interface that can load and store ASTs from a file.
I want to add an optimization that caches instantiated templates on-disk, so the instantiations aren’t repeated across TUs. Since this is a major change, I am getting consensus/feedback.
The architecture is to create an AST dump for every function or class template that serves as a cache. When a clang
job wants to instantiate a template specialization, it locks the cache for the given specialization of the templated class/function and determines if the instantiation is already present. If so, the instantiation is inserted into the AST. If not, the template is instantiated normally and saved in the cache. Ideally, this would use the existing CrossTranslationUnit functionality.
The implementation plan is:
- Add support for serializing function and class template specializations to disk and deserializing them into the AST.
- Allow a single TU to cache template instantiations across rebuilds that do not change headers.
- Allow multiple TUs to cache template instantiations during a single build. The cache will have to be deleted between builds since cache invalidation on changing a template is not present yet.
- Add cache invalidation, so the template cache can be preserved between builds.