[cfe-commits] r39312 - in /cfe/cfe/trunk: AST/ASTContext.cpp AST/Type.cpp include/clang/AST/ASTContext.h include/clang/AST/Type.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:42:54 PDT 2007


Author: sabre
Date: Wed Jul 11 11:42:54 2007
New Revision: 39312

URL: http://llvm.org/viewvc/llvm-project?rev=39312&view=rev
Log:
Switch pointers over to using a FoldingSet to unique them instead of
"obviously braindead" linear searches.  reduces the number of slow
type lookups from 10K to 883 on carbon.h, speeding up parsing from 3.5 to
1.26s.

Modified:
    cfe/cfe/trunk/AST/ASTContext.cpp
    cfe/cfe/trunk/AST/Type.cpp
    cfe/cfe/trunk/include/clang/AST/ASTContext.h
    cfe/cfe/trunk/include/clang/AST/Type.h

Modified: cfe/cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/ASTContext.cpp?rev=39312&r1=39311&r2=39312&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/cfe/trunk/AST/ASTContext.cpp Wed Jul 11 11:42:54 2007
@@ -132,23 +132,30 @@
 /// getPointerType - Return the uniqued reference to the type for a pointer to
 /// the specified type.
 TypeRef ASTContext::getPointerType(TypeRef T) {
-  // FIXME: This is obviously braindead!
   // Unique pointers, to guarantee there is only one pointer of a particular
   // structure.
-  ++NumSlowLookups;
-  for (unsigned i = 0, e = Types.size(); i != e; ++i)
-    if (PointerType *PTy = dyn_cast<PointerType>(Types[i]))
-      if (PTy->getPointeeType() == T)
-        return Types[i];
+  FoldingSetNodeID ID;
+  PointerType::Profile(ID, T);
+  
+  void *InsertPos = 0;
+  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
+    return PT;
   
   // If the pointee type isn't canonical, this won't be a canonical type either,
   // so fill in the canonical type field.
   Type *Canonical = 0;
-  if (!T->isCanonical())
+  if (!T->isCanonical()) {
     Canonical = getPointerType(T.getCanonicalType()).getTypePtr();
+   
+    // Get the new insert position for the node we care about.
+    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
+    assert(NewIP == 0 && "Shouldn't be in the map!");
+  }
   
-  Types.push_back(new PointerType(T, Canonical));
-  return Types.back();
+  PointerType *New = new PointerType(T, Canonical);
+  Types.push_back(New);
+  PointerTypes.InsertNode(New, InsertPos);
+  return New;
 }
 
 /// getArrayType - Return the unique reference to the type for an array of the

Modified: cfe/cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Type.cpp?rev=39312&r1=39311&r2=39312&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Type.cpp (original)
+++ cfe/cfe/trunk/AST/Type.cpp Wed Jul 11 11:42:54 2007
@@ -75,6 +75,10 @@
   }
 }
 
+void PointerType::Profile(FoldingSetNodeID &ID, TypeRef Pointee) {
+  ID.AddPointer(Pointee.getAsOpaquePtr());
+}
+
 void FunctionTypeProto::Profile(FoldingSetNodeID &ID, TypeRef Result,
                                 TypeRef* ArgTys,
                                 unsigned NumArgs, bool isVariadic) {

Modified: cfe/cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/ASTContext.h?rev=39312&r1=39311&r2=39312&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/cfe/trunk/include/clang/AST/ASTContext.h Wed Jul 11 11:42:54 2007
@@ -28,6 +28,7 @@
   std::vector<Type*> Types;
   unsigned NumSlowLookups;
   FoldingSet<FunctionTypeProto> FunctionTypeProtos;
+  FoldingSet<PointerType> PointerTypes;
 public:
   Preprocessor &PP;
   TargetInfo &Target;

Modified: cfe/cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/Type.h?rev=39312&r1=39311&r2=39312&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Type.h Wed Jul 11 11:42:54 2007
@@ -222,7 +222,7 @@
 
 /// PointerType - C99 6.7.5.1 - Pointer Declarators.
 ///
-class PointerType : public Type {
+class PointerType : public Type, public FoldingSetNode {
   TypeRef PointeeType;
   PointerType(TypeRef Pointee, Type *CanonicalPtr) :
     Type(Pointer, CanonicalPtr), PointeeType(Pointee) {
@@ -234,6 +234,12 @@
   
   virtual void getAsString(std::string &InnerString) const;
   
+  
+  void Profile(FoldingSetNodeID &ID) {
+    Profile(ID, getPointeeType());
+  }
+  static void Profile(FoldingSetNodeID &ID, TypeRef Pointee);
+  
   static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
   static bool classof(const PointerType *) { return true; }
 };





More information about the cfe-commits mailing list