[clang] Shrink the inline storage of clang::Module's containers
Most of `Module`'s containers reserve inline storage for one or two elements,
or are `std::vector`s (24 bytes instead of 16 for a `SmallVector<T, 0>`), even
though they're empty in almost every module, or almost always hold exactly
one element.
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.
These get no inline storage:
- `AffectingClangModules`, `DirectUses`, `UndeclaredUses` and `Conflicts`
are empty in every module of these scans.
- `ConfigMacros` is used by 0.0% / 0.1% of the modules, `Requirements` by
1.5% / 0.9% (93% of those hold one element, 7% two), `LinkLibraries` by
1.8% / 1.9% (always one) and `DirectoryDependencies` by 1.3% / 1.8% (88% /
91% one).
- `SubModules` is used by 11.4% / 8.5% of the modules; 28% / 24% of those hold
one element, 27% / 23% two, and the rest three or more, up to 301.
- `Imports` is used by 70.1% / 85.7% of the modules, but only 33% / 28% of
those hold one element and 19% / 22% two; the rest hold three or more, up
to 287.
`std::vector` becomes `SmallVector<T, 0>` (`DirectoryDependencies`,
`SubModules`, `ConfigMacros`, `Conflicts`), and `UndeclaredUses` a
`SetVector<T>` instead of a `SmallSetVector<T, 2>`.
These keep inline storage for exactly one element:
- `Exports`: used by 80.4% / 93.6% of the modules, 99.9% of which have exactly
one export (typically `export *`).
- `TopHeaderNames`: used by 77.3% / 93.4% (modules deserialized from a PCM),
98.2% / 98.5% of which have exactly one. Was a `std::vector`.
- `HeadersStorage`: used by 41.5% / 22.9%, 95.0% / 93.7% of which have exactly
one.
- `TopHeaders`: used by 3.1% / 0.0%, 98.9% of which have exactly one.
Most of these containers live in `Module::ColdMembers` or
`Module::TopLevelMembers` by now, which shrink from 584 to 360 and from 200 to
184 bytes. `sizeof(Module)` goes from 568 to 440 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 | 359.5 MB | 352.6 MB | -6.8 +- 4.1 MB (-1.9%) | +0.2 +- 0.8% (n.s.) |
| populated | 243.5 MB | 235.3 MB | -8.2 +- 2.5 MB (-3.4%) | -0.6 +- 1.1% (n.s.) |