[clang] 319e731 - [clang] Shrink AutoTypes map key to reduce peak memory (#218278)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 26 11:50:03 PDT 2026
Author: Anonmiraj
Date: 2026-08-26T21:49:58+03:00
New Revision: 319e7316b63a41830b0c9cef7371ef83ea257c0d
URL: https://github.com/llvm/llvm-project/commit/319e7316b63a41830b0c9cef7371ef83ea257c0d
DIFF: https://github.com/llvm/llvm-project/commit/319e7316b63a41830b0c9cef7371ef83ea257c0d.diff
LOG: [clang] Shrink AutoTypes map key to reduce peak memory (#218278)
While looking for memory regressions, I found that #118288 caused
[CTRE](https://github.com/hanickadot/compile-time-regular-expressions)
to regress by ~9.5% in peak memory (+758 MB).
The fix is simple: use a reference to the FoldingSetNodeID in the map.
| | Max RSS |
| --- | --- |
| trunk | 8928 MB |
| this PR | **8347 MB (−6.5%)** |
[compile-time-tracker](
https://llvm-compile-time-tracker.com/compare.php?from=49de424f45389cb757c3cc8c50daf38d024e2314&to=0cf6a242f9b5a211f9dffd6b8f52aeeb3e91e508&stat=instructions)
Added:
Modified:
clang/include/clang/AST/ASTContext.h
clang/lib/AST/ASTContext.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 013bc68a00a06..dd67c5d0410f8 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -284,7 +284,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
// arguments. Since both dependent and dependency are on the same set,
// we can end up in an infinite recursion when looking for a node if we used
// a `FoldingSet`, since both could end up in the same bucket.
- mutable llvm::DenseMap<llvm::FoldingSetNodeID, AutoType *> AutoTypes;
+ // Keyed by an interned FoldingSetNodeIDRef rather than a FoldingSetNodeID to
+ // avoid its large inline SmallVector in every bucket.
+ mutable llvm::DenseMap<llvm::FoldingSetNodeIDRef, AutoType *> AutoTypes;
mutable llvm::FoldingSet<DeducedTemplateSpecializationType>
DeducedTemplateSpecializationTypes;
mutable llvm::FoldingSet<AtomicType> AtomicTypes;
@@ -4084,5 +4086,19 @@ template <> struct llvm::DenseMapInfo<llvm::FoldingSetNodeID> {
return LHS == RHS;
}
};
+template <> struct llvm::DenseMapInfo<llvm::FoldingSetNodeIDRef> {
+ static unsigned getHashValue(FoldingSetNodeIDRef Val) {
+ return Val.ComputeHash();
+ }
+ static bool isEqual(FoldingSetNodeIDRef LHS, FoldingSetNodeIDRef RHS) {
+ return LHS == RHS;
+ }
+ static unsigned getHashValue(const FoldingSetNodeID &Val) {
+ return Val.ComputeHash();
+ }
+ static bool isEqual(const FoldingSetNodeID &LHS, FoldingSetNodeIDRef RHS) {
+ return LHS == RHS;
+ }
+};
#endif // LLVM_CLANG_AST_ASTCONTEXT_H
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 335d58ff2c9c8..b502c4436de49 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -6926,7 +6926,7 @@ ASTContext::getAutoType(DeducedKind DK, QualType DeducedAsType,
llvm::FoldingSetNodeID ID;
AutoType::Profile(ID, *this, DK, DeducedAsType, Keyword,
TypeConstraintConcept, TypeConstraintArgs);
- if (auto const AT_iter = AutoTypes.find(ID); AT_iter != AutoTypes.end())
+ if (auto const AT_iter = AutoTypes.find_as(ID); AT_iter != AutoTypes.end())
return QualType(AT_iter->getSecond(), 0);
if (DK == DeducedKind::Deduced) {
@@ -6956,7 +6956,7 @@ ASTContext::getAutoType(DeducedKind DK, QualType DeducedAsType,
assert(InsertedID == ID && "ID does not match");
#endif
Types.push_back(AT);
- AutoTypes.try_emplace(ID, AT);
+ AutoTypes.try_emplace(ID.Intern(BumpAlloc), AT);
return QualType(AT, 0);
}
More information about the cfe-commits
mailing list