Eliminate extra dynamic allocation in the Stack

This commit is contained in:
Jason Turner 2014-10-05 08:58:29 -06:00
parent 5986531bba
commit 5619f2602d

View File

@ -389,8 +389,7 @@ namespace chaiscript
public: public:
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map; typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope; typedef std::map<std::string, Boxed_Value> Scope;
typedef std::deque<Scope> StackData; typedef std::vector<Scope> StackData;
typedef std::shared_ptr<StackData> Stack;
struct State struct State
{ {
@ -528,7 +527,7 @@ namespace chaiscript
void new_stack() void new_stack()
{ {
// add a new Stack with 1 element // add a new Stack with 1 element
m_stack_holder->stacks.emplace_back(std::make_shared<StackData>(1)); m_stack_holder->stacks.emplace_back(1);
} }
void pop_stack() void pop_stack()
@ -536,12 +535,6 @@ namespace chaiscript
m_stack_holder->stacks.pop_back(); m_stack_holder->stacks.pop_back();
} }
/// \returns the current stack
Stack get_stack() const
{
return m_stack_holder->stacks.back();
}
/// Searches the current stack for an object of the given name /// Searches the current stack for an object of the given name
/// includes a special overload for the _ place holder object to /// includes a special overload for the _ place holder object to
/// ensure that it is always in scope. /// ensure that it is always in scope.
@ -718,7 +711,7 @@ namespace chaiscript
Stack_Holder &s = *m_stack_holder; Stack_Holder &s = *m_stack_holder;
// We don't want the current context, but one up if it exists // We don't want the current context, but one up if it exists
StackData &stack = (s.stacks.size()==1)?(*(s.stacks.back())):(*s.stacks[s.stacks.size()-2]); StackData &stack = (s.stacks.size()==1)?(s.stacks.back()):(s.stacks[s.stacks.size()-2]);
std::map<std::string, Boxed_Value> retval; std::map<std::string, Boxed_Value> retval;
@ -966,7 +959,7 @@ namespace chaiscript
/// make const/non const versions /// make const/non const versions
StackData &get_stack_data() const StackData &get_stack_data() const
{ {
return *(m_stack_holder->stacks.back()); return m_stack_holder->stacks.back();
} }
const std::map<std::string, Proxy_Function> &get_function_objects_int() const const std::map<std::string, Proxy_Function> &get_function_objects_int() const
@ -1147,10 +1140,10 @@ namespace chaiscript
Stack_Holder() Stack_Holder()
: call_depth(0) : call_depth(0)
{ {
stacks.emplace_back(std::make_shared<StackData>(1)); stacks.emplace_back(1);
} }
std::deque<Stack> stacks; std::deque<StackData> stacks;
std::list<Boxed_Value> call_params; std::list<Boxed_Value> call_params;
int call_depth; int call_depth;