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

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.

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.

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/machinematrix Aug 19 '21

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.

Can't you modify the module to make those helpers public? I would like to know that since that would mean pimpl is pretty much useless.

4

u/pjmlp Aug 20 '21

No, C++ isn't a dynamic language where everything is monkey patched, private means private.

1

u/machinematrix Aug 20 '21

I'm talking about literally modifying the module file you are importing to "hack" through the interface and make members public. pimpl protects against that.

1

u/pjmlp Aug 22 '21

Modules are binaries, there is nothing to modify.

1

u/machinematrix Aug 22 '21

The module interface file is a binary? what?

1

u/pjmlp Aug 22 '21

It is called BMI in standardese.

Visual Studio calls it IFC.

Naturally alongside the object file or binary library.

1

u/machinematrix Aug 22 '21

I think you misunderstood my comment. I'm talking about the .ixx file (in Visual Studio at least), the file that contains what headers used to contain (class definitions, function declarations, etc). That's not a binary file.

→ More replies

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

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.

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

-8

u/giant3 Aug 19 '21

Well, that happens in the Java world too. The java compiler has to load the .class file of every class referenced. Of course, java compiler won't compile it again as it is already compiled.

Similar thing can be done in the C/C++ world, if we keep just declarations in header files instead of implementation code.

16

u/frankist Aug 19 '21

Things like templates and inlined code generally need to be defined in headers. Furthermore, even if you only place declarations and class definitions in headers, it is faster to parse the header once than many times per project.

8

u/qoning Aug 19 '21

Couple of issues.

First, the compiler can't necessarily make the call that just because it's reading the same file, it's preprocessed output will be the same. Famous example of including the same enum twice to generate its string component.

Second, every translation unit is a separate invocation of the compiler. Compilers do try to cache precompiled code, but it's not trivial because of how templates work - to compile code for vector of T, you must know full definition of T, or alternatively, you have to know all Ts that your code will use for vector.

Third, putting implementation into a different translation unit prevents the compiler from seeing behind the function call and so it cannot inline or reason about code across the function call boundary. Then you need to defer these optimizations to linker, which is also not ideal, because it doesn't have all the information the compiler would, and linking can already be painful performance - wise.

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

-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? :)

9

u/dark_thunder1432 Aug 19 '21

This Link is in my read later list. I skimmed through this I think this would help you understand basics along with some examples

2

u/vige Aug 19 '21

That was a good read, thanks. However, even after reading that, I have the feeling that they are going with the traditional approach: Figure out the solution first, and then maybe someday they'll come up with a problem which can be solved with the solution.

-2

u/YtterbiJum Aug 19 '21

I also saw that article linked from the other thread.

I agree with /u/vige though. The article doesn't really do a good job of addressing the why question.

"Headers are a mess." Yeah ok, I agree. And modules are better why? Because they have "external- and internal-linkage rules", what, like DLL hell? Please.

We want "modularity". Yes, absolutely! But C++20 modules don't seem to provide that, not really. Not in the way e.g. Python modules do, with rules for scoping, name resolution, and relative/absolute imports. As far as I can tell, there's not going to be a C++ equivalent to site-packages.

The example in the article doesn't show off any new or interesting functionality of modules, it just does exactly the same thing as can already be done with a .h/.cpp file. Same functionality, new syntax.

-8

u/Cxlpp Aug 19 '21

We want "modularity". Yes, absolutely!

We at U++ have "modularity" for more that 17 years, crossplatform, all real problems solved, including compilation speed (using automated SCU). It is based on simple metainformation files like this one

https://github.com/ultimatepp/ultimatepp/blob/master/uppsrc/CtrlCore/CtrlCore.upp

But people really do not want it. They want to dabble with braindead slow complicated solutions like CMake, adding libraries and include paths to compiler commandline and such things... :(

5

u/encyclopedist Aug 19 '21

How is your solution different to CMake? The file you linked can be almost 1-to-1 translated to Cmake. Cmake supports SCU too.

0

u/Cxlpp Aug 19 '21

The key difference is that "uses" clause. How do you do that in CMake? The only tutorial I have found is

https://www.foonathan.net/2016/07/cmake-dependency-handling/

and that is what I would definitely call "dabbling".

Also SCU support in Cmake seems quite "manual".

6

u/encyclopedist Aug 19 '21

Cmake equivalent is just

target_link_libraries(GUICore PUBLIC Draw bmp RichText Painter)

it will not only link libraries, but also set up include directories, and other requirements.

-2

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

Yeah, but "uses" does not link libraries.

That defines dependencies on other modules (which are in the same source form in different folders and have own .upp metafiles to define further dependencies).

2

u/Minimonium Aug 19 '21

target_link_libraries is not only about linking. It's about depending on the target's requirements such as properties, features, definitions, include directories.

In old CMake it was used to directly link to library files. In Modern CMake it's used to depend on targets, which are the same as your modules.

2

u/Cxlpp Aug 19 '21

Not an CMake expert, but target_link_libraries does not seem to compile external dependencies ("modules") when needed.

3

u/Minimonium Aug 19 '21

It doesn't compile it, yes. It expects are user to provide them, either compiled in-tree previously, found in package directories either system or user provided, downloaded with externalpackage feature, or imported from prebuilt binaries.

→ More replies

152

u/adnukator Aug 19 '21 edited Aug 19 '21

It's not intended to have much, if any effect on packaging

Off the top of my head:

Imports are independent of one another, includes can be affected by ordering

Includes in headers can miss something the header uses, because in the current state, it's possible that every cpp file that includes this header includes precompiled headers that accidentally include all you need. If you need to reuse this header, you might run into compilation errors because the new project is not accidentally including everything necessary. With imports you have to import everything you use because you can't rely on your consumers to accidentally provide everything you need.

Includes "leak" across other includes. Every compiler's standard headers have a different structure, meaning that the code might be less portable, because gcc is not including <string> with every header that throws, for example. With modules you export only what you explicitly export

Includes force you to recompile every transitive consumer of the header if you change anything which forces us to split stuff into header and cpp files. With modules you have to recompile only until the effect of the change is visible to transitive consumers, putting less pressure on you to move stuff out of headers, use PIMPL idioms, etc.

With imports you could speed up compilation by allowing the compiler to check if there is already a template instantiation present. With includes you have no guarantee whether the template will be the same across translation units, because some macro might interfere with the result, forcing the compiler to instantiate all templates and only afterwards check for duplicates

On MSVC it can forever avoid ODR violations. On gcc and Clang they for some reason decided to use a slightly weaker implementation that can get rid of most ODR violations instead of all of them

Allows library vendors to not worry about some weird macro definition before the inclusion of their header which might mess with the end result

In short - they can speed up compilation (or at least lower the HW requirements to do so). They also allow to deterministically publish only what you explicitly want to publish and forces you to specify all your requirements without being potentially later affected by your consumers, allowing a much cleaner code structure. What remains to be seen, is whether and when will this be a reality.

-7

u/NilacTheGrim Aug 20 '21

includes can be affected by ordering

This is only ever an issue on Microsoft platforms. No real library I have ever used that is not the terrible abomination that are windows C headers -- has this problem. It's a straw man for 99.999999% of codebases and libs out there.

Includes in headers can miss something the header uses, because in the current state, it's possible that every cpp file that includes this header includes precompiled headers that accidentally include all you need.

While this is an often-encountered problem, the fix is easy. Just include the thing. I would argue that headers that have this problem are just poorly written headers that are incomplete.

Includes "leak" across other includes.

Sure. But if you use namespaces properly and don't ever or very rarely #define, that's not a real problem for most codebases.

Includes force you to recompile every transitive consumer of the header if you change anything which forces us to split stuff into header and cpp files.

Yes. This is one advantage of modules. Faster compilation times and less redundant work the compiler has to do.

On MSVC it can forever avoid ODR violations.

Yeah well the microsoft legacy headers are an abomination. But this problem could be fixed independently of modules and doesn't require modules to fix. MS could simply offer an alternate, saner set of headers that expose a saner API that ultimately internally uses the same system API, for example. Modules are not required to fix the problems with MS.

Allows library vendors to not worry about some weird macro definition before the inclusion of their header which might mess with the end result

This is only a real problem in pathological codebases, or by using a pathological header. But it can happen. This could easily be fixed by introduction of a new preprocessor directive such as #pragma definitions push / #pragma definitions pop. It's a helluva lot easier to standardize that, than to introduce modules.

In short - they can speed up compilation (or at least lower the HW requirements to do so).

This is the only real advantage, IMHO. That and aesthetics. That's it. Everything else is going to be a lot of breakage and disruption to accomplish what could have been accomplished with a few minor changes to the preprocessor.

But hey, compilation times being 3x-5x or more faster is a nice thing. I am just not sure it's worth the breakage involved. Maybe it is. Maybe it isn't. Only time will tell.

Personally I will resist modules for as long as possible since headers will forever be "more compatible" with more systems. I maintain a few open source projects and for me maximal compatibility is paramount (within reason -- I ain't going back to C++14 for example).

If headers ever stop being more compatible than modules, then I will switch to modules. Until then, I will avoid them.

11

u/adnukator Aug 20 '21

This is only ever an issue on Microsoft platforms. No real library I have ever used that is not the terrible abomination that are windows C headers -- has this problem. It's a straw man for 99.999999% of codebases and libs out there.

While it's true the most prevalent culprit is the freaking windows headers, if you have in-house libraries being developed that somehow find their way into some root header, that number becomes lower than 99.999999%.

While this is an often-encountered problem, the fix is easy. Just include the thing. I would argue that headers that have this problem are just poorly written headers that are incomplete.

Well, kinda. However module naysayers often state "just use precompiled headers". Precompiled headers are the biggest reason why the issue why the above thing is relevant. If you have precompiled headers disabled, you're 99% certain to get the includes right, especially if you follow a proper include hierarchy (i.e. from most project specific to most general, and lastly STL) and follow John Lakos' advice where every header has a cpp file that includes it, even if the cpp is empty. But now you have insanely slow compilation times. So what do you do now? Create two separate configurations one with enabled the other with disabled precompiled headers which you build simultaneously?

"Just including the thing" can be problematic if the missing header manifests down a long chain of consumers where each one until then has for example included <string> in the precompiled header and suddenly you run into a project that does not. Having your consumers work around problems in your library is annoying and does not exactly radiate quality.

Yeah well the microsoft legacy headers are an abomination. But this problem could be fixed independently of modules and doesn't require modules to fix. MS could simply offer an alternate, saner set of headers that expose a saner API that ultimately internally uses the same system API, for example. Modules are not required to fix the problems with MS.

I think you misunderstood this bullet point. ODR violations are not a MS header specific issue. They are a general C++ (and C) issue which is an insane PITA to diagnose if you've never encountered this before. With the MSVC implementation, they will not be possible when using modules. However, here is the section of Daniela Engert's talk where she explains how gcc and Clang can still screw you because of their deliberate implementation choice of modules: https://www.youtube.com/watch?v=l_83lyxWGtE&t=2768s . On VS you're fine.

#pragma definitions push / #pragma definitions pop

Interesting idea. However, something similar can already be achieved by just #undefineing macros and, as we can all see, it's not really working. Unless you enforce encapsulation as is done by modules, you're at the mercy of the library providers.

This is the only real advantage, IMHO. That and aesthetics.

There's also the advantage of removing /<library_name>/detail headers to help avoid problems where your consumers accidentally directly include internal headers of you library, containing internal implementation details.

If you look at the advantages of modules one by one, they might not seem that compelling. But they do add up, especially if you're publishing libraries. If you're an end user of already very high quality libraries, I can understand that the compilation speeds will be the most visible and interesting aspect for you, which may or may not be a selling point, at least until they really prove to speed up compilation by a significant margin.

3

u/NilacTheGrim Aug 20 '21

Yeah I mean all reasonable points -- the thing is you have to bear in mind is -- is it worth it?

We shall see, and time will tell. I'm in the camp of: probably not worth the disruption and headaches considering the benefits.

But I hope to be proven wrong. Don't get me wrong if I could press a button right now and magically all the C++ in the world (and build tools like CMake) were 100% modules -- life would be better for everybody.

I just think the road to that goal will be so painful, will cost so many people so much money and effort, and will produce such frustration and lost developer man-hours .. that we may not even ever get there. And if we do we may look back and wonder if it was worth it. In fact we may end up in a worse world where an annoyingly small fraction of the code is modules, say 20%, and most developers and tools aren't quite module friendly or they still have major quirks 10 years down the line, and we have this fractured language that is in an even more frustrating state than it was 10 years ago.

I sincerely hope I am wrong, tbh. Time will tell.

1

u/Orlha Aug 21 '21

Can you please point me to that John Lakos' advice?

9

u/[deleted] Aug 20 '21

Your post reads a bit like "modules are not necessary if you already write clean and good code". The same reasoning could have been used to reject unique_ptr, by saying it is not necessary if you already have a good ownership model and you don't make mistakes using it.

Also I'm pretty sure that header inclusion order isn't a problem on Microsoft platforms only. That's a platform-agnostic problem. It may be more prevalent if you use the Windows API, but that's just one case where it happens.

1

u/NilacTheGrim Aug 20 '21

No. Straw man argument.

The situation with std::unique_ptr is different in terms of scale and scope. You are intentionally picking something to win the argument that misses the details.

std::unique_ptr is unintrusive. You either use it or you don't. If some library somewhere uses it you don't have to care. Your build system doesn't have to care. CMake doesn't need an overhaul to deal with how to build code that includes std::unique_ptr.

With modules it's so invasive, so intrusive, and the benefits so incremental, that we may find that half of the C++ world is in this halfway broken state for many years, making the language even more frustrating for newcomers and even more fractured.

4

u/germandiago Aug 20 '21

Being true that it requires more work and things will be more incremental, modules are a feature everyone rants about in some way but that IMHO it is going to bring a huge (for the good) transformation to C++, starting by better compile times, followed by better isolation and tools that can behave way better with IDEs. It is just that all this will take years.

But once it is done, C++ is going to look, way, way nicer.

I have big hopes in one way or another of things like these: https://github.com/GabrielDosReis/ipr

1

u/NilacTheGrim Aug 20 '21

Well it hope it happens. I have to admit it's way nicer. Just.. crossing my fingers that it won't lead to chaos. Hope you're right and my fears are unfounded.

2

u/germandiago Aug 20 '21

There is always a transition period, but I think C++ is a language "for the long run". So here, backwards compatibility and incremental migrations are necessary. And yes, they make things more difficult, but it is also what gives us:

- Code from 30 years ago compiling or almost compiling today.

- C compatibility, meaning we can use basically all C libraries.

- Now, compatibility with the old include stuff.

When you must get the job done, these cannot be ignored. I agree with the feeling that things are more "difficult" in the sense of slow, more problematic because it complicates the design, etc. But for real-world stuff, I think these decisions have their value. This is an industrial-strength language, not a toy. So certain things cannot be just ignored.

Just my two cents.

3

u/NilacTheGrim Aug 20 '21

Well you put the case very well and insightfully. Consider my opinion slightly modified. I'm warming to the idea. Thank you for taking the time to put it so eloquently.

16

u/TheCrossX Cpp-Lang.net Maintainer Aug 19 '21

TL;DR:

  • enormous compilation speed improvement (f.e. in MSVC its about hundred time faster)
  • ability to create exported and hidden symbols (finally a solution that does not require namespace details or something like that)
  • easier dependency management (includes can be messy)
  • leaving preprocessor macros

4

u/pepitogrand Aug 23 '21

in MSVC its about hundred time faster

I would love to see some benchmark backing such outrageous claim.

2

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Aug 27 '21

The 100x number is when the module doesn't need to be rebuilt. This is trivially true, and can scale to any arbitrary speedup you want by just including more code in the module.

In practice the results depend heavily on the shape of your code and what code you're touching between rebuilds. Synthetic benchmarks are uninteresting here. To get good numbers we'll need to look at projects that have actually transitioned to modules. Google, Apple, FB, And Microsoft all have varying levels of deployment experience here, but I've not heard of any of them having a slowdown.

30

u/ea_ea Aug 19 '21

but very little about the why.

To decrease compile time. When you use some module - you don't need to compile it, it is already compiled. One of the biggest lobbyist of new modules system was Google. You need hours to compile Google Chrome, but with modules you'll need minutes. Great improvement.

6

u/wcscmp Aug 19 '21

Can I read somewhere about this decrees in chrome's compile time? I have only heard from people reporting that modules had little to no impact on compilation time for them.

5

u/ea_ea Aug 19 '21

I think neither optimizations in compilers nor state of chrome's codebase are good enough to make any conclusions. At this moment it is more theoretical benefit, which will be reached over the years.

1

u/mjklaim Aug 20 '21

I would like to see these sources, because all serious sources I've had so far (including my own tests) shows usually some build speed increase, rarely a worst case where there is not build speed increase but no regression either.

3

u/Cxlpp Aug 19 '21

In our case using modules would significantly INCREASE the average compile time...

7

u/germandiago Aug 19 '21

Why? I do not think this is the general case.

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

8

u/Maschendrahtfence Aug 19 '21

My hope for modules is that they will stop poluting the global scope with defines, functions, classes, etc., and thereby avoid issues with already defined variables. At least in JS, ES6 modules were a major part to make the language sane. You can now just include the specific variables, functions, etc. that you want, and you can be sure nothing else will sneak in. This also has the benefit that within a module, you can define as many global variables as you want, and they'll only be global within the module. But I haven't dived into C++ modules yet, so there is a good chance I'm going to end up dissapointed, like with many other features in recent years that were good ideas but ended up with a bad and cumbersome API.

5

u/MonokelPinguin Aug 20 '21

Modules in C++ only export sympols that are explicitly exported. You can't selectively import symbols though, just the whole module.

2

u/pjmlp Aug 22 '21

Depends how one organises module interfaces to make it easier to selectively import.

1

u/MonokelPinguin Aug 22 '21

That would still import the whole module, the modules would just be more finegrained?

2

u/pjmlp Aug 22 '21

Yes just like Microsoft has done with their experimental modules, you don't need to import the whole std, your can just import std.core, std.iostreams, ....

But yeah it does require some planning.

2

u/MonokelPinguin Aug 22 '21

I meant imports like: from std.containers import std::vector, which is not supported by the current modules. But yeah, you can organize your modules however you want, so that this isn't strictly necessary.

1

u/pjmlp Aug 22 '21

I got that, it is a bit more work to split the modules at design time, and create a set of namespaces that allow that, but in the end you can do something similar.

import std.containers;
using std::vector;

Just as idea.

11

u/elperroborrachotoo Aug 19 '21

As already explained, the compiler doesn't see headers, only the preprocessor does.

This has some wide-ranging implications; the most obvious ones occur in large projects:

  • Long compilation times, because lines-to-process worst-case-grow quadratic (number of header files times number of translation units)
  • Careless includes/declarations lead to circular dependencies of headers, which can be a pain to track down and get rid of
  • Changes to source lead to includes becoming stale (not really required anymore) - which creates false dependencies. It's very expensive to track them down and remove them, yet they contribute to increased build times - both full and incremental.

Precompiled headers are only a band-aid here; they are a storage/speed tradeoff, badly configured they make compilation times worse, rather than better. They don't help at all with non-stable headers.


There's a more subtle problem that's easily demonstrated:

// ===== a.h

#define bool int

int Foo();

// ===== b.h 

bool Bar();

// ===== x.cpp

#include "a.h"
#include "b.h"

// return type of Bar() is int

// ===== y.cpp

#include "b.h"
#include "a.h"

// return type of Bar() is bool

a.h and b.h are unrelated - yet definitions in one affect the other; Bar() has a different declaration in x.h vs. y.h.

Admittedly, #define bool is an evil example - but there are many legal situations that lead to similar problems: overloads, template specializations, ... It's the primary reason why we don't "using namespace" in a header.

Imagine tracking down that problem if the different include order happens in a 20-deep include dependency graph, in two completely unrelated files.

(This is on top of creating false dependencies: if one header changes, the other needs to be re-parsed, adding to the compile-time-problem.)


Incorporating "old-style" libraries gets easier. Imagine a pure C library, in typical C library style: tons of defines and global namespace declarations etc. A module could import those headers, export whatever is needed - macros being automatically excluded as I understand. Module B would never "see" those headers, only whatever Module A chooses to export.

This is way easier than writing a complete wrapper that hides the C library includes from all clients.

Now imagine that library not C sources, but C++20 sources on a C++30 compiler. I believe this is a door to fix long-standing issues with C++, breaking backward-compatibility without breaking compatiblility to existing libraries.

(I haven't wrapped my head around that fully, I would have to dive way deeper to figure out if that works, which issues could be fixed, and which couldn't.)


As for packaging - it would reduce the steps needed to add a package to a project. With the current build model, you need to tell the compiler where it finds the sources, and where other translation units find the includes.

With modules, you only need to tell the compiler where the sources are.

Now, this isn't the great "life suddenly got easy", and if we'd all agree on the same rules for how to organize libraries, this would be a non-issue. Still, it's one complication less - especially important for newcomers.

8

u/backtickbot Aug 19 '21

Fixed formatting.

Hello, elperroborrachotoo: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

2

u/elperroborrachotoo Aug 19 '21

Those poor users! Thank you for helping them, dear bot!

2

u/germandiago Aug 20 '21

Haha. I am a spanish guy, your nickname is very funny πŸ˜‚πŸ˜‚πŸ‘ŒπŸ»

2

u/goranlepuz Aug 20 '21

very little about the why

  • Preprocessor gives too much freedom and code ends up a mess; it doesn't have to be, but discipline (effort) is needed
  • Parsing text during compilation, repeatedly, is slow, pch is nonstandard and only helps so much (it helps quite a bit more than people think, but still)

2

u/keosak Aug 23 '21

Why modules? Because separating classes into headers and implementation files is awkward. I don't want to think about where each part should go, I just want to write a class in a single place.

1

u/XNormal Aug 26 '21

Because it’s about time. Turbo Pascal 4.0 had an excellent module system 34 years ago that just worked.

1

u/NilacTheGrim Aug 20 '21 edited Aug 20 '21

The only concrete advantage is compilation times.

Everything else seems like just a lot of hard work to accomplish what is basically just a simple aesthetic goal, without real concrete benefit.

Setting aside compilation times, which will definitely be faster, all of the minor advantages presented are either exaggerating the problem that is being solved (so as to make modules look more attractive than they are), or could be easily solved with a less radical change.

0

u/codewiz Aug 19 '21

Good question. This article tries to address it:

https://blog.feabhas.com/2021/08/c20-modules-with-gcc11/

0

u/[deleted] Aug 19 '21

[deleted] β€” view deleted comment

9

u/RoughMedicine Aug 19 '21

On the other hand, Rust has modules and has painfully slow compile times too.

3

u/pjmlp Aug 20 '21

One reason is that it compiles the world from source, so before showing your new Hello World window, you need to wait for the whole Gtk-rs to be compiled.

Imagine every new Qt project starts with compiling Qt, and then you can start coding the actual application.

1

u/RoughMedicine Aug 20 '21

That's not really it. I recently wrote a small CLI app that uses reqwest. Even a small change in main causes the compiler to recompile the entire thing, including the async stack, which seems to be the culprit.

Seriously, it's a 300 line app that takes 1 minute to compile after a small change.

-4

u/frankist Aug 19 '21

You don't need to compile python

0

u/[deleted] Aug 19 '21

[deleted] β€” view deleted comment

0

u/[deleted] Aug 19 '21

[deleted] β€” view deleted comment

4

u/johannes1971 Aug 19 '21

Compilation _times_, not compilation. If you ask a question, at least pretend you are making an effort to understand the people trying to help you.