使用许多外部类型声明时如何加快编译时间
项目:
xcode 中的 C++ 编程。我有超过 3,000 多个类型定义,分布在 2,000 多个 .c/.h 文件中。每个 myType
类型都包含一个字符串描述。我使用脚本在 .cpp
文件中定义了包含 3,000 多个元素的 map
,用于查找 myType< /code> 类型传递给一个函数,该函数根据传递给它的
myType
来处理数据。由于 myType
定义分布在 2,000 多个文件中,因此我使用脚本将每个 extern myType TYPENAME;
写入头文件中。
概述:
(2,000+ 个具有 myType
定义的 .c
文件)
myTypes.h
(包含所有外部 上述文件中每个
语句)myType
的 >myType
myTypes.cpp
(包含 map
)
typeProcessor.cpp
(包括 myTypes.h
。使用 myTypes.cpp< 中定义的映射/code> 将字符串与
myType
相匹配。将 myType
传递给下面文件中的函数)
dataProcessor.cpp
(基于数据处理)传递给它的 myType
)
问题:
因为我添加了带有 3,000 多个 extern 语句的 myTypes.h
和带有 a 的 myTypes.cpp
包含 3,000 多个元素的地图我的项目编译时间从 20 秒延长到 1-1.5 小时。
我的问题:
在不接触 2,000 多个文件或接收 myType
的 dataProcessor.cpp
的情况下,我可以做些什么来减少编译时间?
我的一些想法:
使用脚本将所有
myType
定义放入一个大myTypes.cpp
文件中,并删除 extern 语句。 或使用脚本
#include
包含myType
定义的2,000多个文件中的每一个
我对编译器、编译时间的主要因素以及如何编写代码以最小化编译时间了解不多。任何帮助表示赞赏。谢谢。
The Project:
C++ Programming in xcode. I have over 3,000+ type definitions spread over 2,000+ .c/.h files. Each myType
type contains a string description. I used a script to define a map<std::string, myType>
of 3,000+ elements in a .cpp
file to use for looking up a myType
type to pass to a function that processes data based on which myType
is passed to it. Because the myType
definitions are spread over 2,000+ files, I used a script to write each extern myType TYPENAME;
in a header file.
Overview:
(2,000+ .c
files with myType
definitions)
myTypes.h
(contains all the extern myType
statements for each myType
in the above files)
myTypes.cpp
(contains the map<std::string, myType>
of 3,000+ elements in the above files)
typeProcessor.cpp
(includes myTypes.h
. Uses the map defined in myTypes.cpp
to match a string to a myType
. Passes the myType
to a function from the file below)
dataProcessor.cpp
(processes data based on the myType
passed to it)
The Problem:
Since I've added myTypes.h
with 3,000+ extern statements and myTypes.cpp
with a map of 3,000+ elements my project compile time has lengthened from 20 seconds to 1-1.5 hours.
My Question:
Without touching the 2,000+ files or the dataProcessor.cpp
that receives the myType
, what can I do to reduce the compile time?
Some thoughts I've had:
use a script to put all the
myType
definitions into one bigmyTypes.cpp
file and remove the extern statements. oruse a script to
#include
each of the 2,000+ files containing themyType
definitions
I don't know much about compilers, the main factors on compile time, and how to write code to minimize compile time. Any help is appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不管你可以做/已经做了很多事情来更好地构建你的代码,为什么不
。
在类型定义的每个 .cpp 中,调用注册函数。这样,类型会自行注册,并且类型映射不需要“知道所有类型”。这是依赖关系的反转,以一种简单的形式
关键在于,没有文件需要包含注册过程的所有类型标头。如果您需要特定于子类型的接口(例如,在地图元素上使用
dynamic_cast
时),您仍然需要包含(选定的)类型标头关键
PS。我假设 myType 有某种常见的基本类型(因为否则您无法获得
std::map
进行编译)更新
编辑
registerTypeMapping
设为extern "C"
函数(只需编辑原型使其符合 C 标准)type_xx.h/type_xx.c
组合起来,您总是可以生成一个额外的源文件type_xx_register.c
,其中仅仅该类型并注册它。请注意,这将导致更多源,但可能会减少编译时间。特别是,如果您有一个 makefile,并且仅在依赖项真正更改时才编译
type_xx_register.o
对象文件。这样,只有
typemap.h
中的更改才会导致所有这些文件的重新编译。<子>
简单的_也不是不可能
比一次性编译包含所有 type_xx.h 的单个源更快_
Regardless of many things you could do/have done to structure your code better, why don't you
.
In every .cpp for a type definition, call the registration function. That way a type registers itself, and there is no need for the type map to 'know all the types'. This is inversion of dependency, in a simple form
The crux is, that no file needs to include all the type headers for the registration process. You'll still need to include (selected) type headers if you need interfaces specific to the subtype (e.g. when using
dynamic_cast<subtype>
on a map element)PS. I'm assuming some kind of common base type for myType (because there is no way you could get the
std::map<std::string, myType>
to compile otherwise)Update
Edit
registerTypeMapping
anextern "C"
function (just edit the prototype to be C-conforming)type_xx.h/type_xx.c
combi you can always generate an extra source filetype_xx_register.c
that includes just that type and registers it.Note that this will lead to more sources but will likely reduce compile times. Especially, if you have a makefile and only compile the
type_xx_register.o
object file when it's dependencies truly changed.That way, only a change in
typemap.h
would cause a recompilation of all these files.It is also not impossible that a simple
_would be faster than the compilation of a single source including all type_xx.h at once_
由于所有这些文件都没有太大变化(我假设),因此要么解析 3000 多个行标头,要么进行链接,从而缩短编译时间。如果是解析,预编译头应该可以解决这个问题,如果是链接,像 Visual Studio 的增量链接之类的东西应该会有所帮助,但我认为除了 MSVC 之外没有任何编译器具有此功能(如果我错了,请纠正我)。
As all these files don't change much (I'm assuming), it's either parsing the 3000+ line header or linking that kills the compile time. If it's parsing, a precompiled header should solve that, if it's linking, something like Visual Studio's incremental linking should help out, but I don't think any compiler except MSVC has this capability (correct me if I'm wrong).
这可能是阻止保存这 3,000 个文件的问题
,与 xcode 自动保存方法
这个
自动保存关闭
选项在较新版本的 xcode 中缺失(非常愚蠢)(3.1 是一个可靠的 xcode),因此所有文件都会获得一个新的时间戳,并且那么相当于清理所有重建。尝试在命令行上
make
不保存文件作为速度比较。it might be a matter of preventing a save on those 3,000 files
do the opposite of the xcode autosave method
this
autosave off
option is (very foolish) missing in newer versions of xcode (3.1 is a solid xcode) thus all files get a new timestamp and then the equivalent of a clean all rebuild.try
make
on the commandline that doesn't save files as a speed comparison.