Reduce dynamic memory allocation by reusing scope maps in symbol table.

This commit is contained in:
Matt Pharr
2012-05-04 12:45:22 -07:00
parent 78d62705cc
commit e495ef2c48
2 changed files with 13 additions and 2 deletions

13
sym.cpp
View File

@@ -73,14 +73,23 @@ SymbolTable::~SymbolTable() {
void
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
SymbolTable::PopScope() {
Assert(variables.size() > 1);
delete variables.back();
freeSymbolMaps.push_back(variables.back());
variables.pop_back();
}

2
sym.h
View File

@@ -260,6 +260,8 @@ private:
typedef std::map<std::string, Symbol *> SymbolMapType;
std::vector<SymbolMapType *> variables;
std::vector<SymbolMapType *> freeSymbolMaps;
/** Function declarations are *not* scoped. (C99, for example, allows
an implementation to maintain function declarations in a single
namespace.) A STL \c vector is used to store the function symbols