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
diff --git a/Lex/Preprocessor.cpp b/Lex/Preprocessor.cpp
index 4e3f68e..8376b9f 100644
--- a/Lex/Preprocessor.cpp
+++ b/Lex/Preprocessor.cpp
@@ -48,7 +48,7 @@
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 @@
// 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 @@
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 @@
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 @@
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 @@
/// 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 @@
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;