C++20 Modules: Practical Insights, Status and TODOs

chuanqixu9.github.io

76 points by ashvardanian 5 days ago


achatham - 2 days ago

We use c++ modules at Waymo, inside the google monorepo. The Google toolchain team did all the hard work, but we applied it more aggressively than any team I know of. The results have been fantastic, with our largest compilation units getting a 30-40% speedup. It doesn't make a huge difference in a clean build, as that's massively distributed. But it makes an enormous difference for iterative compilation. It also has the benefit of avoiding recompilation entirely in some cases.

Every once in a while something breaks, usually around exotic use of templates. But on the whole we love it, and we'd have to do so much ongoing refactoring to keep things workable without them.

Update: I now recall those numbers are from a partial experiment, and the full deployment was even faster, but I can't recall the exact number. Maybe a 2/3 speedup?

triknomeister - 2 days ago

> Yes, we can. C++20 Modules are usable in a Linux + Clang environment. There are also examples showing that C++20 Modules are usable in a Windows environment with MSVC. I have not yet heard of GCC’s C++20 Modules being used in non-trivial projects.

People keep saying this and yet I do not know of a good example from a real life project which did this which I can test. This seems very much still an experimental thing.

ho_schi - 2 days ago

Looking at the examples from using Clang (I use GCC btw.):

   clang++ -std=c++20 Hello.cppm --precompile -o Hello.pcm
   clang++ -std=c++20 use.cpp -fmodule-file=Hello=Hello.pcm Hello.pcm -o Hello.out
   ./Hello.out
Why is something which shall makes things easy and secure so complicated?

I'm used to:

    g++ -o hello hello.cpp

It can use headers. Or doesn't use headers. I doesn't matter. That's the decision of the source file. To be fair, the option -std=c++20 probably isn't necessary in future.

I recommend skimming over this issue from Meson:

https://github.com/mesonbuild/meson/issues/5024

Reading the last few blog posts from a developer of Meson, providing some insights why Meson doesn't support modules until now:

https://nibblestew.blogspot.com/

menaerus - 2 days ago

> The data I have obtained from practice ranges from 25% to 45%, excluding the build time of third-party libraries, including the standard library.

> Online, this number varies widely. The most exaggerated figure I recall is a 26x improvement in project compilation speed after a module-based refactoring.

> Furthermore, if a project uses extensive template metaprogramming and stores constexpr variable values in Modules, the compilation speed can easily increase by thousands of times, though we generally do not discuss such cases.

> Apart from these more extreme claims, most reports on C++20 Modules compilation speed improvements are between 10% and 50%.

I'd like to see references to those claims and experiments, size of the codebase etc. I find it hard to believe the figures since the bottleneck in large codebases is not a compute, e.g. headers preprocessing, but it's a memory bandwidth.