From 7a4af3b73ddb9dc9dfc5441a51b64bcad588ea74 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 26 Jul 2006 06:26:52 +0000 Subject: [PATCH] Change Preprocessor::ReadFunctionLikeMacroArgs to use a SmallVector to lex argument tokens into instead of a real vector. This avoids some malloc traffic in common cases. In an "abusive macro expansion" testcase, this reduced -Eonly time by 25%. llvm-svn: 38757 --- clang/Lex/MacroExpander.cpp | 6 +++--- clang/Lex/Preprocessor.cpp | 8 +++++--- clang/include/clang/Lex/MacroExpander.h | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/clang/Lex/MacroExpander.cpp b/clang/Lex/MacroExpander.cpp index 24aab614c8af..8fb569afd372 100644 --- a/clang/Lex/MacroExpander.cpp +++ b/clang/Lex/MacroExpander.cpp @@ -26,12 +26,12 @@ using namespace clang; /// MacroArgs ctor function - This destroys the vector passed in. MacroArgs *MacroArgs::create(const MacroInfo *MI, - const std::vector &UnexpArgTokens) { + const LexerToken *UnexpArgTokens, + unsigned NumToks) { assert(MI->isFunctionLike() && "Can't have args for an object-like macro!"); // Allocate memory for the MacroArgs object with the lexer tokens at the end. - unsigned NumToks = UnexpArgTokens.size(); MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) + NumToks*sizeof(LexerToken)); // Construct the macroargs object. @@ -40,7 +40,7 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI, // Copy the actual unexpanded tokens to immediately after the result ptr. if (NumToks) memcpy(const_cast(Result->getUnexpArgument(0)), - &UnexpArgTokens[0], NumToks*sizeof(LexerToken)); + UnexpArgTokens, NumToks*sizeof(LexerToken)); return Result; } diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp index 71f315d03d8a..1a95b887f393 100644 --- a/clang/Lex/Preprocessor.cpp +++ b/clang/Lex/Preprocessor.cpp @@ -33,6 +33,7 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" +#include "llvm/ADT/SmallVector.h" #include using namespace llvm; using namespace clang; @@ -709,8 +710,9 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName, --NumFixedArgsLeft; // Start reading the first arg. // ArgTokens - Build up a list of tokens that make up each argument. Each - // argument is separated by an EOF token. - std::vector ArgTokens; + // argument is separated by an EOF token. Use a SmallVector so we can avoid + // heap allocations in the common case. + SmallVector ArgTokens; unsigned NumActuals = 0; while (Tok.getKind() == tok::comma) { @@ -808,7 +810,7 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName, } } - return MacroArgs::create(MI, ArgTokens); + return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size()); } /// ComputeDATE_TIME - Compute the current time, enter it into the specified diff --git a/clang/include/clang/Lex/MacroExpander.h b/clang/include/clang/Lex/MacroExpander.h index c001182d2588..f5f802a0d68f 100644 --- a/clang/include/clang/Lex/MacroExpander.h +++ b/clang/include/clang/Lex/MacroExpander.h @@ -47,7 +47,8 @@ public: /// MacroArgs ctor function - Create a new MacroArgs object with the specified /// macro and argument info. static MacroArgs *create(const MacroInfo *MI, - const std::vector &UnexpArgTokens); + const LexerToken *UnexpArgTokens, + unsigned NumArgTokens); /// destroy - Destroy and deallocate the memory for this object. ///