命名空间别名会影响大型代码库中的构建时间吗? (C++)

发布于 2024-12-11 18:18:28 字数 166 浏览 0 评论 0原文

我在标头中声明了一些(不是很多)名称空间别名。一些标头在大型代码库中具有相当高的可见性。几年前我大部分就不再使用它们了,但有些仍然挥之不去。我还一路删除了其中一些,但我没有在之前/之后计时。在我去移除它们之前,我想知道是否有人对此进行了测试和计时,以及测试的结果是什么。我不介意它们的存在,除非它们明显损害了构建时间。

I have some (not many) namespace aliases declared in headers. Some of the headers have pretty high visibility in a large codebase. I stopped using them for the most part years ago, but some still linger. I've also removed some of them along the way, but I did not time before/after. Before I go and remove them, I was wondering if anybody had tested and timed this, and what the results of that test were. I don't mind that they exist, unless they measurably hurt build times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-12-18 18:18:28

命名空间别名的存在不太可能显着影响您的构建时间,但是包含多余的标头肯定会影响。

C++ 编译阶段会受到其生成的所有 IO 的影响,这些 IO 是为了获取分散在磁盘上的多个包含文件的内容而产生的,消除这些文件的很大一部分应该会缩短构建时间。

一个极端的情况是,如果您有繁重的预处理/元编程代码,在这种情况下,您可能会达到 CPU/内存带宽限制,并且补充文件不会有太大影响。

提醒一下,减少编译时间的最佳方法是消除依赖关系,从而减少增量构建。

The presence of namespace aliases is unlikely to affect your build time significantly, however the inclusion of superfluous headers certainly is.

A C++ compilation phase suffers greatly from all the IO it generates to get the content of the multiple include files that are scattered around the disk, eliminating a significant portion of those files should improve your build-time.

A corner case would be if you have heavy preprocessing/meta-programming code, in which case you might hit CPU/memory bandwidth bounds and the supplementary file won't matter much.

As a reminder, the best way to reduce compilation time is to eliminate dependencies so that incremental builds are smaller.

幼儿园老大 2024-12-18 18:18:28

免责声明:我只能根据我对 clang 编译器的有限经验来发言。

在 clang 中,命名空间别名定义不会导致源命名空间中的所有符号被复制到当前声明上下文(即作用域)中。相反,编译器将在上下文中发出命名空间别名声明记录。

换句话说,符号表的大小仅增加一,因此,未命名别名的查找仅受到轻微影响。当然,如果您使用别名,则会执行两次查找 - 一次查找别名声明,另一次在目标命名空间中执行查找。

Disclaimer: I can only speak from my limited experience with the clang compiler.

In clang, a namespace alias definition will not cause all symbols from the source namespace to be copied into the current declaration context (i.e. scope). Instead, the compiler will emit a namespace alias declaration record into the context.

In other words, the size of the symbol table is only increased by one and as such, lookups that don't name the alias are only marginally affected. Of course, if you use the alias, two lookups are performed -- one that locates the alias declaration and one that performs the lookup in the target namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文