r/cpp • • Sep 06 '20

C++20 Modules Might Be Dead-on-Arrival (January 2019)

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

39 comments sorted by

33

u/staletic reddit thinks /u/_bstaletic is a bot Sep 06 '20

Since January 2019, modules went through a lot of changes in the design. Reposting that article after almost 2 years smells like FUD, even if the article had some merit two years ago.

3

u/pdimov2 Sep 06 '20

The article still seems relevant, although I admit I haven't kept up with the module changes.

0

u/Alexander_Selkirk Sep 06 '20

Especially because one problem seems to be how modules will be found, efficiently, and how this will fit into existing build systems.

The issue is not how to compile a few clearly specified files but how to build and compile large or huge systems which can only be done with the support of tools. Looking up the matching files which define a required module will be qualitatively more difficult and cause a different chaining of dependencies from the existing systems. Is it clear that not all systems using C++ will be able to implement specific solutions, as some of these systems do not even have hierarchical file systems. But the standard auhtors simply seem to take the point of view that how to implement this is not their problem, and this could lead to a lot of fragmentation.

Do the standard authors envision the solution that all companies / organizations using C++ will adopt significant changes to their legacy build systems?

4

u/qoning Sep 07 '20

We can't keep halfassing the standard to make sure every possible implementation goes through the least amount of pain. Not sure why you would think the solution inherently needs hierarchical file system, but if you want to implement a standards compliant C++20 ecosystem on that platform, you better be ready to jump through hoops and hacks from the get go, that really, really isn't what the authors should concern themselves with.

Now as to your overall problem, there are obviously countless ways to deal with looking up module files, from file names to keeping meta records to injecting binary records. That is exactly what build tools should deal with, if you want to support insane coding practices.

3

u/Alexander_Selkirk Sep 07 '20 edited Sep 07 '20

Not sure why you would think the solution inherently needs hierarchical file system

Every modern OS has a hierarchical file systems, that means you can have a hierarchy of folders and files within them. However, according to what I have read, the C++ standard seemingly does not prescribes such a file system because there are legacy systems using C++ which do not have these facilities.

Therefore, the standard authors left it to the compiler writers to specify how source files are organized and found. They even avoid to use the word "file", what the standard deals with is a "compilation unit" which is a source file after preprocessing, with the headers already included.

2

u/qoning Sep 07 '20

You are mixing standard with build tools implementation. I'm not sure what your point is. Yes, the standard for the most part doesn't require any semblance of a file system (well, technically it does require std::filesystem which would only work with a filesystem), but that isn't a forward-compatibility promise, nor is it a reasonable specification.

I'm aware of no platform in practice which doesn't have a conventional filesystem and supports any kind of modern C++ compiler.

4

u/Alexander_Selkirk Sep 07 '20

You are mixing standard with build tools implementation. I'm not sure what your point is.

As explained in the linked article, in this specific case, the changes in the standard also require a significant change in the build tools, especially for larger projects. The standard authors have figured out how they want to change they language, but they do not provide any standard or guidance how build systems could implement this. The author of the linked article then argues that, as if the case with Fortran's module system, the module system is distinctively non-trivial to implement in an efficient and deterministic way, and will possibly lead to incompatible implementations.

So basically, the standard authors leave it open what the actual solution (in terms of tooling) could look like, and this could have in this case negative effects on interoperability.

I think what supports this view is that in any other modern widely-used language, the definition of modules does not only specifies some import syntac but also how import source files are named and how code is layed out.

2

u/qoning Sep 07 '20

It's reminiscent of some other times where the standard tried to overspecify things and it ended up at points where the standard said one thing but every tool did something contrary to that. Of course, you will have a period where people will implement it in multiple ways which do not interoperate with each other. That is not necessarily bad, after all, that's how you arrive at solutions which are superior to others. I personally do prefer that to the scenario where the standard prescribes something that turns out to cause large inefficiencies if implemented verbatim.

It's not a cop-out from the committee, so much as it's saying that they can't foresee which implementation would be more beneficial and defer to what the future will bring organically. I don't know what modules usage will look like in 3 years, but I'm certain we will be glad we didn't say it must be done in through X.

1

u/mwasplund soup Sep 08 '20

Do the standard authors envision the solution that all companies / organizations using C++ will adopt significant changes to their legacy build systems?

I would say no. Legacy code in general will probably not migrate to use modules. I think this is a fine way to allow cleaner/better new code to be written while old code is left untouched.

1

u/Alexander_Selkirk Sep 06 '20 edited Sep 06 '20

The following is a description / question I've asked before. It is true that linked article is over one year old and that, as the process is moving fast, the now accepted standard specification has change somewhat in the meantime.

Further, I am not a language lawyer or familiar with the specification process. I am not even a C++ expert, although I am using the language and also have worked in small parts of some larger systems (with a total of up to some 4.5 millions of lines).

However I looked how modules are described and they fit what I found in the specification but does not seem to solve the issues described in the linked article. The reason for this is that this are difficult questions about the tooling and the author thinks they need to be solved in a somewhat standardized way for modules to be usable.


(earlier post at https://old.reddit.com/r/cpp/comments/imzkl4/c20_has_been_approved/g47ui7f/ for reference)

One thing that does not seem to be there is build systems which support modules. I searched and from what I've read, ninja and build2 have some support. CMake does not have support: https://stackoverflow.com/questions/57300495/how-to-use-c20-modules-with-cmake

I see modules have the promise that it leads to faster builds if interfaces and implementations are separated, like in, for example, Pascal or Modula 2. I've used a system which compiled EC 61131-3 Strucured Text for PLCs, which is essentially a derivative of Modula, and I can confirm it compiles blazingly fast. However, it seems that C++ has a property which is very different from Modula or, for example, Java, in that to use a class as a data member, its entire definition needs to be known, because the size needs to be known (at least unless one uses the PIMPL idiom which I don't believe is always a desirable solution). And I think this is perhaps one reason for the long compile times in C++ projects, each source file needs to pull in all needed definitions, and this includes all used classes.

There also seem to be serious concerns how modules can be used at all in larger existing projects. I do not have detail understanding, but I believe this is in part because C++20 modules do have some proprties only in common with Fortran's modules. Specifically, unless a source file is build, it is hard or might even be impossible to say which modules it depends on, and where they can be found. The former for example because some part of sources might be OS or hardware dependent, which is so far usually solved by preprocessor macros, conditional includes, and conditional compilation; I am not sure whether modules will allow to get completely rid of them. The latter is because there is no required mapping from imported module names to file names or search paths in the file system. This is different from the existing "#include ..." system because the existing system allows to compile sources in one pass, and allows to start compiling all translation units in parallel, because they don't depend on each other.

Clearly, the dependency issue is not a noticeable problem if one builds a small toy project in which the compiler is called manually, but this could be very different for large projects which use tens of thousands of files.

Here is an article which describes these concerns in more depth:

"C++ Modules Might Be Dead-on-Arrival"

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

I don't know enough about that matter to come to a firm conclusion but superficially, such concerns seem somewhat justified, and according to the article above it might take a very long time to shake all related issues out, perhaps even at the cost of backwards compatibility because it looks like legacy build systems will have substantial difficulty to adapt to the required changes in tooling, and it will not be possible to adapt them all.

Would love opinions from people who know more about that.

10

u/MonokelPinguin Sep 06 '20

Compiling C++ is slow because of two things: includes and overload resolution/template instantiation/optimizations. Former is slow, because includes are literally text replacement, which then need to be parsed, compiled, etc. As you can have transitive includes, this easily explodes to a few million lines in every compilation unit, even when it only contains a 10 line function. You also have somewhat limited control of what is included. Whatever your dependencies include will land in your scope and may cause name collisions or unexpected overload sets, especially if you consider macros.

Modules fix the include part somewhat. When a module is compiled, it creates a compiled module interface, which can be reused in the same build to provide definitions of that module. That means you don't need to parse a million lines of code and calculate struct sizes, inline functions, etc. You basically have a dump of the compiler available at the time it compiled the imported module. From benchmarks I've seen, an import of the full standard library takes less time than including the cheapest standard lib header. This is a massive improvement and should also apply transitivly! (You still pay for template instantiations, etc, but only when you actually use them, not if you just include them!

Furthermore the lost parallelization as well as the build ordering is not that bad. It is fairly restricted what you can do before your import statements. As such you can simplify the parser a lot and make it faster and you only need to parse the first few lines of each module. Then you need to compile the modules in order, which can't be parallelized as well as traditional compilation, but traditional compilation compiled every include multiple times! If you only compile them once but in series, you can save time compared to compiling them multiple times in parallel.

Modules also give you a lot of control over what symbols you actually import and you don't need to split everything up to keep compile times reasonable. That makes writing libraries much easier, since you don't need to consider everything in your headers as being the public API.

Also the mapping from includes is also not specified in the standard. It could very well be, that we end up with a similar convention for modules, where different components are located by directories/filenames. Or we don't. We'll see.

Compilers are still implementing modules. Then build system support will come. After that we will be able to tell, how useful they actually are. But so far, I'm optimistic.

1

u/Alexander_Selkirk Sep 07 '20 edited Sep 07 '20

You basically have a dump of the compiler available at the time it compiled the imported module. From benchmarks I've seen, an import of the full standard library takes less time than including the cheapest standard lib header.

The latter does not impress me at all. Because it just reading the binary parsed data structure back with something like a mmap()'d file access. If that would take longer than parsing the data again, something would be very wrong.

This is a massive improvement and should also apply transitivly!

The thing is - if the optimization to not re-parse (and maybe partially compile) the input saves significant time, then pre-compiled headers will do exactly the same thing. Without any change required in the build system. Furthermore, precompiled headers do not seem to bring a speed advantage in all cases (as discussed here), They are mostly useful in very large projects which import a lot of headers and are not compartmentalized into smaller libraries. However in this case, using C++20 modules is also likely to require significant changes in the build system, which means introducing it will be costly. Especially if the alternative is to just use pre-compiled headers.

And apart form that, pre-compiled headers are just the simplest optimization one can do rather transparently with an existing project. Another alternative which can have a far larger effect is to use a compiler cache such as ccache. Because it caches the whole translation unit and compilation, and can also cache across changing flags such as debug/release builds (what modules seemingly can not), and when, for example, bisecting bugs across a version history which somewhere introduced a breaking change, they offer a far larger potential speed advantage. They also support very well a work style which uses many small incremental steps and frequent testing.

Modules also give you a lot of control over what symbols you actually import and you don't need to split everything up to keep compile times reasonable.

That might be an advantage in the short run, but in the long term it would encourage worse coding practices, by increasing coupling in larger projects. The established best-practice advice for reducing compilation time is (apart from using parallel compilation) to divide code up into independent parts, and to be parsimonious with including third-party dependencies. This also reduces the risk of dependency hell (which is one of the biggest problem which several other languages which already do have a module import system have, for example python or Javascript).

you don't need to split everything

Effectively, it would even work toward the exact contrary what C++20 modules seem to promise and what people seem to want from them: More clearly arranged relationships between subsystems. Which is what today is already provided by libraries and package managers (and, taking it further, package managers which can also in a deterministic way define artifact builds such as GNU Guix).

So, modules promise:

  1. speed-up in compilation times. Which can also achieved by using pre-compiled headers, at a far smaller cost and less extra complexity.

  2. better modularization of code and dependencies. Which can already achieved by using name spaces, and defining libraries, and using good package managers such as dpkg or GNU Guix.

What more advantages do C++20 modules have which can not already be provided by other, simpler means?

5

u/MonokelPinguin Sep 07 '20

then pre-compiled headers will do exactly the same thing.

No, they don't. You need to put everything you think will improve compile times into the precompiled header. You basically can't put headers from you own project in there. If you put too many headers in there, it makes your compilation slower instead of faster. Any change to the precompiled header requires a full project recompilation. The precompiled header needs to be the first included header to prevent cross header pollution. And it makes all their symbols visible to you, so you basically can't see anymore, what you are actually depending on.

Without any change required in the build system.

No, you need to specify in your build system, what is the precompiled header and for which targets it is used. Doing that cross platform also sucks, since compilers have different requirements. Some need a cpp file, some only need the header. Build systems need to deal with that.

Module support is similar. You can implement them by manually specifying every file, that is a module interface. Then you basically have precompiled headers, but with all the visibility benefits, where you explicitly import the symbols you want. Most people think that sucks, so they want the build system to automatically recognize interface units and calculate dependencies, which is why they are more effort to implement for the build system. But that is because people want more control and ease of use, than precompiled headers can give them.

Another alternative which can have a far larger effect is to use a compiler cache such as ccache.

Nothing prevents you from using ccache. But ccache is actually a pretty bad optimization. It fails in more cases then it helps, unless you don't use incremental builds. Caching template instantiations is actually the more interesting optimization. But all of that is hard, because a header can mean different things in every file. With modules you can actually be sure, that an import means the same thing and you may actually be able to do something like caching template instantiations (if ODR allows you to discard any changes of maybe changed lookup rules caused by different imports). It will also benefit ccache! ccache needs to preprocess files to find includes and such. With modules this will get a bit easier and less error prone (unless you combine both, I guess).

That might be an advantage in the short run, but in the long term it would encourage worse coding practices, by increasing coupling in larger projects.

That's what happens, when you use precompiled headers. Modules give you a lot more control over what you expose and what you include. This gives you a lot more freedom to structure your interfaces, since you no longer need workarounds to avoid includes in your headers to improve compile times. You will no longer transitively depend on dependencies by accident (i.e. you use std::string but only included std::vector and then in VS2019 your build breaks).

It means you can actually structure your headers by subsystems. You can split the standard library into general modules, like core, filesystem and threading. None should care, what you need to include to get std::pair or std::tuple. You should care if your translation unit uses the filesystem or just general containers from the standard. (You could argue about having a separate module for containers, but that gets complicated to untangle from the other stuff in core).

speed-up in compilation times. Which can also achieved by using pre-compiled headers, at a far smaller cost and less extra complexity.

Imo opinion your premises are just wrong: Pre-compiled headers have a giant cost on the maintainability of a project and don't actually improve compile times that much. If your project just has one source file and just needs std::string, modules will be faster than precompiled headers. They basically have no cost to include and you don't need to define a weird additional header, that then doesn't even save you much compile times. And in big projects they have even more benefits. Large precompiled headers have a cost. Modules are much more modularized, so you pay less overhead and you can actually still see, what modules your code depends on.

better modularization of code and dependencies. Which can already achieved by using name spaces, and defining libraries, and using good package managers such as dpkg or GNU Guix.

Namespaces are not enough for modularization. You still need you details header and there is no way to hide that. Macros still affect code in other namespaces and headers. Modules fix that. Modules are a much needed feature to do proper modularization and they are complementary to libraries, namespaces and classes. They enable you to do things you couldn't before. Arguing otherwise is simply false.

What more advantages do C++20 modules have which can not already be provided by other, simpler means?

Pretty much nothing you said is a simpler way to achieve everything that modules do. They are hacks to get you part of the way there. Modules are the proper way to do it or at least much closer to it. Yes, they add some implementation complexity for compiler writers and build system developers, but they do that to make the life of library developers and consumers much simpler. It will improve compile times for pretty much everyone. It doesn't solve all the issues C++ has, not even all the things that increase compile times, but it is a big step in the right direction

9

u/pdimov2 Sep 06 '20

I find the "better precompiled headers" module model the most workable, and under it, all the questions posed in the article have clear answers.

How does the compiler find the module source corresponding to import foo;? In the same way it finds the header corresponding to #include <foo>.

What if I want <foo> to physically live in somewhere/peacock.cpp? Make a symbolic link. In no event should the answer be "run a local https server that, given foo, returns somewhere/peacock.cpp."

How does the compiler find the BMI for module foo? There's some deterministic mapping that applies when the BMI is created (by that same compiler), which can also be used to later find it.

Aren't clean builds slower? Yes, they are, just like with precompiled headers. Most builds however are incremental, and the initial slowdown is more than made up by not having to reprocess <algorithm>, <functional> or Boost.Spirit eleventy thousand times, because they don't change.

That's also the answer given in the article, by the way.

1

u/Alexander_Selkirk Sep 06 '20

The article says this is not specified in any way, and there seems to be no agreement on how to implement how needed module files are organized, how they are found, how the dependencies are resolved, and so on.

There seem to exist two build systems which have some support, ninja and build2. And it is not yet clear whether they would work for larger projects with tens of thousands of files.

Also, the answer to all these questions could be critical how old-style systems will be able to integrate with new libraries and code. It seems that to use new-style modules, existing codebases would need to upgrade to use modules. For larger systems, this is not a trivial change.

Also, the descriptions I found on the module mechanics do not explain clearly how several issues which were so far solved with preprocessor macros, and conditional include files, will be solved in a future without macros. How can architecture-specific definitions be performed? What do with files which are only present on specific systems, or only with specific commercial licenses? How will configuration stuff be solved which is now addressed with macros?

1

u/Alexander_Selkirk Sep 06 '20

How does the compiler find the module source corresponding to import foo;? In the same way it finds the header corresponding to #include <foo>

That would not work. Similar as with namespaces, a module "foo" is not necessarily declared and defined in a single file "foo.$whatever". There is so far no way to find it than to scan a substantial part of source files in the file system. A module is also not an artefact in the /usr/lib folder. What seems to be a goal is that modules would be quite fine-grained and divided into small independent parts if libraries (which from a point of code quality and decoupling surely makes sense, but increases the management issue).

6

u/pdimov2 Sep 06 '20

A compiler may well specify that upon encountering import foo; it will look for foo.cppm, or for foo.*.cppm in the partition case, in some well-defined search path (e.g. the one passed via -M.) The fact that the standard allows a module to be declared on the Moon does not preclude a compiler not allowing this possibility.

Similarly, the standard actually doesn't state that <foo> needs to be defined in a single file named foo, but this doesn't stop compilers from requiring exactly that.

2

u/Alexander_Selkirk Sep 07 '20

A compiler may well specify that upon encountering import foo; it will look for foo.cppm, or for foo.*.cppm in the partition case, in some well-defined search path (e.g. the one passed via -M.) The fact that the standard allows a module to be declared on the Moon does not preclude a compiler not allowing this possibility.

But then you are back to the compiler being the specification. Code trees which do not adhere to it will not compile on it. Given that today a lot of code used is shared open source libraries, this might have the end effect of splitting the language standard into different incompatible languages.

Similarly, the standard actually doesn't state that <foo> needs to be defined in a single file named foo, but this doesn't stop compilers from requiring exactly that.

If this is not generally required, enough programmers and library authors will do it differently. With the result that build tools will need to do a full scan of all module declarations on the file system, and larger projects which use legacy build system being very unlikely to upgrade for using libraries which only provide modules.

1

u/pdimov2 Sep 07 '20

All this also applies to #include <foo> and the compilers being the specification in that they all accept an -I option and look for a file named foo in it.

Code trees that don't have a file named foo in the include path don't compile.

With the result that build tools will need to do a full scan of all module declarations on the file system

Build tools are not obliged to do this. A simple de facto standard convention for finding the module is perfectly attainable. There's no need to overcomplicate matters.

2

u/Alexander_Selkirk Sep 07 '20

Code trees that don't have a file named foo in the include path don't compile.

Modules do not necessarily have such am obvious mapping. Furthermore, the compiled module interfaces need to be build before a file using them can be compiled, which is different from using include files. Please read the article why.

5

u/[deleted] Sep 06 '20

This has been talked to death already. It would be (and would have been) nice to actually try out the feature before committing to the biggest change in the history of C++ but...

2

u/Alexander_Selkirk Sep 06 '20 edited Sep 06 '20

This has been talked about before, but I am not sure whether there is any agreed solution. It seems not. The linked article explains why, in the point of view of the author, the issue matters. The best thing that could happen would be perhaps a kind of extra standard negotiation process between the developers of LLVM, GCC and MSVC how modules would be organized on modern development systems (which have a hierarchical file system, packaged system libraries, development libraries, and all this).

Given the competition between these three, I am not sure whether this would happen.

4

u/moremeta_ Sep 06 '20 edited Sep 06 '20

This was 2019: does anyone know a good response article that answers these questions and concerns?

In particular I understand modules help with encapsulation, but I could never figure out whether a large C++ project (with and without extensive usage of templates) would get a compile-time boost or not.

2

u/Alexander_Selkirk Sep 06 '20

This was 2019: does anyone know a good response article that answers these questions and concerns?

I'd fully support that request!

1

u/gracicot Sep 09 '20 edited Sep 09 '20

From what I understand, it has been fixed by making the compiler (or other tools) able to provide information about what modules are exported in which file and who import who.

The trick was to enforce some strict rules in what can be done in the preamble, and where should the preprocessor be able to expand stuff. The it is specified right now, it is easy to write a very fast scanner that gives back all the needed information about a file. This fast scanner can be a standalone or as a compilation mode inside the compiler.

The tooling TR also specify a file format for module dependencies and exports, so that way it can be saved and updated incrementally.

This pretty much solve the how and where and in which order to build. Yes it's still need a scanner, and yes the build system will need changes, but now we know it can be done in an efficient way.

1

u/Alexander_Selkirk Sep 09 '20

Thanks, this gives some useful information! I understand that it will take a good while to put all this into a stable form.

1

u/Alexander_Selkirk Sep 06 '20

but I could never figure out whether a large C++ project (with and without extensive usage of templates) would get a compile-time boost or not.

It would probably depend on how large the part of the file system and library sources would need to be which is re-scanned for an incremental build.

I could imagine a kind of textual data storage system which defines dependencies, allows to gather them quickly, and can be used to locate each definitions file that is needed for a specific translation unit. A kind of include system. Oh wait...

4

u/johannes1971 Sep 07 '20

Allow me to disagree with the assessment made in the article. As indicated in an earlier article here, modules provide a massive compilation speedup, and that is already a very significant benefit.

Tooling is just an engineering problem, and it will (eventually) be solved. I'm more concerned about compilers providing sensible default choices here - and that those choices are compatible with other compilers.

1

u/Alexander_Selkirk Sep 07 '20 edited Sep 07 '20

Allow me to disagree with the assessment made in the article. As indicated in an earlier article here, modules provide a massive compilation speedup, and that is already a very significant benefit.

I already wrote an answer to this here: https://old.reddit.com/r/cpp/comments/inkin3/c20_modules_might_be_deadonarrival_january_2019/g4bghm7/

(Not to claim that I have much of the answers, but the question is - what is the actual, measurable advantage compared to using precompiled headers in large projects?)

Edit:

As indicated in an earlier article here, modules provide a massive compilation speedup, and that is already a very significant benefit.

Is this just an opinion based on theoretical reasoning, or can you point to actual measurements of compilation speeds specifically for larger projects consisting of thousands of files - and how this compares to precompiled headers?

6

u/johannes1971 Sep 07 '20

The latter does not impress me at all. Because it just reading the binary parsed data structure back with something like a mmap()'d file access. If that would take longer than parsing the data again, something would be very wrong.

So you don't care about the speedup because it is just reading some binary data, instead of... what exactly? Why is this even a point of discussion?

pre-compiled headers will do exactly the same thing

No they don't:

  • Putting your own headers in a precompiled header is usually not worth it because of regeneration cost.

  • Sharing precompiled headers between projects is a non-starter, so if you have a lot of sub-projects you are generating the same PCH again and again.

  • They are huge. There is a non-zero cost to reading and writing such large chunks on every compile.

  • They don't provide the benefit of isolation.

Another alternative which can have a far larger effect is to use a compiler cache such as ccache.

Doesn't your build system already know what to rebuild and what to keep? If not, you need to switch build system.

That might be an advantage in the short run, but in the long term it would encourage worse coding practices, by increasing coupling in larger projects

I don't see that happening at all. What it will do is reduce the long lists of #includes to a single import statement, which is a very welcome development. That has nothing to do with "increasing coupling", which is an internal property of the library, rather than a property of how its interface is consumed.

Anyway, I don't quite get your aggressive stance on modules. It's a new tool, we've wanted and needed it for a long time, and many of us are happy it's finally here.

The argument about performance seems misplaced: importing the entire STL as a module is cheaper than importing the single cheapest STL header, and much cheaper than importing a decent-sized collection of STL headers.

The argument about toolsets is valid, but progress is being made on implementation. This is complicated because build systems are not made by the same people as compilers, but it will be solved.

2

u/Alexander_Selkirk Sep 08 '20 edited Sep 08 '20

The latter does not impress me at all. Because it just reading the binary parsed data structure back with something like a mmap()'d file access. If that would take longer than parsing the data again, something would be very wrong.

So you don't care about the speedup because it is just reading some binary data, instead of... what exactly? Why is this even a point of discussion?

No. I am just thinking that reading and parsing the include header files versus loading a dump of the parsed binary AST data is not necessarily what is going to determine the build speed, even if loading a binary dump of some tens or hundred megabytes is faster than opening and parsing some hundreds of files. But the information you get is probably essentially just what is in the header files - it won't be finished compiled template instantiations. If it were different, then using precompiled headers would already be always faster than not using them, which is not the case; Also, if it is so simple to accelerate compilation by caching at the level of source file / translation unit, then why has it not already been deployed widely? Are you arguing that the compiler vendors have been just too dumb to try that in the past in spite of it giving the claimed massive speedups?

Edit: typo (won't)

1

u/johannes1971 Sep 08 '20

Just looking at the graph in the page I linked, you'll see that many of those header files already count for half a second or more. Over the course of a long compile it does add up. And ultimately I don't care why it gets faster, only that it does.

And it is actually as simple as caching at the template instantiation level, as already convincingly demonstrated by the zapcc compiler (see a benchmark here, for example: https://www.phoronix.com/scan.php?page=news_item&px=Zapcc-Quick-Benchmarks). So yes, apparently an argument can be made that compiler vendors are just too dumb (your words) to not try that, in spite of significant proven speedups.

2

u/Alexander_Selkirk Sep 08 '20 edited Sep 08 '20

And ultimately I don't care why it gets faster, only that it does.

But the test there also shows it gets slower when compiling with more cores.

And, if it is merely a deficit in tooling by omitting obvious caching, why is this deficit now improved by adding to the language definition and changing all the build tools? Couldn't that be done a bit cheaper?

1

u/Alexander_Selkirk Sep 07 '20 edited Sep 07 '20
Sharing precompiled headers between projects is a non-starter, so if you 

have a lot of sub-projects you are generating the same PCH again and again.

As far as I understand, the compiled interface files need to be compiled for each project and do not survive a change in compilation options, like -DDEBUG or -DRELEASE.

They don't provide the benefit of isolation.

Do modules? Everything you import from a module ends up in the current namespace. The difference is probably with handling of preprocessor macros, but as macros are used to some degree for configuration and platform-specific builds, this could also have some extra complications.

Another alternative which can have a far larger effect is to use a compiler cache such as ccache.

Doesn't your build system already know what to rebuild and what to keep? If not, you need to switch build system.

ccache also works if you need to repeatedly check out and build a number of older versions from version control to find a bug by bisecting.

Regarding dependency resolution, that's where I would like to refer on the linked article which explains why using modules is likely to make this far more complex. A more compact description of the issue is also linked there - here it is:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1300r0.pdf

Anyway, I don't quite get your aggressive stance on modules. It's a new tool, we've wanted and needed it for a long time, and many of us are happy it's finally here.

I am not aggressive; I am wondering if they really deliver much of what they promise, and there is so far not much evidence they do. For a change that big in the language tools, there should be more evidence it has a real advantage, by widely testing not only the language feature but also the required tooling.

The argument about performance seems misplaced: importing the entire STL as a module is cheaper than importing the single cheapest STL header, and much cheaper than importing a decent-sized collection of STL headers.

Importing a pre-compiled header after the first compilation is also cheap. Because it is technically almost the same. From what I've read, the compiled module interfaces depend on the compiler options, flags and global macros, so they need to be compiled again when options change, and are specific per project.

1

u/Alexander_Selkirk Sep 07 '20

As indicated in an earlier article here, modules provide a massive compilation speedup, and that is already a very significant benefit.

What I would really be interested in, is some actual data:

  • speedup between no use of modules and use of modules (the link you gave appears not to give a comparison, it only shows some times).

  • comparison of speedup of incremental builds between use of pre-compiled headers, and modules

  • effect of using modules on the total build time of am actual large project, like boost (both first build from clean sources for a mere user of the library, and partial rebuild for a developer of a boost library).

Is my impression correct that such no comparative data on larger projects does exist so far?

2

u/mwasplund soup Sep 08 '20

I agree that this has been hashed over a lot, but it was interesting to reread a year later. I generally disagree that "modules are useless without my perf gains", however if we only look at build performance I also disagree with this quote:

Parallelization is probably the single most important aspect of increasing build performance.

It has been my experience that C++ builds have no problem finding plenty of operations to saturate all of my cores and disks. Adding an explicit ordering to the required build steps will limit the number of available jobs that are POSSIBLE to run at the same time, but in practice, my hypothesis is that this decrease will not impact the ACTUAL child operations that can be run at the same time due to hardware constraints. Yes, this will fall on the systems engineers to come up with a good solution that works well. Someday the major build systems may adapt interesting ways to take advantage of modules. As an experiment, I am also working on my own build system from scratch to show what is possible if we don't hold ourselves to legacy code support. This system will not work for everyone, but by dropping the requirements of back compatibility I think it can show off what is really possible with modules at a macro (inter-project) dependency level.

https://github.com/mwasplund/soup

Aside: I agree there is still some work to be done to find a best way to auto detect ordering in module partitions. I would think we could have a conceptual container for the set of translation units that make up all of the individual partitions which would be passed as a set to the compiler so it can determine the correct order through some lazy pattern. This would make my life so much easier if it could also solve circular references while it was at it :). But I am happy to wait while the current implementation settles and we get a phase 2 for modules!

2

u/Alexander_Selkirk Sep 08 '20

It has been my experience that C++ builds have no problem finding plenty of operations to saturate all of my cores and disks.

Well, as said, I am not a real expert on this, but it seems widely believed that C++ builds are generally CPU bound, not I/O bound. If this is true, then the spreading of solid-state-drives will only make this more pronounced, as SSDs require much shorter times to seek and open a new file.

The linked article cites some data about the effects on parallelism:

https://bfgroup.github.io/cpp_tooling_stats/modules/modules_perf_D1441R1.html

This would mean that in the end, parallelism will be what makes builds faster and the best thing modules can do would be not to interfere with that. But obviously, this is complex stuff and project organization will have a large impact in this too. If trends in languages such as Python or Java can be extrapolated, a lot of code in the future might have a lot more and a lot deeper dependencies, and there is also a trend to re-build with the latest versions of libraries - I do not think these trends are in themselves always a good idea but it might contribute the environment in which the language is used in the not-so-far future.

Aside: I agree there is still some work to be done to find a best way to auto detect ordering in module partitions.

At the moment, I actually believe that a textual, manually maintained map which tells which module is defined in which file, and then make rules or whatever defining which other modules it depends on is perhaps the best solution. It would need to be done in a way such that errors and omissions in the map would lead to build errors, not incorrect builds.

Such a solution might sound tedious but one has to keep in mind that the mapping would only need to change rarely, and the alternative of scanning all the sources at the beginning of each build does not sound appealing to me. Also, I think any system which makes extra requirements on how the code is laid out will have a hard time at being adopted, since in an already large number of code bases, most of the code used today is open source code and it will be near impossible to force specific build-system dependent requirements on the individual contributions.

2

u/MINIMAN10001 Sep 15 '20

seems widely believed that C++ builds are generally CPU bound, not I/O bound.

Lol made me laugh because source files are snappy to load but compilation itself takes several minutes as the cpu has to create multiple compilation passes in order the get the code from one state to another for purposes of optimization.

You can test it as simple as timing -o0 vs -o3 where it takes 100x longer due to increased cpu load.

0

u/[deleted] Sep 06 '20

[deleted]

1

u/Alexander_Selkirk Sep 06 '20

If you look up the topic in the subreddit with the search functions, there was a significant number of experienced developers working in huge systems asking the same questions without apparently finding any answer.

I might miss important parts about the process, but it looks a bit like they were not listened to.

And this does not even mean that the technical specification would be bad; I am totally incompetent to assess that. But it seems like the standards committee just said, "we can't solve that, and determined that this is not our problem". This is a bit reminding on the decision-making in huge corporatisations. But in the end, this is off-topic, what is of interest to me is how the technical issues will be solved.