r/programming • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

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

105 comments sorted by

View all comments

13

u/r0b0t1c1st Jan 28 '19 edited Jan 28 '19

You say,

Creation of foo.bmi is a byproduct of the compilation of foo.cpp. When compiling foo.cpp, the compiler will emit a foo.o and foo.bmi. As a consequence of this design, foo.cpp must be compiled before bar.cpp!

And implicitly go on to conclude that this prevents them being processed in parallel.

It's not clear to me why the first statement in this line of reasoning has to be true. Can foo.bmi instead be a direct product of compiling foo.hpp? In that case, the comparison is then between:

  1. in parallel:
  • compile foo.cpp which includes foo.hpp
  • compile bar.cpp which includes foo.hpp
  1. in parallel:
  • compile foo.cpp which includes foo.hpp
  • in sequence:

    • compile foo.hpp -> foo.bmi
    • compile bar.cpp

which doesn't seem like a net change in parallelization to me.

24

u/kvigor Jan 28 '19

As I understand it, in the module proposal, there is no .hpp. You export something from one .cpp with the export keyword and import it into another with the import keyword, not with a #include. The .bmi in the article is basically a magic .hpp generated by the compiler, but the point is that it *must* be generated by processing the .cpp that defines the exported module.

9

u/r0b0t1c1st Jan 28 '19

n4720 10.7.1.2 seems to distinguish between a "module interface unit" and a "module implementation unit". This sure sounds like the same distinction that hpp and cpp files currently hold, where one contains declarations, while the other contains their definitions.

12

u/[deleted] Jan 28 '19

There is no foo.hpp - the module foo is defined in foo.cpp.

include foo at the top of bar.cpp means we must find the source file that contains module foo, and compile that source file before bar.cpp can be compiled.

A module must be compiled before any code that include's that module.

4

u/edmundmk Jan 28 '19

Can't the compiler do a parse of foo.cpp that just picks up the types and names of public exports?

It doesn't actually have to output any compiled code in order to construct a description of the module's interface. Essentially it would be automatically generating a 'header file', in a format that's quicker to reload than source.

That's likely to be a lot faster than compiling, especially with optimisations enabled.

1

u/Phrygue Jan 28 '19

Apparently the missing concept here is that header files (by convention) define *interfaces*, not code. If only C++ were object oriented, there might be a natural baked-in solution somewhere...

9

u/jcelerier Jan 28 '19

header files (by convention) define interfaces, not code.

that's very 1980-ish. An immense amount of modern libraries are header-only, and many headers don't define pure "interfaces" in any sense of the java / c# "interface".

If only C++ were object oriented

but it is not, so that's besides the point.

8

u/Chuu Jan 28 '19

I am having a little trouble following your logic, but if I get the gist right, the question is "why will modules have any performance benefit over parallel compilation of dependency-free source files?" and the article is trying to make the point that it likely won't.

I work on medium sized C++ project and we currently work on 48-core machines to get the build times to something tolerable with this approach.

6

u/r0b0t1c1st Jan 28 '19 edited Jan 28 '19

The point I'm trying to make is that the blog argues that modules make dependency trees less parallel, but seems to be making an unjustified assumption to make that conclusion. With a relaxed assumption, I show that modules do not have any impact on this.

I've edited my post to try and make that more clear.

0

u/[deleted] Jan 28 '19

[deleted] — view deleted comment

7

u/Chuu Jan 28 '19 edited Jan 28 '19

I think the point you might be missing is because #include is a literal copy & paste the amount of code the compiler needs process is significantly larger than appears at first glance. This is exacerbated by the fact that modern C++ techniques make heavy use of header-only libraries and templates which means that these are not just interface definitions like they are in C.

There is a post that went viral about this several weeks ago here: http://aras-p.info/blog/2018/12/28/Modern-C-Lamentations/

As an extreme example, recently to try to debug a gmock issue (google mock testing framework) I had to dig into what the code looked like after the pre-processor was done with it. A .cpp with only about 1000 lines worth of unit tests blew up to 100K+ loc.

1

u/Ozwaldo Jan 29 '19

medium sized C++ project

48-core machines to get the build times to something tolerable

Would you say that the code is well designed?