[cfe-commits] r42139 - in /cfe/trunk: 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

Steve Naroff snaroff at apple.com
Wed Sep 19 09:18:46 PDT 2007


Author: snaroff
Date: Wed Sep 19 11:18:46 2007
New Revision: 42139

URL: http://llvm.org/viewvc/llvm-project?rev=42139&view=rev
Log:

Remove SelectorTable/SelectorInfo, simply store all selectors in the central IdentifierTable.

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



Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/Lex/IdentifierTable.cpp
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Lex/IdentifierTable.h

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

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Wed Sep 19 11:18:46 2007
@@ -104,9 +104,6 @@
     NumFunctionP*sizeof(FunctionTypeProto)+
     NumFunctionNP*sizeof(FunctionTypeNoProto)+
     NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
-  
-  if (Selectors)
-    Selectors->PrintStats();
 }
 
 
@@ -809,11 +806,3 @@
   
   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);
-}
-

Modified: cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Expr.cpp?rev=42139&r1=42138&r2=42139&view=diff

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Wed Sep 19 11:18:46 2007
@@ -858,7 +858,7 @@
 
 // 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(
-  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 @@
 
 // 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(
-  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];

Modified: cfe/trunk/Lex/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/IdentifierTable.cpp?rev=42139&r1=42138&r2=42139&view=diff

==============================================================================
--- cfe/trunk/Lex/IdentifierTable.cpp (original)
+++ cfe/trunk/Lex/IdentifierTable.cpp Wed Sep 19 11:18:46 2007
@@ -209,34 +209,3 @@
   // 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();
-}

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=42139&r1=42138&r2=42139&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Wed Sep 19 11:18:46 2007
@@ -1308,8 +1308,8 @@
     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 @@
                       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,

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=42139&r1=42138&r2=42139&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Sep 19 11:18:46 2007
@@ -1857,7 +1857,7 @@
   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 @@
     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 @@
   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(
   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 @@
   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 @@
   
   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);

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Wed Sep 19 11:18:46 2007
@@ -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 @@
   llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
   llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
   RecordDecl *CFConstantStringTypeDecl;
-  SelectorTable *Selectors;
 public:
   SourceManager &SourceMgr;
   TargetInfo &Target;
@@ -57,8 +55,7 @@
   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 @@
   /// '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

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Sep 19 11:18:46 2007
@@ -26,7 +26,6 @@
 class AttributeList;
 class ObjcIvarDecl;
 class ObjcMethodDecl;
-class SelectorInfo;
 
 
 /// Decl - This represents one declaration (or definition), e.g. a variable, 
@@ -618,7 +617,7 @@
   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 @@
   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 @@
       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)

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Wed Sep 19 11:18:46 2007
@@ -1068,7 +1068,7 @@
   unsigned NumArgs;
   
   // A unigue name for this message.
-  SelectorInfo &Selector;
+  IdentifierInfo &Selector;
   
   IdentifierInfo **KeyIdents;
   
@@ -1078,17 +1078,17 @@
 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() {

Modified: cfe/trunk/include/clang/Lex/IdentifierTable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/IdentifierTable.h?rev=42139&r1=42138&r2=42139&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/IdentifierTable.h (original)
+++ cfe/trunk/include/clang/Lex/IdentifierTable.h Wed Sep 19 11:18:46 2007
@@ -166,54 +166,6 @@
   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





More information about the cfe-commits mailing list