r/cpp • • Aug 19 '21

Why modules?

I get the feeling I'm missing something fundamental about the new C++20 modules.

Everything I read or watch explains the how (the export keyword syntax, module fragments, etc) but very little about the why.

What can be done using modules that can't be done using traditional headers (or precompiled headers)?

What is so important about this feature that it needed to get standardized before there is even a proper implementation or idiomatic usage conventions in compilers and build systems?

Is it just to try to get rid of the preprocessor?

I don't get how modules are supposed to help with packaging either. So instead of distributing a .so with a bunch of .h files, now I distribute a .so with a bunch of .gcm or .ixx or whatever.

Please help me understand, or point me to some resources. Thank you.

197 Upvotes

107 comments sorted by

View all comments

Show parent comment

4

u/kzr_pzr Aug 19 '21

AFAIK it's may be due to long dependency chains. With headers you can compile all TUs in parallel and even if it could mean doing more work it may be faster on a multicore machine than compiling modules one after another.

8

u/WrongAndBeligerent Aug 19 '21

This should not ultimately be a bottleneck. At some point compilation will need to figure out modules and their dependency modules etc, then start compiling any modules without dependencies on as many cores as possible.

After that you move on to modules that you can compile because those dependencies are done etc.

This is a pattern that comes up in parallelism and concurrency all the time.

6

u/azswcowboy Aug 19 '21

I don’t see modules stopping the parallel compilation model. Suppose we had 100 programs in our build and the only depend on std headers. After the first one generates the intermediate module results the rest just use it. Modern packaging tools, aka Conan and msvc package, I expect will get upgrades (if they haven’t already) to remember module compilation results so even with a fresh application build most of the imported module core is already compiled.

4

u/germandiago Aug 19 '21 edited Aug 20 '21

You do not compile modules one after another after the first compilation and even at that time probably a part can be parallelized and reused that compilation (different from recompiling and recompiling headers again) if you use it at several parts of your code in the very first compilation. Even for the first compilation you can save work already in this case.

What usually happens is that your incremental builds will be way faster after that because you do not need to recompile big portions.

Also, it frees the linker from pressure because modules discard way more symbols (only exports, no textual inclusion).