Skip to content

release/20.x: Reduce memory usage in AST parent map generation by lazily checking if nodes have been seen (#129934) #131209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions clang/lib/AST/ParentMapContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
//===----------------------------------------------------------------------===//

#include "clang/AST/ParentMapContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/TemplateBase.h"
#include "llvm/ADT/SmallPtrSet.h"

using namespace clang;

Expand Down Expand Up @@ -69,17 +70,21 @@ class ParentMapContext::ParentMap {
for (; N > 0; --N)
push_back(Value);
}
bool contains(const DynTypedNode &Value) {
return Seen.contains(Value);
bool contains(const DynTypedNode &Value) const {
const void *Identity = Value.getMemoizationData();
assert(Identity);
return Dedup.contains(Identity);
}
void push_back(const DynTypedNode &Value) {
if (!Value.getMemoizationData() || Seen.insert(Value).second)
const void *Identity = Value.getMemoizationData();
if (!Identity || Dedup.insert(Identity).second) {
Items.push_back(Value);
}
}
llvm::ArrayRef<DynTypedNode> view() const { return Items; }
private:
llvm::SmallVector<DynTypedNode, 2> Items;
llvm::SmallDenseSet<DynTypedNode, 2> Seen;
llvm::SmallVector<DynTypedNode, 1> Items;
llvm::SmallPtrSet<const void *, 2> Dedup;
};

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