[clang] Move rarely used members out of clang::Module
Move nineteen members that are empty or unset in almost every module into a
`Module::ColdMembers` struct (584 bytes), managed by a small private helper,
`Module::LazyMembers<T>`, which holds a `std::unique_ptr<T>` and allocates it
on the first write. Reads go through `get()`, which returns a static empty
instance when nothing is allocated; `clear()` and `take()` don't allocate
either. Users go through accessors: `getX()` returns an `ArrayRef`,
`StringRef` or value (empty when the struct isn't allocated), `addX()`/`setX()`
write, `takeX()` moves a list out (for the resolve-and-re-add loops in
`ModuleMap`), and `clearX()` (which doesn't allocate) is used by the
`ASTReader` when a PCM replaces the module map's information. The member is
`mutable` because the visible modules cache is built lazily from a const
member function. The behaviour is unchanged.
The module statistics come from temporary instrumentation that records, for
every module, the largest size each container reaches and whether each member
is ever set, in the scans measured below; numbers are given as "empty /
populated module cache". The scanner creates 1.89M / 0.38M modules, but only
about 40-55k of them are alive at the same time, since a TU's modules are freed
once it's scanned, so the peak memory goes down by roughly the bytes saved per
module times that.
- Never used in these scans: `VisibleModulesCache`, `UndeclaredUses`,
`ExportAsModule`, `ShadowingModule`, `UnresolvedHeaders`, `MissingHeaders`,
`AffectingClangModules`, `DirectUses`, `UnresolvedDirectUses`,
`UnresolvedConflicts` and `Conflicts`.
- The umbrella header or directory (`Umbrella`, `UmbrellaAsWritten`,
`UmbrellaRelativeToRootModuleDirectory`): 2.0% / 2.6% of the modules.
`UmbrellaDeclLoc` and `InferredSubmoduleLoc`: 0.6% / 1.0%.
- `Requirements`: 1.5% / 0.9%. `LinkLibraries`: 1.8% / 1.9%. `TopHeaders`:
3.1% / 0.0%.
At least one of them is used, and the struct allocated, in 7.3% / 4.0% of the
modules (24.5% / 39.3% of the top-level modules, 5.7% / 1.9% of the
submodules), so a module saves about 540 bytes on average.
`UnresolvedExports` stays inline for now: it also holds the plain `export *`
that most module map modules declare.
The top-level-only members and the submodule list are left for separate
changes: adding the former to the same struct would make it a lot larger and
allocated in 8.7% / 5.1% of the modules, and adding the latter would allocate
it in 16.2% / 9.6%.
`sizeof(Module)` goes from 1344 to 760 bytes.
Measured with an assertions-disabled Release build of `clang-scan-deps
-format experimental-full -j 20` on 200 TUs (10 TUs importing various SDK
frameworks, each in 20 module context hashes), with an empty module cache and
with one populated by the previous scan. Each of 40 rounds runs the binaries
of the whole series in random order. Peak memory footprint from
`/usr/bin/time -l`; mean +- 95% confidence interval of the per-round
difference to the previous commit:
| Module cache | before | after | peak memory | scan time |
|---|--:|--:|--:|--:|
| empty | 395.3 MB | 369.0 MB | -26.3 +- 2.9 MB (-6.7%) | -0.4 +- 0.7% (n.s.) |
| populated | 272.8 MB | 252.5 MB | -20.3 +- 2.8 MB (-7.4%) | -1.1 +- 0.9% |