r/cpp • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
280 Upvotes

240 comments sorted by

View all comments

Show parent comments

4

u/pjmlp Jan 28 '19

Parsing is the problem you are missing.

Java only parses the source code once, then any client of a specific library only needs to read the public symbols table from the jar file.

Whereas in C++ the header file will be processed every single time there is an #include. With the increase in template usage, this leads to logarithmic complexity in build times.

There are pre-compiled headers, but they are a brittle solution with different outcomes depending on the compiler.

0

u/oldprogrammer Jan 28 '19

I understand the compilation model, but I'm not really understanding how this is an issue. Java is still technically parsing the .class files as well it just happens to know where the specific symbol tables are in the structured format.

3

u/Ameisen vemips, avr, rendering, systems Jan 28 '19
  1. Class files are effectively self-contained in Java. Bringing in a class file doesn't necessitate loading and parsing other class files at compile-time, as they only have thin references to other classes.

  2. Class files are already parsed and compiled. Bringing in class files in Java is effectively similar to linking in C++ (at runtime), and at compile-time is similar to bringing in a precompiled header.

  3. Java mandates type-elision. You cannot have deep, nested templates in Java that eat up compilation time, as generics don't work that way in Java. Java generics are way weaker, but way faster to compile.

When you #include a header file, you are literally copy-pasting that header file's code into your code. Any #includes in that header file are also handled that way, so every translation unit ends up massive. This is all raw source code that must be parsed and compiled.

For compilation, in Java, another class is looking in a precompiled (though not optimized) class file looking for symbols. That's it. The symbols are already there, in a nice, easy-to-read format. When you are loading them, it's similar (the JIT handles the rest underneath).

The equivalent, as said, would be precompiled headers in C++, but even then it doesn't eliminate the template issue.

Imagine if Java (a magical dialect that allowed this), instead of just looking into thin classes, copy-pasted the Java source for every class you imported. Any imports of that Java source also were copy-pasted recursively. Every time. Now imagine this dialect didn't have type-elision, so your generics, instead of compiling to a single implementation, generated a separate implementation every time you instantiated it with a different class. Now imagine it also allowed primitive types.

4

u/pjmlp Jan 28 '19

I don't need to imagine.

Eiffel, Ada, .NET, Delphi, D all support modules, have generics without type elision, do support primitive types and compile blazing fast compared with C++.

1

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Yeah, but they don't do textual import.

3

u/pjmlp Jan 28 '19

Which is the whole point of having modules in first place.

1

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

But I said imagine a language that also did textual import.

2

u/pjmlp Jan 28 '19

Ada, Delphi and D also support textual import.

1

u/oldprogrammer Jan 28 '19

I completely understand everything the class file brings and how the preprocessor works I was trying to understand why it is a problem, but I think you answer it in a sidewise fashion

but even then it doesn't eliminate the template issue.

I don't generally write templates. I'll use them from things like the STL but for myself I don't do any template programming. For me the header is generally nothing more than a typical interface definition and maybe some constants so including that in the compilation unit of a source file isn't a big deal.

So modules are a solution to over complicated template programming?

3

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Modules are a 'solution' for the fact that #include is really very brute force, and tends to bring in a lot of implementation details (including includes and such) - think when you #include <Windows.h> - it brings with it all those macros, etc.

Modules only expose what you export.

1

u/oldprogrammer Jan 29 '19

Isn't that just an issue with the structure of the header file though? If I look at the windows.h file in the MinGW distribution it is a master header file that has a collection of conditionals that include other header files as appropriate. That type of conditional inclusion could also be required in a source file importing modules.

If you drill into one of the included files as an example, winnt.h what you see is a collection of forward reference function declarations, typedefs, structures and constants defined. The macros are in-line replacements for functions so either you use the macros or define additional function calls.

Even in a module approach you need the forward declarations and structure declarations. You need the typedefs to go with the function declarations. You could ignore the #define constants but then you'd be using magic numbers or magic strings in your code.

If there are details in these headers that should be hidden and only available to the windows library that it goes with then those source files could include both an internal header and the external header.

If the information is needed by consumers of the windows library then it has to be defined in the header. So you have the same level of control over what is exposed do you not? What is magical about modules that makes them so much more needed than what is currently available?

I'm trying to understand exactly what problems modules are looking to fix that a properly defined header file and associated .so /.dll file don't already provide?

1

u/Ameisen vemips, avr, rendering, systems Jan 30 '19

I don't need 99% of the symbols Windows.h brings in, some might conflict with other code, and you cannot namespace it or otherwise hide it.

Headers make it difficult to hide implementation details, and make it easy to pollute the namespace. Modules resolve that.

And, with modules (or precompiled headers), I am including Windows.h once, and exporting the things that I want. Much lighter to bring that module into every TU than to include that header.

1

u/oldprogrammer Jan 31 '19

You might not need them but if the module developer is exporting them because others might need them you get them anyway, right?

1

u/Ameisen vemips, avr, rendering, systems Jan 31 '19

Yes, but they're then contained to that module rather than the global namespace.

1

u/oldprogrammer Jan 31 '19

Isn't that just the same thing that happens if you use C++ namespaces inside header files?

→ More replies (0)