from Hacker News

The hidden compile-time cost of C++26 reflection

by SuperV1234 on 3/6/26, 5:10 PM with 40 comments

  • by ralferoo on 3/10/26, 10:59 AM

    "I can recompile the entire thing from scratch in ~4.3s. That’s around ~900 TUs, including external dependencies, tests, and examples"

    In 30 years of using C++ this is the first time I've ever come across "translation unit" being abbreviated to TU and it took a bit of effort to figure out what the author was trying to say. Not sure why they felt the need to abbreviate this when they explain PCH for instance, which is a far more commonly used term.

    Thought I'd add the context here to help anyone else out.

  • by SuperV1234 on 3/10/26, 5:59 PM

    Update & Apology:

    I've fully updated the article with new benchmarks.

    A reader pointed out that the GCC 16 Docker container I originally used was built with internal compiler assertions enabled, skewing the data and unfairly penalizing GCC.

    I've re-measured everything on a proper release build (Fedora 44), and the compile times are ~50% faster across the board.

    The article now reflects the accurate numbers, and I've added an appendix showing the exact cost of the debug assertions.

    I sincerely apologize for the oversight.

  • by leni536 on 3/10/26, 10:07 AM

    libstdc++'s <print> is very heavy, reflection or not. AFAIK there is no inherent reason for it to be that heavy, fmtlib compiles faster.

    <meta> is another question, it depends on string_view, vector, and possibly other parts. Maybe it's possible to make it leaner with more selective internal deps.

  • by wjakob on 3/10/26, 10:53 AM

    I am very worried by feature creep in libc++ and libstdc++ and the harm that this inflicts on the wider C++ ecosystem. Transitive inclusion of large parts of the STL, and entangling of STL with core language features are both extremely bad. This should IMO be topic #1 of the committee but is barely even noticed. The mantra "It's okay, modules will save us" is naive and will not work.
  • by SuperV1234 on 3/6/26, 11:32 PM

    I ran some more measurements using import std; with a properly built module that includes reflection.

    I first created the module via:

      g++ -std=c++26 -fmodules -freflection -fsearch-include-path -fmodule-only -c bits/std.cc 
    
    And then benchmarked with:

      hyperfine "g++ -std=c++26 -fmodules -freflection ./main.cpp"
    
    The only "include" was import std;, nothing else.

    These are the results:

    - Basic struct reflection: 352.8 ms

    - Barry's AoS -> SoA example: 1.077 s

    Compare that with PCH:

    - Basic struct reflection: 208.7 ms

    - Barry's AoS -> SoA example: 1.261 s

    So PCH actually wins for just <meta>, and modules are not that much better than PCH for the larger example. Very disappointing.

  • by r2vcap on 3/10/26, 11:14 AM

    Yuck. I’ve already noticed compilation times increasing from C++17 to C++20, and this feature makes it much worse. I guess I’ll need to audit any reflection usage in third-party dependencies.
  • by HexDecOctBin on 3/10/26, 6:29 PM

    > Language features like templates are not the issue – the Standard Library is.

    What sins does STL commits that make it slow if templates themselves are not slow, and what kind of template code doesn't bloat compile times? In my experience, C++ libraries are usually one order of magnitude or more slower to compile than equivalent C ones, and I always chalked it upto the language.

  • by dalvrosa on 3/10/26, 10:54 AM

    Nice to learn about hyperfine

    Compilation speed is a huge part of productivity and enjoying writing C++

  • by wffurr on 3/10/26, 12:23 PM

    There are no zero cost abstractions: https://youtu.be/rHIkrotSwcc?si=oSBlKG9M_Ey8sVIU
  • by jstimpfle on 3/10/26, 9:57 AM

    The hidden compile-time cost of <insert almost any C++ feature>
  • by Jyaif on 3/11/26, 10:05 AM

    > the more enjoyable and productive I find C++ as a language – insanely fast compilation times are a large part of tha

    +1

    Rust really showed some of us how lucky we were with C++.

  • by zombot on 3/10/26, 11:57 AM

    How is it hidden if you can measure it?
  • by comandillos on 3/10/26, 2:32 PM

    just another piece to this jenga tower called c++. if you want reflection maybe just use a language that was designed with reflection support since the beginning.