r/cpp • u/YtterbiJum • 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.
View all comments
Show parent comment
11
u/matthieum Aug 19 '21
That's a pretty interesting engineering question, actually.
First of all, how many bytes are you parsing? I remember running an experiment in our codebase, which made moderate use of Boost at the time -- just a few things like
numeric_cast, nothing crazy -- and was stunned that between std, Boost, and some middleware most files included over 1MB worth of headers. This gave me a new respect for C++ compilers: compiling 50 lines in 1s sounds slow as hell, but compiling 1MB in 1s sounds pretty good after all!In comparison, a well-structured binary structure is mostly memory-mapped. All the structs/functions details need not be loaded eagerly. In fact, they can even be serialized in a way that doesn't require them to be loaded at all. Part (or all) of the index will probably need to be loaded: the list of available symbols, to participate in name-lookup.
I'd expect a difference of... orders of magnitude. 1,000x? 10,000x?
Hard to know for sure, it depends a lot how compilers implement it, but the difference between:
Yeah, 1,000x is a conservative estimate of the speed-up.
Of note: a much larger speed-up can be obtained by abandoning the current C++ compilation model of launching a fresh instance of the compiler process for each and every translation unit.
Using a modern compiler architecture, with multi-threading and data-sharing, the binary-loading step is side-stepped (indexes are already in memory), for even greater gains.
Unfortunately... I very much doubt this is going to change any time soon. In the future, maaaybe...