Closed
Description
At mpusz/mp-units#350 (comment) is a table that reveals that compiling the compile-time tests of a template-heavy library's module mp_units
is 3x slower than using #include
s.
Factoring out a lightweight module mp_units.core
from mp_units
, and changing the test unit_test
to import that instead (and no other changes), reveals that simply importing mp_units
results in terribly slow compilation. Below is a table comparing the compile times. This particular test was 5x slower than using #include
, and 11x slower than using the lightweight module.
Compiled item | #include |
mp_units.core |
mp_units |
---|---|---|---|
unit_test |
2 s | 1 s | 11 s |
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
llvmbot commentedon Mar 6, 2023
@llvm/issue-subscribers-clang-modules
koplas commentedon Mar 6, 2023
Can you compile with
-ftime-trace
and open the resulting json file withchrome://tracing
in chrome?I'm also experiencing increased build times with modules, so a flamegraph could give a hint if both issues are related.
JohelEGP commentedon Mar 6, 2023
This is how it looks.

koplas commentedon Mar 6, 2023
Could be related to #61040.
[-][Modules] Importing template-heavy library 3x slower than `#include`[/-][+][C++20] [Modules] Importing template-heavy library 3x slower than `#include`[/+]ChuanqiXu9 commentedon Mar 14, 2023
@JohelEGP would you like to test again on the newest trunk?
JohelEGP commentedon Mar 14, 2023
Sure. I didn't know anything had changed.
ChuanqiXu9 commentedon Mar 14, 2023
I fixed this one (#61064). But I am not sure if it can address your problem fully. If not, could you try to provide a reduced example? So that I can work on it quickly.
JohelEGP commentedon Mar 14, 2023
I see that you just closed the referenced #61040. I had been watching that one, since it was more relevant to me. I'll build and test tomorrow.
JohelEGP commentedon Mar 14, 2023
Times are unchanged. I'll try to reduce it today.
ChuanqiXu9 commentedon Mar 17, 2023
@JohelEGP how is it going? The issue report is a big concern for me.
ChuanqiXu9 commentedon Mar 17, 2023
BTW, maybe you can try to remove all the constexpr/consteval things.
JohelEGP commentedon Mar 17, 2023
I tried for a day, and failed so far.
I merged the modules into the preprocessed
mp_units.systems
(equivalent tomp_units
in the table) andunit_test
(but takes me 3.4 s to compile rather than 11 s, because I defined some feature-stripping macros when compilingmp_units.systems
), and appended their compile command line as a comment.Remember that compiling
unit_test
with#include
(without enabling modules, so there's no chance for include translation of standard C++ headers) takes 2 s (not feature-stripped). Compiling the preprocessedunit_test
that importsmp_units.systems
(feature-stripped) takes 3.4 s. I noticed that each type inunit_test
adds a significant amount of overhead. Commenting out lines 144-179, so that there's only typemetre
, takes me 1.4 s to compile.JohelEGP commentedon Mar 17, 2023
Definitely not all. I'd have to try that judiciously. After all,
struct metre : named_unit<metre, "m"> {};
, which passes a string literal as a template argument, needs to continue working. Compile-time is too ingrained into the library.ChuanqiXu9 commentedon Mar 20, 2023
I'm not suggesting you to do that. I just want to locate the problem.
ChuanqiXu9 commentedon May 10, 2023
I image there will be some changes after b6c7177. @JohelEGP would you like to test it again?
JohelEGP commentedon May 10, 2023
Thank you! Seems fixed.
Updating the opening table:
#include
mp_units.core
mp_units
unit_test
beforeunit_test
after b6c7177Updating the table from the issue linked in the OP:
mp_units
beforemp_units
after b6c7177#include
)mp_units
) beforemp_units
) after b6c7177As for the last row, now there are compilation errors. I'll have to investigate.
Do note that
mp_units
was made into one big module of all the would-be heavy submodules.Even when all that
mp_units
did was reexport those submodules, it took about the same of time to compile as the modules it exported.Now, there's still one such submodule,
mp_units.systems
, which still takes the 33 s to compile.mp_units
now takes 2 s, although all it does is reexport some modules and a few templates.That's, perhaps, justified, as in the GMF it has:
#include <gsl/gsl-lite.hpp>
.Module mp_units
module mp_units
:<units/chrono.h>
: