Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will modules in c++20 reduce compile time compared to traditional header-files? [closed]

Suppose we have the module interface source file foo.ixx in which the module foo is defined. We use

import foo;

in many different cpp-files. Will there be compile time reduction compared to the case where a traditional header-file foo.h is included in many different cpp-files? If the compile time is reduced, why is this the case?

like image 927
BlueTune Avatar asked Jul 26 '20 15:07

BlueTune


People also ask

Why are modules better than headers?

Modules eliminate or reduce many of the problems associated with the use of header files. They often reduce compilation times. Macros, preprocessor directives, and non-exported names declared in a module aren't visible outside the module.

Are header files compiled in C?

c' files call the pre-assembly of include files "compiling header files". However, it is an optimization technique that is not necessary for actual C development.

Can you define which header file to include at compile time?

Yes. This can be done by using the #if, #else, and #endif preprocessor directives.


2 Answers

Yes, one of the advantages of modules is that it can reduce compilation times. For comparison, here's how it's done today:

// foo.hpp
// some code
// a.cpp
#include "foo.hpp"
// b.cpp
#include "foo.hpp"

Now when the 2 translation units a.cpp and b.cpp are compiled, some code is textually included into these source files, and hence some code is compiled twice. While the linker will take care that only one definition is actually in the final executable, the compiler still has to compile some code twice, which is wasted effort.

With modules, we would have something like:

// foo.hpp
export module foo;
// some code
// a.cpp 
import foo;
// b.cpp 
import foo;

Now the compilation process is different; there is an intermediate stage where foo.hpp is compiled into a format that is consumable by a.cpp, and b.cpp, which means that the implementation files do not need to compile some code, they can just use the definitions in some code directly.

This means that the foo.hpp only needs to be compiled once, which can lead to potentially large reductions in compile times, especially as the number of implementation files that consume the module interface unit increases.

like image 118
cigien Avatar answered Oct 13 '22 01:10

cigien


"The mechanism for accessing headers from implementation files is to use the include directive from the C preprocessor. In other words, your headers are implicitly copied many times.

There are many copies of all header files scattered across a project, and the compiler has to pass through and parse them over and over again. One of the most visible problems is code compilation times.

Modules effectively replace header files and the preprocessor include directive. The solution proposed by modules suggests that we get rid of textual inclusion with the C preprocessor and, therefore, all of its drawbacks." [Each module handled just once. See Table 2]

enter image description here

Reference

like image 21
Amit G. Avatar answered Oct 13 '22 00:10

Amit G.