user541686
user541686

Reputation: 210402

How to find frequently instantiated templates in C++?

I have a lot of templates in my code.

How do I figure out which templates are causing code bloat (i.e. by being instantiated many times)?

Upvotes: 0

Views: 87

Answers (1)

Zólyomi István
Zólyomi István

Reputation: 2441

A quick note to the question and its comments: the number of template instantiations is not significant, only the number of instantiations with different type combinations are. This is very different from template specializations.

There's an easy and dirty way to follow instantiations manually. For the templates you want to check, simply add a line that generates a warning. It's hard to give such a statement here, because it completely depends on your compiler type and your compiler options in the project. A good guess is to add a static variable with a unique name and a suspicious value. Now all you have to do is to filter the compiler logs for these custom warnings. Since all different type combinations are compiled only once, you should have only one warning for each combination. If not so, it's still easy to filter duplications from the log.

Unfortunately, marking all your templates with such a custom statement is intrusive and can be very time consuming. If you need something more sophisticated, you can try a template metaprogram debugger. Here (Boost mailing list) you can find a short summary and a link to the download page of a freely available debugger.

http://boost.2283326.n4.nabble.com/Re-vsix-template-profiler-extension-Templight-td3888368.html

If you're curious how it works, you can read more about it in http://gsd.web.elte.hu/contents/articles/gpce06.pdf .

Upvotes: 3

Related Questions