From 8e4a17634f1baa9df652412229a06be5ca4c9ba2 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 22 Oct 2015 11:21:40 +0000 Subject: [AST] Store Decl* and Stmt* directly into the ParentMap. These are by far the most common types to be parents in the AST so it makes sense to optimize for them. Put them directly into the value of the map. This currently saves 32 bytes per parent in the map and a pointer indirection at the cost of some additional complexity in the code. Sadly this means we cannot return an ArrayRef from getParents anymore, add a proxy class that can own a single DynTypedNode and otherwise behaves exactly the same as ArrayRef. For example on a random large file (X86ISelLowering.cpp) this reduces the size of the parent map by 24 MB. Differential Revision: http://reviews.llvm.org/D13976 llvm-svn: 251008 --- clang/lib/AST/ASTContext.cpp | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'clang/lib/AST/ASTContext.cpp') diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index dd47d43ccb1..379289c2bef 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -797,8 +797,7 @@ void ASTContext::ReleaseParentMapEntries() { for (const auto &Entry : *AllParents) { if (Entry.second.is()) { delete Entry.second.get(); - } else { - assert(Entry.second.is()); + } else if (Entry.second.is()) { delete Entry.second.get(); } } @@ -8673,6 +8672,15 @@ bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { namespace { +ast_type_traits::DynTypedNode +getSingleDynTypedNodeFromParentMap(ASTContext::ParentMap::mapped_type U) { + if (const auto *D = U.template dyn_cast()) + return ast_type_traits::DynTypedNode::create(*D); + if (const auto *S = U.template dyn_cast()) + return ast_type_traits::DynTypedNode::create(*S); + return *U.template get(); +} + /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their /// parents as defined by the \c RecursiveASTVisitor. /// @@ -8728,16 +8736,23 @@ namespace { // do not have pointer identity. auto &NodeOrVector = (*Parents)[Node]; if (NodeOrVector.isNull()) { - NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back()); + if (const auto *D = ParentStack.back().get()) + NodeOrVector = D; + else if (const auto *S = ParentStack.back().get()) + NodeOrVector = S; + else + NodeOrVector = + new ast_type_traits::DynTypedNode(ParentStack.back()); } else { - if (NodeOrVector.template is()) { - auto *Node = - NodeOrVector.template get(); - auto *Vector = new ASTContext::ParentVector(1, *Node); + if (!NodeOrVector.template is()) { + auto *Vector = new ASTContext::ParentVector( + 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); NodeOrVector = Vector; - delete Node; + if (auto *Node = + NodeOrVector + .template dyn_cast()) + delete Node; } - assert(NodeOrVector.template is()); auto *Vector = NodeOrVector.template get(); @@ -8774,7 +8789,7 @@ namespace { } // end namespace -ArrayRef +ASTContext::DynTypedNodeList ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { assert(Node.getMemoizationData() && "Invariant broken: only nodes that support memoization may be " @@ -8787,12 +8802,12 @@ ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { } ParentMap::const_iterator I = AllParents->find(Node.getMemoizationData()); if (I == AllParents->end()) { - return None; + return llvm::ArrayRef(); } - if (auto *N = I->second.dyn_cast()) { - return llvm::makeArrayRef(N, 1); + if (auto *V = I->second.dyn_cast()) { + return llvm::makeArrayRef(*V); } - return *I->second.get(); + return getSingleDynTypedNodeFromParentMap(I->second); } bool -- cgit v1.2.3