Reduce dynamic memory allocation by reusing scope maps in symbol table.
This commit is contained in:
13
sym.cpp
13
sym.cpp
@@ -73,14 +73,23 @@ SymbolTable::~SymbolTable() {
|
|||||||
|
|
||||||
void
|
void
|
||||||
SymbolTable::PushScope() {
|
SymbolTable::PushScope() {
|
||||||
variables.push_back(new SymbolMapType);
|
SymbolMapType *sm;
|
||||||
|
if (freeSymbolMaps.size() > 0) {
|
||||||
|
sm = freeSymbolMaps.back();
|
||||||
|
freeSymbolMaps.pop_back();
|
||||||
|
sm->erase(sm->begin(), sm->end());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
sm = new SymbolMapType;
|
||||||
|
|
||||||
|
variables.push_back(sm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SymbolTable::PopScope() {
|
SymbolTable::PopScope() {
|
||||||
Assert(variables.size() > 1);
|
Assert(variables.size() > 1);
|
||||||
delete variables.back();
|
freeSymbolMaps.push_back(variables.back());
|
||||||
variables.pop_back();
|
variables.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
sym.h
2
sym.h
@@ -260,6 +260,8 @@ private:
|
|||||||
typedef std::map<std::string, Symbol *> SymbolMapType;
|
typedef std::map<std::string, Symbol *> SymbolMapType;
|
||||||
std::vector<SymbolMapType *> variables;
|
std::vector<SymbolMapType *> variables;
|
||||||
|
|
||||||
|
std::vector<SymbolMapType *> freeSymbolMaps;
|
||||||
|
|
||||||
/** Function declarations are *not* scoped. (C99, for example, allows
|
/** Function declarations are *not* scoped. (C99, for example, allows
|
||||||
an implementation to maintain function declarations in a single
|
an implementation to maintain function declarations in a single
|
||||||
namespace.) A STL \c vector is used to store the function symbols
|
namespace.) A STL \c vector is used to store the function symbols
|
||||||
|
|||||||
Reference in New Issue
Block a user