]> granicus.if.org Git - clang/commitdiff
Remove SelectorTable/SelectorInfo, simply store all selectors in the central Identifi...
authorSteve Naroff <snaroff@apple.com>
Wed, 19 Sep 2007 16:18:46 +0000 (16:18 +0000)
committerSteve Naroff <snaroff@apple.com>
Wed, 19 Sep 2007 16:18:46 +0000 (16:18 +0000)
Rationale:

We currently have a separate table to unique ObjC selectors. Since I don't need all the instance data in IdentifierInfo, I thought this would save space (and make more sense conceptually).

It turns out the cost of having duplicate entries for unary selectors (i.e. names without colons) outweighs the cost difference between the IdentifierInfo & SelectorInfo structures. Here is the data:

Two tables:

*** Selector/Identifier Stats:
# Selectors/Identifiers: 51635
Bytes allocated:         1999824

One table:

*** Identifier Table Stats:
# Identifiers:   49500
Bytes allocated: 1990316

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42139 91177308-0d34-0410-b5e6-96231b3b80d8

AST/ASTContext.cpp
AST/Expr.cpp
Lex/IdentifierTable.cpp
Sema/SemaDecl.cpp
Sema/SemaExpr.cpp
include/clang/AST/ASTContext.h
include/clang/AST/Decl.h
include/clang/AST/Expr.h
include/clang/Lex/IdentifierTable.h

index df0398cd1b681110b5fb2c04e4fbf08e97701b7d..6420f0f05112ef70ce9377666b0562db72c81ceb 100644 (file)
@@ -104,9 +104,6 @@ void ASTContext::PrintStats() const {
     NumFunctionP*sizeof(FunctionTypeProto)+
     NumFunctionNP*sizeof(FunctionTypeNoProto)+
     NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
-  
-  if (Selectors)
-    Selectors->PrintStats();
 }
 
 
@@ -809,11 +806,3 @@ QualType ASTContext::getCFConstantStringType() {
   
   return getTagDeclType(CFConstantStringTypeDecl);
 }
-
-SelectorInfo &ASTContext::getSelectorInfo(const char *NameStart, 
-                                          const char *NameEnd) {
-  if (!Selectors) // create the table lazily
-    Selectors = new SelectorTable();
-  return Selectors->get(NameStart, NameEnd);
-}
-
index 08773e86d0cccf8d9704469c2c0984d6fb8d7c00..e5a128247eecdd0960be5d3aff960beb59b6e180 100644 (file)
@@ -858,7 +858,7 @@ unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
 
 // constructor for unary messages.
 ObjCMessageExpr::ObjCMessageExpr(
-  IdentifierInfo *clsName, SelectorInfo &methName, QualType retType, 
+  IdentifierInfo *clsName, IdentifierInfo &methName, QualType retType, 
   SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(methName) {
   ClassName = clsName;
@@ -867,7 +867,7 @@ ObjCMessageExpr::ObjCMessageExpr(
 }
 
 ObjCMessageExpr::ObjCMessageExpr(
-  Expr *fn, SelectorInfo &methName, QualType retType, 
+  Expr *fn, IdentifierInfo &methName, QualType retType, 
   SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(methName), ClassName(0) {
   SubExprs = new Expr*[1];
@@ -878,7 +878,7 @@ ObjCMessageExpr::ObjCMessageExpr(
 
 // constructor for keyword messages.
 ObjCMessageExpr::ObjCMessageExpr(
-  Expr *fn, SelectorInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs, 
+  Expr *fn, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs, 
   QualType retType, SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(0) {
   SubExprs = new Expr*[numargs+1];
@@ -890,7 +890,7 @@ ObjCMessageExpr::ObjCMessageExpr(
 }
 
 ObjCMessageExpr::ObjCMessageExpr(
-  IdentifierInfo *clsName, SelectorInfo &selInfo, ObjcKeywordMessage *keys, 
+  IdentifierInfo *clsName, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, 
   unsigned numargs, QualType retType, SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(clsName) {
   SubExprs = new Expr*[numargs+1];
index c1c00d232b6977f39d8682148862e76076379a56..d3faeb58dba50fe40a0d6689e25e38b061508d17 100644 (file)
@@ -209,34 +209,3 @@ void IdentifierTable::PrintStats() const {
   // Compute statistics about the memory allocated for identifiers.
   HashTable.getAllocator().PrintStats();
 }
-
-/// PrintStats - Print statistics about how well the identifier table is doing
-/// at hashing identifiers.
-void SelectorTable::PrintStats() const {
-  unsigned NumBuckets = HashTable.getNumBuckets();
-  unsigned NumIdentifiers = HashTable.getNumItems();
-  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
-  unsigned AverageIdentifierSize = 0;
-  unsigned MaxIdentifierLength = 0;
-  
-  // TODO: Figure out maximum times an identifier had to probe for -stats.
-  for (llvm::StringMap<SelectorInfo, llvm::BumpPtrAllocator>::const_iterator
-       I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
-    unsigned IdLen = I->getKeyLength();
-    AverageIdentifierSize += IdLen;
-    if (MaxIdentifierLength < IdLen)
-      MaxIdentifierLength = IdLen;
-  }
-  
-  fprintf(stderr, "\n*** Selector Table Stats:\n");
-  fprintf(stderr, "# Selectors:   %d\n", NumIdentifiers);
-  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
-  fprintf(stderr, "Hash density (#selectors per bucket): %f\n",
-          NumIdentifiers/(double)NumBuckets);
-  fprintf(stderr, "Ave selector length: %f\n",
-          (AverageIdentifierSize/(double)NumIdentifiers));
-  fprintf(stderr, "Max selector length: %d\n", MaxIdentifierLength);
-  
-  // Compute statistics about the memory allocated for identifiers.
-  HashTable.getAllocator().PrintStats();
-}
index 37427020587727f8c8d75905718d77a890f4643b..42024b1adb4cc93984eadaa500f5f2f410c72520 100644 (file)
@@ -1308,8 +1308,8 @@ Sema::DeclTy *Sema::ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
     methodName += ":";
   }
   methodName[len] = '\0';
-  SelectorInfo &SelName = Context.getSelectorInfo(&methodName[0], 
-                                                  &methodName[0]+len);
+  IdentifierInfo &SelName = Context.Idents.get(&methodName[0], 
+                                               &methodName[0]+len);
   llvm::SmallVector<ParmVarDecl*, 16> Params;
 
   for (unsigned i = 0; i < NumKeywords; i++) {
@@ -1340,8 +1340,8 @@ Sema::DeclTy *Sema::ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
                       IdentifierInfo *SelectorName, AttributeList *AttrList,
                      tok::ObjCKeywordKind MethodDeclKind) {
   const char *methodName = SelectorName->getName();
-  SelectorInfo &SelName = Context.getSelectorInfo(methodName, 
-                                                  methodName+strlen(methodName));
+  IdentifierInfo &SelName = Context.Idents.get(methodName, 
+                                              methodName+strlen(methodName));
   QualType resultDeclType = QualType::getFromOpaquePtr(ReturnType);
   ObjcMethodDecl* ObjcMethod = new ObjcMethodDecl(MethodLoc, 
                                     SelName, resultDeclType, 0, -1,
index 886b3dd5bd75e6bd56fe02217ec5013909c9ec46..11c3eccb36a7b3662db3d64cc47d3b1e0fafeaee 100644 (file)
@@ -1857,7 +1857,7 @@ Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
   return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
 }
 
-static SelectorInfo &DeriveSelector(ObjcKeywordMessage *Keywords, 
+static IdentifierInfo &DeriveSelector(ObjcKeywordMessage *Keywords, 
                                     unsigned NumKeywords,
                                     ASTContext &Context) {
   // Derive the selector name from the keyword declarations.
@@ -1875,7 +1875,7 @@ static SelectorInfo &DeriveSelector(ObjcKeywordMessage *Keywords,
     methodName += ":";
   }
   methodName[len] = '\0';
-  return Context.getSelectorInfo(&methodName[0], &methodName[0]+len);
+  return Context.Idents.get(&methodName[0], &methodName[0]+len);
 }
 
 // This actions handles keyword message to classes.
@@ -1884,7 +1884,7 @@ Sema::ExprResult Sema::ActOnKeywordMessage(
   ObjcKeywordMessage *Keywords, unsigned NumKeywords,
   SourceLocation lbrac, SourceLocation rbrac)
 {
-  SelectorInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
+  IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
   assert(receivingClassName && "missing receiver class name");
 
   return new ObjCMessageExpr(receivingClassName, SelName, Keywords, NumKeywords, 
@@ -1895,7 +1895,7 @@ Sema::ExprResult Sema::ActOnKeywordMessage(
 Sema::ExprResult Sema::ActOnKeywordMessage(
   ExprTy *receiver, ObjcKeywordMessage *Keywords, unsigned NumKeywords,
   SourceLocation lbrac, SourceLocation rbrac) {
-  SelectorInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
+  IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
   assert(receiver && "missing receiver expression");
   
   Expr *RExpr = static_cast<Expr *>(receiver);
@@ -1910,7 +1910,7 @@ Sema::ExprResult Sema::ActOnUnaryMessage(
   assert(receivingClassName && "missing receiver class name");
   
   // FIXME: this should be passed in...
-  SelectorInfo &SName = Context.getSelectorInfo(
+  IdentifierInfo &SName = Context.Idents.get(
            selName->getName(), selName->getName()+strlen(selName->getName()));
   return new ObjCMessageExpr(receivingClassName, SName,
                              Context.IntTy/*FIXME*/, lbrac, rbrac);
@@ -1924,7 +1924,7 @@ Sema::ExprResult Sema::ActOnUnaryMessage(
   
   Expr *RExpr = static_cast<Expr *>(receiver);
   // FIXME: this should be passed in...
-  SelectorInfo &SName = Context.getSelectorInfo(
+  IdentifierInfo &SName = Context.Idents.get(
            selName->getName(), selName->getName()+strlen(selName->getName()));
   return new ObjCMessageExpr(RExpr, SName,
                              Context.IntTy/*FIXME*/, lbrac, rbrac);
index 5d753aa6fc0f762c3f2366ff84b12277eee6e8ff..76a51fc254ae83926d416f08c7f2d8689cd89089 100644 (file)
@@ -24,7 +24,6 @@
 
 namespace clang {
   class TargetInfo;
-  class SelectorTable;
   
 /// ASTContext - This class holds long-lived AST nodes (such as types and
 /// decls) that can be referred to throughout the semantic analysis of a file.
@@ -39,7 +38,6 @@ class ASTContext {
   llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
   llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
   RecordDecl *CFConstantStringTypeDecl;
-  SelectorTable *Selectors;
 public:
   SourceManager &SourceMgr;
   TargetInfo &Target;
@@ -57,8 +55,7 @@ public:
   QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
   
   ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents) : 
-    CFConstantStringTypeDecl(0), Selectors(0), 
-    SourceMgr(SM), Target(t), Idents(idents) {
+    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), Idents(idents) {
     InitBuiltinTypes();
     BuiltinInfo.InitializeBuiltins(idents, Target);
   }    
@@ -179,14 +176,6 @@ public:
   /// 'typeSize' is a real floating point or complex type.
   QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 
                                              QualType typeDomain) const;
-
-  //===--------------------------------------------------------------------===//
-  //                            Objective-C
-  //===--------------------------------------------------------------------===//
-  
-  /// getSelectorInfo - Return a uniqued character string for the selector.
-  SelectorInfo &getSelectorInfo(const char *NameStart, const char *NameEnd);
-
 private:
   ASTContext(const ASTContext&); // DO NOT IMPLEMENT
   void operator=(const ASTContext&); // DO NOT IMPLEMENT
index aea0dc822b859756825f4a75fb9533082e61dbda..67f5ebd756676a303e5aa1e91736e52935ad2e1c 100644 (file)
@@ -26,7 +26,6 @@ class FunctionDecl;
 class AttributeList;
 class ObjcIvarDecl;
 class ObjcMethodDecl;
-class SelectorInfo;
 
 
 /// Decl - This represents one declaration (or definition), e.g. a variable, 
@@ -618,7 +617,7 @@ public:
   enum ImplementationControl { None, Required, Optional };
 private:
   // A unigue name for this method.
-  SelectorInfo &Selector;
+  IdentifierInfo &Selector;
   
   // Type of this method.
   QualType MethodDeclType;
@@ -636,7 +635,7 @@ private:
   ImplementationControl DeclImplementation : 2;
 
 public:
-  ObjcMethodDecl(SourceLocation L, SelectorInfo &SelId, QualType T,
+  ObjcMethodDecl(SourceLocation L, IdentifierInfo &SelId, QualType T,
                 ParmVarDecl **paramInfo = 0, int numParams=-1,
                 AttributeList *M = 0, bool isInstance = true, 
                 Decl *PrevDecl = 0)
@@ -644,7 +643,7 @@ public:
       ParamInfo(paramInfo), NumMethodParams(numParams),
       MethodAttrs(M), IsInstance(isInstance) {}
 
-  ObjcMethodDecl(Kind DK, SourceLocation L, SelectorInfo &SelId, QualType T,
+  ObjcMethodDecl(Kind DK, SourceLocation L, IdentifierInfo &SelId, QualType T,
                 ParmVarDecl **paramInfo = 0, int numParams=-1,
                 AttributeList *M = 0, bool isInstance = true, 
                 Decl *PrevDecl = 0)
index 84d9a791b9a233ade19cb547ea5be4118cd21050..24eecf597c1a2348c51c4c9e5c11d3709db1a5e1 100644 (file)
@@ -1068,7 +1068,7 @@ class ObjCMessageExpr : public Expr {
   unsigned NumArgs;
   
   // A unigue name for this message.
-  SelectorInfo &Selector;
+  IdentifierInfo &Selector;
   
   IdentifierInfo **KeyIdents;
   
@@ -1078,17 +1078,17 @@ class ObjCMessageExpr : public Expr {
 public:
   // constructor for unary messages. 
   // FIXME: clsName should be typed to ObjCInterfaceType
-  ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo &selInfo,
+  ObjCMessageExpr(IdentifierInfo *clsName, IdentifierInfo &selInfo,
                   QualType retType, SourceLocation LBrac, SourceLocation RBrac);
-  ObjCMessageExpr(Expr *receiver, SelectorInfo &selInfo,
+  ObjCMessageExpr(Expr *receiver, IdentifierInfo &selInfo,
                   QualType retType, SourceLocation LBrac, SourceLocation RBrac);
                   
   // constructor for keyword messages.
   // FIXME: clsName should be typed to ObjCInterfaceType
-  ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo &selInfo,
+  ObjCMessageExpr(IdentifierInfo *clsName, IdentifierInfo &selInfo,
                   ObjcKeywordMessage *keys, unsigned numargs, QualType retType, 
                   SourceLocation LBrac, SourceLocation RBrac);
-  ObjCMessageExpr(Expr *receiver, SelectorInfo &selInfo,
+  ObjCMessageExpr(Expr *receiver, IdentifierInfo &selInfo,
                   ObjcKeywordMessage *keys, unsigned numargs, QualType retType, 
                   SourceLocation LBrac, SourceLocation RBrac);
   ~ObjCMessageExpr() {
index 9a6a93ec3649bf85c2be6d360e6006b54d7ceaeb..fd2cb6ae717807f8d0938198004c3bdb05caf0b8 100644 (file)
@@ -166,54 +166,6 @@ private:
   void AddKeywords(const LangOptions &LangOpts);
 };
 
-/// SelectorInfo - One of these records is kept for each selector. Selectors
-/// are created as a by-product of parsing a method declaration/definition, 
-/// message expression, or @selector expression.
-class SelectorInfo {
-  void *ObjcMethodDecl;  // FIXME: add setter/getter.
-  
-  SelectorInfo(const SelectorInfo&);  // NONCOPYABLE.
-public:
-  SelectorInfo() : ObjcMethodDecl(0) {}
-
-  /// getName - Return the actual string for this selector.  The returned 
-  /// string is properly null terminated.
-  ///
-  const char *getName() const {
-    // String data is stored immediately after the IdentifierInfo object.
-    return (const char*)(this+1);
-  }
-};
-
-/// SelectorTable - This table implements an efficient mapping from strings to
-/// SelectorInfo nodes.  
-class SelectorTable {
-  // Shark shows that using MallocAllocator is *much* slower than using this
-  // BumpPtrAllocator!
-  typedef llvm::StringMap<SelectorInfo, llvm::BumpPtrAllocator> HashTableTy;
-  HashTableTy HashTable;
-public:
-  SelectorTable() : HashTable(4096) { }
-  
-  /// get - Return the selector info for the specified name.
-  ///
-  SelectorInfo &get(const char *NameStart, const char *NameEnd) {
-    return HashTable.GetOrCreateValue(NameStart, NameEnd).getValue();
-  }
-  SelectorInfo &get(const char *Name) {
-    return get(Name, Name+strlen(Name));
-  }
-  typedef HashTableTy::const_iterator iterator;
-  typedef HashTableTy::const_iterator const_iterator;
-  
-  iterator begin() const { return HashTable.begin(); }
-  iterator end() const   { return HashTable.end(); }
-  
-  /// PrintStats - Print some statistics to stderr that indicate how well the
-  /// hashing is doing.
-  void PrintStats() const;
-};
-
 }  // end namespace clang
 
 #endif