[clang] [clang-tools-extra] [clang][modules] Shrink the size of `Module::Headers` (PR #113395)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 25 08:58:16 PDT 2024


================
@@ -263,17 +261,36 @@ class alignas(8) Module {
     FileEntryRef Entry;
   };
 
-  /// Information about a directory name as found in the module map
-  /// file.
+private:
+  static const int NumHeaderKinds = HK_Excluded + 1;
+  // The begin index for a HeaderKind also acts the end index of HeaderKind - 1.
+  // The extra element at the end acts as the end index of the last HeaderKind.
+  unsigned HeaderKindBeginIndex[NumHeaderKinds + 1] = {};
+  SmallVector<Header, 2> HeadersStorage;
+
+public:
+  ArrayRef<Header> getAllHeaders() const { return HeadersStorage; }
+  ArrayRef<Header> getHeaders(HeaderKind HK) const {
+    assert(HK < NumHeaderKinds && "Invalid Module::HeaderKind");
+    auto BeginIt = HeadersStorage.begin() + HeaderKindBeginIndex[HK];
+    auto EndIt = HeadersStorage.begin() + HeaderKindBeginIndex[HK + 1];
----------------
jansvoboda11 wrote:

I think it would have to be
```cpp
HK == NumHeaderKinds - 1 ? HeadersStorage.end() : HeadersStorage.begin() + HeaderKindBeginIndex[HK + 1]
```
but in principle you're right. In the end I felt that making the function branchless is worth the extra 4B of storage.

https://github.com/llvm/llvm-project/pull/113395


More information about the cfe-commits mailing list