aboutsummaryrefslogtreecommitdiff
path: root/Lex/Preprocessor.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-07-15 00:25:26 +0000
committerChris Lattner <sabre@nondot.org>2007-07-15 00:25:26 +0000
commit9594acf32de2939b15eafa8fe818607bfc56bf66 (patch)
tree7a6b83a0b4ddccddcd1c4e8de7d89edc09b0e39d /Lex/Preprocessor.cpp
parent9e344c65b1e8b83e1d3ada507cf653526ff2c005 (diff)
Cache macro expander objects to avoid thrashing malloc in heavy expansion situations.
This doesn't significantly improve carbon.h, but it does speed up INPUTS/macro_pounder_obj.c by 48% git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39864 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Lex/Preprocessor.cpp')
-rw-r--r--Lex/Preprocessor.cpp39
1 files changed, 33 insertions, 6 deletions
diff --git a/Lex/Preprocessor.cpp b/Lex/Preprocessor.cpp
index 4e3f68e1a1..8376b9f8a3 100644
--- a/Lex/Preprocessor.cpp
+++ b/Lex/Preprocessor.cpp
@@ -48,7 +48,7 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
ScratchBuf = new ScratchBuffer(SourceMgr);
-
+
// Clear stats.
NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
NumIf = NumElse = NumEndif = 0;
@@ -65,6 +65,7 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
// Macro expansion is enabled.
DisableMacroExpansion = false;
InMacroArgs = false;
+ NumCachedMacroExpanders = 0;
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
// This gets unpoisoned where it is allowed.
@@ -88,6 +89,10 @@ Preprocessor::~Preprocessor() {
IncludeMacroStack.pop_back();
}
+ // Free any cached macro expanders.
+ for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
+ delete MacroExpanderCache[i];
+
// Release pragma information.
delete PragmaHandlers;
@@ -386,7 +391,12 @@ void Preprocessor::EnterMacro(LexerToken &Tok, MacroArgs *Args) {
CurLexer = 0;
CurDirLookup = 0;
- CurMacroExpander = new MacroExpander(Tok, Args, *this);
+ if (NumCachedMacroExpanders == 0) {
+ CurMacroExpander = new MacroExpander(Tok, Args, *this);
+ } else {
+ CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
+ CurMacroExpander->Init(Tok, Args);
+ }
}
/// EnterTokenStream - Add a "macro" context to the top of the include stack,
@@ -402,7 +412,12 @@ void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) {
CurDirLookup = 0;
// Create a macro expander to expand from the specified token stream.
- CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
+ if (NumCachedMacroExpanders == 0) {
+ CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
+ } else {
+ CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
+ CurMacroExpander->Init(Toks, NumToks);
+ }
}
/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
@@ -410,8 +425,16 @@ void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) {
/// state of the top-of-stack lexer is known.
void Preprocessor::RemoveTopOfLexerStack() {
assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
- delete CurLexer;
- delete CurMacroExpander;
+
+ if (CurMacroExpander) {
+ // Delete or cache the now-dead macro expander.
+ if (NumCachedMacroExpanders == MacroExpanderCacheSize)
+ delete CurMacroExpander;
+ else
+ MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
+ } else {
+ delete CurLexer;
+ }
CurLexer = IncludeMacroStack.back().TheLexer;
CurDirLookup = IncludeMacroStack.back().TheDirLookup;
CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
@@ -1047,7 +1070,11 @@ bool Preprocessor::HandleEndOfMacro(LexerToken &Result) {
assert(CurMacroExpander && !CurLexer &&
"Ending a macro when currently in a #include file!");
- delete CurMacroExpander;
+ // Delete or cache the now-dead macro expander.
+ if (NumCachedMacroExpanders == MacroExpanderCacheSize)
+ delete CurMacroExpander;
+ else
+ MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
// Handle this like a #include file being popped off the stack.
CurMacroExpander = 0;