<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/97239>97239</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Counterintuitive effects of module import location on compilation performance
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kamrann
</td>
</tr>
</table>
<pre>
I recently experienced an extreme improvement and subsequent regression in compilation performance whilst migrating a library to modules. It turned out the key change had little to do with my gradually encapsulating more into modules, but was down to something that I did by accident. Here's a reduced and much simplified repro.
```
// lots_of_std.h
#include <chrono>
#include <format>
#include <mutex>
#include <thread>
#include <memory>
#include <ranges>
#include <algorithm>
#include <iostream>
#include <functional>
#include <filesystem>
```
```
// pmi_x.ixx
module;
#include "lots_of_std.h"
export module x;
```
```
// maybe_import_x.h
#ifdef import_x_in_gmf
import x;
#endif
```
```
// ip_y_a.ixx
module;
#include "maybe_import_x.h"
#include "lots_of_std.h"
export module y:a;
import x;
```
```
// ip_y_b.ixx
module;
#include "maybe_import_x.h"
#include "lots_of_std.h"
export module y:b;
import x;
import :a;
```
```
<ip_y_c.ixx, ip_y_d.ixx, ip_y_e.ixx, ip_y_f.ixx as above, each importing the preceding partition>
```
```
// pmi_y.ixx
export module y;
export import :a;
export import :b;
export import :c;
export import :d;
export import :e;
export import :f;
```
So a simple module including some heavy headers in its GMF, then a second module with a linear chain of interface partitions each including the same headers in their GMF, importing the first module, and importing the preceding partition. Each partition also conditionally (via preprocessor switch) imports the first module at the top of its GMF, on top of doing so in its purview.
Compilation times from `-ftime-trace` below. Note that in the default (fat BMI) mode, each TU generates two JSON files from the time trace; one appears largely insignificant time-wise, so below numbers are from the significant one only.
![clang-modules-perf-chart](https://github.com/llvm/llvm-project/assets/2475657/f6a80fff-6860-49c4-bfc6-968d2eec995a)
In addition to the above, compilation time for `pmi_x.ixx` was constant at around 3.8s for all cases.
BMI sizes were as follows (MB):
| | x [default] | x [import-in-gmf] | y partitions [default] | y partitions [import-in-gmf] |
|--------|--------|--------|--------|--------|
| fat | 30 | 30 | 30-32 | 14 |
| thin | 25 | 25 | 10 | 10 |
Observations:
1. Most obviously, including the additional `import x;` statement at the top of the partition GMFs produces a huge improvement in compilation performance. If the import were moved there from the purview then perhaps I might expect some improvement since if I understand right, that would be a semantic change as the declarations would not be passed on to downstream importing partitions. But since it's just being added there as an addition to the import in the purview, this is very counterintuitive to me. When I noticed I'd done this accidentally I wasn't even sure it was legal to import in the GMF. My understanding was that as all code becomes more and more modularized, the goal would be for the GMF to contain fewer and fewer things, so it was super surprising to find that my compilation times got slashed (by 3x in my real use case) by just dumping an additional, seemingly superfluous import statement into the GMF.
2. With the fat BMI, TU compilation time increases with depth of the TU in the DAG of module partitions. This is also kind of surprising, since there is nothing new being added at each level, and each partition only imports its directly preceding partition, so it's not clear why compiling `f` should take any longer than `b`. Note that from looking at the time traces, the increased time is entirely confined within `WriteAST`; the preceding frontend times barely change. Also, though in the fat BMI case there is a small variance in partition BMI sizes in the default (no import in GMF) case, it appeared somewhat arbitrary - the size was not increasing from `a` to `f`.
One other question is why the BMI contains much of anything at all in the above repro since nothing is exported or even declared in the module purviews. Though I'm guessing this must be known and is perhaps just a pending implementation issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWEFz4zbP_jXKBWONI9lOfPBhnTT7-ZtJe-h29pihSEhiQ5EqSdnW_vp3QEq2HMdvuz29O7srSxAJ4gHwABBzTlYacZMst8ny-Y51vjZ2884ay7S-K4zoNzuwyFF71QMeW7QSNUcBTAMevcUGQTatNXtsUHtgWoDrCod_dXRrsbLonDQapAZumlYq5um2RVsa2zDNEQ61VM5DIyvLvNQVMFCysMz24A00RnQKXQo7D76zGgWYzoOvEd6xB14zXSHUTICS3iukNcLAQfoamh4qy0THFB1fc9a6TkUdjbEIUp8VJNkTFJ2HA3MgzEHTPs406Gt63dfMww6EFFD0wDiXArVP4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