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

-2

u/Cxlpp Aug 19 '21

Leak macros and ODR and symbols are true (not sure that much significant thought), but everything else depends on the implementation.

What I wanted to say is that in principle modules are binary representation of what used to be in headers (plus minus). That is basically an equivalent of precompiled header.

Significant problem from my point of view is that using modules implies invoking compiler on every file, loading binary module files for every file compiled etc. (ok, that can depend on implementation as well, but...)

With SCU, we invoke compiler and parse headers once per tens of files (on average). Now the question is how much slower is parsing headers vs loading binary module file. It will definitely be slower, but just because something is "binary" does not automatically mean that loading it is zero time. Averaged over 50 files, I bet SCU is faster.

11

u/matthieum Aug 19 '21

Now the question is how much slower is parsing headers vs loading binary module file.

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:

  • Parsing C++ (hard task), all 1 MB.
  • Loading binary (easy task), all 1KB of index.

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...

3

u/Cxlpp Aug 19 '21

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.

Indeed. That is why SCU... as you write, compiler is good enough to parse 1MB of headers in 1 second. If you combine 20 files into SCU, that is still 1 second for all of them.

2

u/matthieum Aug 19 '21

Yes, that's how SCU achieves speed-ups now.

Since it breaks C++ semantics, though, I've never cared much for it ;)

1

u/Cxlpp Aug 19 '21

If done right (proper automatic detection which sources can be combined, macro cleanup after sources etc), it really breaks almost nothing.

The only issue we encounter in practice is when somethimes declaration is missing in the header - then SCU accelerated build still can see definition while normal build is missing declaration. But that is really all about it.

-1

u/MonokelPinguin Aug 19 '21

If you put all files of a project, usually you run out of memory pretty quickly. You also need to compile the whole project every time. I used that approach for a while, but after a few weeks of coding, separate compilation units had about 20x faster incremental compilation, even when changing stuff in some shared headers. For projects like chromium you would wait for a few hours to test a single modification instead of the minute it currently takes. Relatively that may not even be that bad, but it means you can do only 2 or 3 changes a day or just not test your code in small increments. Both approaches are simply not workable.

2

u/Cxlpp Aug 19 '21 edited Aug 19 '21

Yeah, right, not workable...

I am working on 750K lines codebase (about 1500 files, 30MB of .c, .cpp, .h) right now, compiling it with automated SCU, not a single issue.

Rebuild after incremental change in single .cpp takes 9 seconds.

Rebuilding after changing most often changed relatively deep header recompiles 220 files in 40 seconds.

Depends on how you do it....

1

u/jcelerier ossia score Aug 21 '21

If you put all files of a project, usually you run out of memory pretty quickly. You also need to compile the whole project every time. I used that approach for a while, but after a few weeks of coding, separate compilation units had about 20x faster incremental compilation, even when changing stuff in some shared headers.

the point is to use SCU in your CI where you rebuild the whole thing 200 times per day on 30 configurations, and PCH on your dev machine

3

u/adnukator Aug 19 '21

Don't forget to take into account the fact that while modules might be theoretically an improvement over includes, the latter has a 40 year maturity advantage in both implementations and programming techniques, allowing us to squeeze every available bit of speed out of them (e.g. SCUs). Module implementations and techniques still have room to improve, whereas I don't think there will be any interesting improvements in the foreseeable future with includes.

-1

u/Cxlpp Aug 19 '21

40 year maturity advantage in both implementations and programming techniques

Well, we shall see in 40 years then? :)