From e495ef2c4853579276f6790b69f39cb3ac92997b Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 4 May 2012 12:45:22 -0700 Subject: [PATCH] Reduce dynamic memory allocation by reusing scope maps in symbol table. --- sym.cpp | 13 +++++++++++-- sym.h | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/sym.cpp b/sym.cpp index 0a211884..42d1f66f 100644 --- a/sym.cpp +++ b/sym.cpp @@ -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(); } diff --git a/sym.h b/sym.h index 43c8ff16..07bbe187 100644 --- a/sym.h +++ b/sym.h @@ -260,6 +260,8 @@ private: typedef std::map SymbolMapType; std::vector variables; + std::vector 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