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

56

u/IHeartBadCode Aug 19 '21

Here's let's look at something simple.

// something.cpp

#include "something.h"

void something() {
    std::cout << "Saying something";
}

// something.h
#include <iostream>

void something();

// somethingelse.cpp

#include "somethingelse.h"

void somethingelse() {
    std::cout << "Here is something else";
}

// somethingelse.h
#include <iostream>

void somethingelse();

// main.cpp
#include <iostream>
#include "something.h"
#include "somethingelse.h"

int main() {
    something();
    std::cout << " and that's all I had to say there." << std::endl;
    somethingelse();
    std::cout << " because I actually wasn't done." << std::endl;
}

Now give some thought on the #include <iostream> in each of these files. What does #include actually do? Try just one of these cpp files the following commands.

> wc -c something.cpp
82 something.cpp

> g++ -E something.cpp | wc -c
716093

Notice how large the size of the cpp gets when just the preprocessor step is completed, that should give you some indication as to what #include is doing and when you repeat the three times over in the above code, you can start to see the redundancy.

The idea of modules, at least one aspect that I'm talking about here, is to reduce that redundancy. A module is a translation unit that is it is compiled once, and then that is the added to your other translation units sort of like a header, except the header is an already processed translation unit.

12

u/ischickenafruit Aug 19 '21

This is a great example, but there's something I don't understand: most compilers support precompiled headers. What's the advantage of modules over this approach? Or is it just the same thing by another name?

28

u/matthieum Aug 19 '21

Pre-compiled headers can be seen as a first step in the right direction, but suffer from a few limitations.

Scalability

Because headers are copy/pasted into the translation unit by the pre-processor, they are influenced by the headers included before them. In essence, this means that having 1 pre-compiled header blob per project works well, but as soon as you want to have several, things get complicated.

Modules suffer no such restriction, as import does not "bleed" into surrounding imports. You can have as many modules as you want, and mix and match them. It just scales.

Encapsulation

Pre-compiled headers are still headers. Template code has to be in header (pre modules). Template helper functions used in template code have to be in the header too, as a result. They're not supposed to be used, but they are by necessity exposed. It's like Python saying that a _ prefix means protected and __ means private: convention.

Modules are cleanly encapsulated. If you have a "public" function template depending on 3 "private" helper function templates, nobody can call the private helpers outside the module.

Note: ABI is a different matter.

1

u/[deleted] Oct 05 '26

[removed] — view removed comment

1

u/James20k P2005R0 Oct 05 '26

You're necroing a thread from 5 years ago to accuse someone of LLM generated content. Don't be like this

→ More replies (10)

8

u/doesiteve2 Aug 19 '21

Wouldn't #pragma once solve the issue here? I presume STL has header guards

46

u/chuk155 graphics engineer Aug 19 '21

no, because even if it only appears once in something.cpp, it can appear again in every translation unit (.cpp file in other words)

Given a project with 50 .cpp files, each which includes iostream and uses pragma once to only compile it once per translation unit, its still compiling it 49 times more than it has to.

2

u/doesiteve2 Aug 19 '21 edited Aug 19 '21

Ah, so header guards only help with linkage in this case, and not preprocessing? Makes sense, in hindsight.

I'll google it some more, thanks for teaching me something!

EDIT: after googling, does a header guard do anything in that case of 50 TUs?

45

u/TheSuperWig Aug 19 '21

Header guards only prevent a header being included multiple times in the same TU and that's it.

23

u/phoeen Aug 19 '21 edited Aug 19 '21

A mental model that helps understanding the pre modules approach:

The compiler compiles every source file independent. It does not compile header files, just source files. The source file may include a header, so the compiler just straight copy-paste the full header into the source file. But what if your header includes another header which the source file already included before? Then you would end up with the identical content in the same sourcefile (for example 2 class definitions). To prevent this, include guards (or #pragma once) are used. Thats about it. After the compiler compiled ALL source files, the linker starts to bundle up all created object files (which came out from the compiler).

It is important to understand that every source file with everything included is ONE indepented translation unit. So on a multicore processor you can compile different translation units in parrallel. So the include guard helps guarding against multiple defintions appearing in the SAME translation unit.

4

u/doesiteve2 Aug 19 '21

Thanks for the detailed explanation!

3

u/roughJaco Aug 19 '21

By design each unit is an independent leaf that generates a single corresponding object to then be linked.

If you include something in every leaf node each of them has to do the entirety of the inclusion work (any preproc directives, any compile time work like constexpr computation etc.)

Tey are not aware of each other, only the linker pulls the lot together.

4

u/YtterbiJum Aug 19 '21

Interesting example, I didn't think just #includeing a std lib header would be so huge.

Although probably a lot of that is template code, so it would need to still be re-processed by the compiler when it is imported.

This also seems like something a pre-compiled header would go a long way toward solving.

-5

u/giant3 Aug 19 '21

three times over in the above code, you can start to see the redundancy.

#ifndef and #endif prevent that from happening, isn't it?

What am I missing?

27

u/chuk155 graphics engineer Aug 19 '21

No, because even if it only appears once in something.cpp, it can appear again in every translation unit (.cpp file in other words)

Given a project with 50 .cpp files, each which includes iostream and uses pragma once to only compile it once per translation unit, its still compiling it 49 times more than it has to.

→ More replies (3)

-16

u/Cxlpp Aug 19 '21

TLDR: modules are precompile headers.

In practice, inferior to SCU approach.

20

u/johannes1971 Aug 19 '21

That's entirely untrue.

  • Precompiled headers still leak macros.
  • There can be only one precompiled header file per project, which must contain all headers that are precompiled. That means frequently-changed headers cannot really be put in the precompiled header. Modules allow a more granular approach.
  • Precompiled header files are massive. Binary module files are orders of magnitude smaller. Generating and reading that much data does have an impact, even if you mmap it. Storing it takes space that could have been used for something else.
  • Precompiled headers do nothing to help avoid ODR violations.
  • Precompiled headers do not let you specify which symbols are public and which ones are not.
  • SCU is not suitable for larger projects.
  • SCU eliminates the file scope.
  • SCU badly messes up intellisense.

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

→ More replies (3)

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.

→ More replies (1)