C++26 反射的隐藏编译时代价
The hidden compile-time cost of C++26 reflection

原始链接: https://vittorioromeo.com/index/blog/refl_compiletime.html

## C++26 反射:早期编译时性能预期 作者探讨了 C++26 反射对编译时性能的影响,其动机是追求快速迭代和生产力。使用 GCC 16(实验版)的基准测试表明,虽然反射特性标志本身开销很小,但使用反射*会*带来显著的编译时成本。 包含 `` 大约增加 149 毫秒,即使反射少量结构体也会迅速增加 – 最初每个类型增加约 6.3 毫秒,随后每个额外类型降低到约 2.2 毫秒。标准库仍然是主要瓶颈,`` 和 `` 会增加大量的解析时间。 预编译头文件 (PCH) 对于缓解这些成本至关重要,可以显著缩短编译时间。令人惊讶的是,初步测试表明,PCH 目前在反射密集型代码中优于模块。 作者对反射与标准库的紧密耦合表示遗憾,并倡导更轻量级的实现,如最初所建议的。他们预测,反射的广泛使用将需要 PCH(或最终,有效的模块)来保持合理的编译速度,可能为每个编译单元增加至少 540 毫秒的开销,具体取决于常见依赖项。最终,更快的编译仍然是 C++ 乐趣和生产力的关键因素。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交 登录 C++26 反射的隐藏编译时成本 (vittorioromeo.com) 6 分,SuperV1234 2 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 SuperV1234 19 分钟前 [–] 我使用 `import std;` 和一个正确构建的包含反射的模块进行了更多测量。我首先通过以下方式创建模块:`g++ -std=c++26 -fmodules -freflection -fsearch-include-path -fmodule-only -c bits/std.cc` 然后使用以下方式进行基准测试:`hyperfine "g++ -std=c++26 -fmodules -freflection ./main.cpp"` 唯一的“包含”是 `import std;`,没有其他内容。结果如下: - 基本结构体反射:352.8 毫秒 - Barry 的 AoS -> SoA 示例:1.077 秒 与 PCH 相比: - 基本结构体反射:208.7 毫秒 - Barry 的 AoS -> SoA 示例:1.261 秒 所以 PCH 在仅针对 `` 时获胜,并且模块对于更大的示例来说并没有比 PCH 好多少。非常令人失望。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

I am very excited about C++26 reflection.

I am also obsessed by having my code compile as quickly as possible. Fast compilation times are extremely valuable to keep iteration times low, productivity and motivation high, and to quickly see the impact of your changes.

With time and experience, I’ve realized that C++ can be an extremely fast-to-compile language. Language features like templates are not the issue – the Standard Library is.

My fork of SFML uses almost no Standard Library at all, and I can recompile the entire thing from scratch in ~4.3s. That’s around ~900 TUs, including external dependencies, tests, and examples. Incremental builds are, for all intents and purposes, instantaneous. I love it.

I would love to live in a world where C++26 reflection is purely a lightweight language feature, however that ship has sailed (thank you, Jonathan Müller, for trying).

In this article, I’ll try to provide some early expectations about the compile-time impact of C++26 reflection.

let’s measure!

I found a nice Docker image containing GCC 16, the first version that supports reflection, and got to work. To get reasonably stable measurements, I used the hyperfine command-line benchmarking tool.

These are my specs:

  • CPU: 13th Gen Intel Core i9-13900K
  • RAM: 32GB (2x16GB) DDR5-6400 CL32
  • OS: Debian 13 Slim (on Docker, sourcemation/gcc-16)
  • Compiler: GCC 16.0.1 20260227 (experimental)
  • Flags: -std=c++26 -freflection

My test scenarios were as follows:

  1. Baseline test. Just a int main() { }.

  2. Header inclusion test. Same as above, but with #include <meta>.

  3. Basic reflection over a struct’s fields:

  4. AoS to SoA transformation example, from his blog post.

I’ve also tested using precompiled headers (PCHs) for <meta> and other large dependencies.

⚠️ DISCLAIMER: please take these benchmark results with a grain of salt. ⚠️

  • My measurements are not that rigorous and that the compiler I used is still work-in-progress.
  • Also note that my specs are quite beefy – YMMV.
  • Finally, remember that these measurements are for a single translation unit – in a real project, you’d have to multiply the compile time overhead by the number of affected TUs.

benchmark results

# Scenario Code Precompiled Header (PCH) Compile Time (Mean)
1 Baseline (No Reflection Flag) int main() only None 43.9 ms
2 Baseline + -freflection int main() only None 43.1 ms
3 <meta> Header Inclusion int main() + #include <meta> None 310.4 ms
4 Basic Struct Reflection (1 type) reflect_struct with User None 331.2 ms
5 Basic Struct Reflection (10 types) reflect_struct with User<N> None 388.6 ms
6 Basic Struct Reflection (20 types) reflect_struct with User<N> None 410.9 ms
7 AoS to SoA (Original) Barry Revzin’s Unedited Code None 1,622.0 ms
8 AoS to SoA (No Print) Removed <print> None 540.1 ms
9 AoS to SoA (No Print/Ranges) Removed <print>, <ranges> None 391.4 ms
10 AoS to SoA (Original + PCH) Barry Revzin’s Unedited Code <meta>, <ranges>, <print> 1,265.0 ms
11 AoS to SoA (No Print + PCH) Removed <print> <meta>, <ranges> 229.7 ms
12 AoS to SoA (No Print/Ranges + PCH) Removed <print>, <ranges> <meta> 181.9 ms

A few clarifications:

P3429: <meta> should minimize standard library dependencies) was given more thought and support.

I also really wish that a game-changing feature such as reflection wasn’t so closely tied to the Standard Library. The less often I use the Standard Library, the more enjoyable and productive I find C++ as a language – insanely fast compilation times are a large part of that.

Hopefully, as reflection implementations are relatively new, things will only get better from here.

shameless self-promotion

  • I offer training, mentoring, and consulting services. If you are interested, check out romeo.training, alternatively you can reach out at mail (at) vittorioromeo (dot) com or on Twitter.

  • Check out my newly-released game on Steam: BubbleByte – it’s only $3.99 and one of those games that you can either play actively as a timewaster or more passively to keep you company in the background while you do something else.

  • My book “Embracing Modern C++ Safely” is available from all major resellers.

  • If you enjoy fast-paced open-source arcade games with user-created content, check out Open Hexagon, my VRSFML-powered game available on Steam and on itch.io.

    • Open Hexagon is a community-driven spiritual successor to Terry Cavanagh’s critically acclaimed Super Hexagon.
联系我们 contact @ memedata.com