如何减少模板重的C ++代码的输出大小? [英] How to reduce output size of template-heavy C++ code?

查看:114
本文介绍了如何减少模板重的C ++代码的输出大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的问题。我有一个共同的图书馆,这是用于我的项目。这个库密集使用 boost.spirit boost.fusion 。不幸的是,图书馆大约。 700Mb。所有 boost.spirit - 海盗代码使用,它工作得很好。可以做什么步骤来减少其输出大小?是否有一个工具可以帮助确定什么模板实例化浪费了大部分空间?

I have a huge problem. I have a common library, that is used all across my project. This library intensively uses boost.spirit and boost.fusion. Unfortunately, the library is approx. 700Mb in size. All the boost.spirit-heavy code is used and it works well. What steps can be done to reduce its output size? Is there is a tool that can help to determine what template instantiations waste most of the space?

首先,我决定将所有的精神感知代码移动到cpp文件。第二,我将尝试不同的编译器标志来优化大小。我不知道该怎么做。

At first, I decided to move all spirit-aware code to cpp files. Second, I will try different compiler flags to optimize for size. I don't know what else to do.

更新(详情)

我使用GNU工具链。巨大的图书馆实际上是一个静态图书馆。可执行文件,使用这个700Mb库的大小是200Mb。至少有一半的代码在* .h文件中。 boost.spirit 语法(非常模板重的东西)也位于* .h文件中。

I'm using GNU toolchain. Huge library is actually a static library. Executable, that uses this 700Mb library is 200Mb in size. At least half of the code is in *.h files. Some boost.spirit grammars (very template heavy thing) is also located in *.h files.

干杯!

推荐答案

将精神感知代码移动到 .cpp 第一步,它可能不完整,虽然你提到在头文件中有精神语法。

Moving the spirit aware code to .cpp files is a good first step, it might be incomplete though as you mention having spirit grammar in header files.


  1. 确保语法/规则被导出到库外部。如果你有典型的 include / src 目录,那么移动这些文件$ c> src 目录。

  1. Make sure than none of the grammar / rules are ever exported outside the library. If you have the typical include/src directories, then move those files (even if headers) within the src directory.

将所有这些符号标记为库的内部。他们不应该从图书馆外面访问。根据您的编译器,有特定的pragmas /属性,在gcc查找 visibility属性 __ attribute__((visibility(internal)))。这有助于编译器相应地优化它们,特别地,即使在给定调用位置处,在函数地址被采用的情况下,编译器也可以发出函数的代码。但是,由于内部可见性,因为它知道代码不会离开对象,它可能会删除该函数。

Mark all those symbols as internal to the library. They should not be accessible from outside the library at all. There are specific pragmas/attributes depending on your compiler, on gcc lookup the visibility attribute: __attribute__ ((visibility ("internal"))). This helps the compiler optimizing them accordingly, notably a compiler may emit the code of a function even if it inlines it at a given call site, just in case this function address is taken. With internal visibility however, since it knows the code will not leave the object, it may elide the function.

身体,但似乎不能再找到...

I seem to remember a flag to fuse identical function bodies but cannot seem to find again...

这篇关于如何减少模板重的C ++代码的输出大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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