分析模板元编程时间 [英] Profiling template metaprogram compilation time

查看:129
本文介绍了分析模板元编程时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有大量编译时计算的C ++项目。长时间的编译时间会减慢我们的速度。我如何找到我们的模板元程序中最慢的部分,以便我可以优化它们? (当我们有慢运行时计算时,我有许多选择,例如valgrind的 callgrind 工具。所以我尝试构建一个调试GCC和分析它编译我们的代码,但我)



我使用GCC和Clang,但欢迎任何建议。



我在Boost的网站上发现了 profile_templates ,但它似乎文档很薄,需要jam / bjam构建系统。如果你展示如何使用它在一个非堵塞的项目 1 ,我会upvote你。 https://svn.boost.org/svn/boost/sandbox/tools/profile_templates/ 1 我们的项目使用CMake,并且是

解决方案

我一直在工作,因为2008对使用模板元编程的图书馆。有一个真正需要更好的工具或方法来理解什么消耗最多的编译时间。



我知道的唯一的技术是分而治之的方法,代码转换为不同的文件,注释掉模板定义的主体,或者通过在#define宏中包装你的模板实例,并暂时重新定义这些宏来不做任何事情。



顺便提一下,只要将相同的代码分成更多,更小的文件,就可以更快地编译。我不只是谈论并行编译的机会 - 甚至连续,我观察它仍然更快。我在gcc中观察到这种效果,当我编译我的库和编译Boost Spirit解析器时。我的理论是,gcc中的一些符号解析,重载解析,SFINAE或类型推断代码相对于类型定义或符号的数量具有O(n log n)或甚至O(n ^ 2)复杂度



最后,你需要做的是仔细检查你的模板,并分离什么真正取决于类型信息从什么不是,并使用类型擦除和虚拟函数,尽可能在代码的实际上不需要模板类型的部分。您需要从标头中获取内容,并进入cpp文件 if ,可以移动部分代码。在一个完美的世界中,编译器应该能够为自己找出这一点 - 你不应该手动移动这段代码babysit它 - 但这是与我们今天的编译器的最先进的。


I'm working on a C++ project with extensive compile-time computations. Long compilation time is slowing us down. How might I find out the slowest parts of our template meta-programs so I can optimize them? (When we have slow runtime computations, I have many profilers to choose from, e.g. valgrind's callgrind tool. So I tried building a debug GCC and profiling it compiling our code, but I didn't learn much from that.)

I use GCC and Clang, but any suggestions are welcome.

I found profile_templates on Boost's site, but it seems to be thinly documented and require the jam/bjam build system. If you show how to use it on a non-jam project1, I will upvote you. https://svn.boost.org/svn/boost/sandbox/tools/profile_templates/ appears to count number-of-instantiations, whereas counting time taken would be ideal.

1 Our project uses CMake and is small enough that hacking together a Jamfile just for template profiling could be acceptable.

解决方案

I've been working since 2008 on a library that uses template metaprogramming heavily. There is a real need for better tools or approaches for understanding what consumes the most compile time.

The only technique I know of is a divide and conquer approach, either by separating code into different files, commenting out bodies of template definitions, or by wrapping your template instantiations in #define macros and temporarily redefining those macros to do nothing. Then you can recompile the project with and without various instantiations and narrow down.

Incidentally just separating the same code into more, smaller files may make it compile faster. I'm not just talking about opportunity for parallel compilation - even serially, I observed it to still be faster. I've observed this effect in gcc both when compiling my library, and when compiling Boost Spirit parsers. My theory is that some of the symbol resolution, overload resolution, SFINAE, or type inference code in gcc has an O(n log n) or even O(n^2) complexity with respect to the number of type definitions or symbols in play in the execution unit.

Ultimately what you need to do is carefully examine your templates and separate what really depends on the type information from what does not, and use type erasure and virtual functions whereever possible on the portion of the code that does not actually require the template types. You need to get stuff out of the headers and into cpp files if that part of the code can be moved. In a perfect world the compiler should be able to figure this out for itself - you shouldn't have to manually move this code to babysit it - but this is the state of the art with the compilers we have today.

这篇关于分析模板元编程时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆