[cfe-commits] r61042 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPDirectives.cpp lib/Lex/PPMacroExpansion.cpp lib/Lex/Preprocessor.cpp

Ted Kremenek kremenek at apple.com
Mon Dec 15 11:56:43 PST 2008


Author: kremenek
Date: Mon Dec 15 13:56:42 2008
New Revision: 61042

URL: http://llvm.org/viewvc/llvm-project?rev=61042&view=rev
Log:
Preprocessor: Allocate MacroInfo objects using a BumpPtrAllocator instead using new/delete.  This speeds up -Eonly on Cocoa.h using the regular lexer by 1.8% and the PTHLexer by 3%.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Mon Dec 15 13:56:42 2008
@@ -24,6 +24,7 @@
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/Allocator.h"
 
 namespace clang {
   
@@ -55,6 +56,10 @@
   /// PTH - An optional PTHManager object used for getting tokens from
   ///  a token cache rather than lexing the original source file.
   llvm::OwningPtr<PTHManager> PTH;
+  
+  /// BP - A BumpPtrAllocator object used to quickly allocate and release
+  ///  objects internal to the Preprocessor.
+  llvm::BumpPtrAllocator BP;
     
   /// Identifiers for builtin macros and other builtins.
   IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
@@ -145,6 +150,10 @@
   /// to the actual definition of the macro.
   llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
   
+  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
+  ///  allocation.
+  std::vector<MacroInfo*> MICache;
+  
   // Various statistics we track for performance analysis.
   unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
   unsigned NumIf, NumElse, NumEndif;
@@ -530,6 +539,16 @@
     IncludeMacroStack.pop_back();
   }
   
+  /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide
+  ///  SourceLocation.
+  MacroInfo* AllocateMacroInfo(SourceLocation L);
+  
+  /// ReleaseMacroInfo - Release the specified MacroInfo.  This memory will
+  ///  be reused for allocating new MacroInfo objects.
+  void ReleaseMacroInfo(MacroInfo* MI) {
+    MICache.push_back(MI);
+  }
+  
   /// isInPrimaryFile - Return true if we're in the top-level file, not in a
   /// #include.
   bool isInPrimaryFile() const;

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=61042&r1=61041&r2=61042&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Mon Dec 15 13:56:42 2008
@@ -22,6 +22,18 @@
 // Utility Methods for Preprocessor Directive Handling.
 //===----------------------------------------------------------------------===//
 
+MacroInfo* Preprocessor::AllocateMacroInfo(SourceLocation L) {
+  MacroInfo *MI;
+  
+  if (!MICache.empty()) {
+    MI = MICache.back();
+    MICache.pop_back();
+  }
+  else MI = (MacroInfo*) BP.Allocate<MacroInfo>();
+  new (MI) MacroInfo(L);
+  return MI;
+}
+
 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
 /// current line until the tok::eom token is found.
 void Preprocessor::DiscardUntilEndOfDirective() {
@@ -920,7 +932,7 @@
   if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
   
   // Create the new macro.
-  MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
+  MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
   
   Token Tok;
   LexUnexpandedToken(Tok);
@@ -935,7 +947,7 @@
     MI->setIsFunctionLike();
     if (ReadMacroDefinitionArgList(MI)) {
       // Forget about MI.
-      delete MI;
+      ReleaseMacroInfo(MI);
       // Throw away the rest of the line.
       if (CurPPLexer->ParsingPreprocessorDirective)
         DiscardUntilEndOfDirective();
@@ -998,7 +1010,7 @@
       if (!Tok.getIdentifierInfo() ||
           MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
         Diag(Tok, diag::err_pp_stringize_not_parameter);
-        delete MI;
+        ReleaseMacroInfo(MI);
         
         // Disable __VA_ARGS__ again.
         Ident__VA_ARGS__->setIsPoisoned(true);
@@ -1023,12 +1035,12 @@
   if (NumTokens != 0) {
     if (MI->getReplacementToken(0).is(tok::hashhash)) {
       Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
-      delete MI;
+      ReleaseMacroInfo(MI);
       return;
     }
     if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
       Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
-      delete MI;
+      ReleaseMacroInfo(MI);
       return;
     }
   }
@@ -1051,7 +1063,7 @@
         << MacroNameTok.getIdentifierInfo();
       Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
     }
-    delete OtherMI;
+    ReleaseMacroInfo(OtherMI);
   }
   
   setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
@@ -1082,7 +1094,7 @@
     Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
   
   // Free macro definition.
-  delete MI;
+  ReleaseMacroInfo(MI);
   setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
 }
 

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=61042&r1=61041&r2=61042&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Mon Dec 15 13:56:42 2008
@@ -42,7 +42,7 @@
   IdentifierInfo *Id = getIdentifierInfo(Name);
   
   // Mark it as being a macro that is builtin.
-  MacroInfo *MI = new MacroInfo(SourceLocation());
+  MacroInfo *MI = AllocateMacroInfo(SourceLocation());
   MI->setIsBuiltinMacro();
   setMacroInfo(Id, MI);
   return Id;

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=61042&r1=61041&r2=61042&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Mon Dec 15 13:56:42 2008
@@ -95,9 +95,9 @@
   // Free any macro definitions.
   for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
        Macros.begin(), E = Macros.end(); I != E; ++I) {
-    // Free the macro definition.
-    delete I->second;
-    I->second = 0;
+    // We don't need to free the MacroInfo objects directly.  These
+    // will be released when the BumpPtrAllocator 'BP' object gets
+    // destroyed.
     I->first->setHasMacroDefinition(false);
   }
   





More information about the cfe-commits mailing list