Skip to content

Commit 8c7f0ea

Browse files
Reduce memory usage in AST parent map generation by lazily checking if nodes have been seen (#129934)
This mitigates a regression introduced in #87824. The mitigation here is to store pointers the deduplicated AST nodes, rather than copies of the nodes themselves. This allows a pointer-optimized set to be used and saves a lot of memory because `clang::DynTypedNode` is ~5 times larger than a pointer. Fixes #129808.
1 parent 8aa835c commit 8c7f0ea

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

clang/lib/AST/ParentMapContext.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "clang/AST/ParentMapContext.h"
15-
#include "clang/AST/RecursiveASTVisitor.h"
1615
#include "clang/AST/Decl.h"
1716
#include "clang/AST/Expr.h"
17+
#include "clang/AST/RecursiveASTVisitor.h"
1818
#include "clang/AST/TemplateBase.h"
19+
#include "llvm/ADT/SmallPtrSet.h"
1920

2021
using namespace clang;
2122

@@ -69,17 +70,21 @@ class ParentMapContext::ParentMap {
6970
for (; N > 0; --N)
7071
push_back(Value);
7172
}
72-
bool contains(const DynTypedNode &Value) {
73-
return Seen.contains(Value);
73+
bool contains(const DynTypedNode &Value) const {
74+
const void *Identity = Value.getMemoizationData();
75+
assert(Identity);
76+
return Dedup.contains(Identity);
7477
}
7578
void push_back(const DynTypedNode &Value) {
76-
if (!Value.getMemoizationData() || Seen.insert(Value).second)
79+
const void *Identity = Value.getMemoizationData();
80+
if (!Identity || Dedup.insert(Identity).second) {
7781
Items.push_back(Value);
82+
}
7883
}
7984
llvm::ArrayRef<DynTypedNode> view() const { return Items; }
8085
private:
81-
llvm::SmallVector<DynTypedNode, 2> Items;
82-
llvm::SmallDenseSet<DynTypedNode, 2> Seen;
86+
llvm::SmallVector<DynTypedNode, 1> Items;
87+
llvm::SmallPtrSet<const void *, 2> Dedup;
8388
};
8489

8590
/// Maps from a node to its parents. This is used for nodes that have

0 commit comments

Comments
 (0)