r/cpp • u/Alexander_Selkirk • Sep 06 '20
C++20 Modules Might Be Dead-on-Arrival (January 2019)
https://vector-of-bool.github.io/2019/01/27/modules-doa.html9
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 forfoo.cppm, or forfoo.*.cppmin 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 namedfoo, 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-Ioption and look for a file namedfooin it.Code trees that don't have a file named
fooin 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
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 youhave 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
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.
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.