Approximate 20% reduction in runtime on long running scripts, based on profiling data. Reduce number of execution of object cache culling, reduction of copies of the stack and reduction of Boxed_Value copies

This commit is contained in:
Jason Turner 2009-08-05 02:43:13 +00:00
parent 5b424be4ed
commit ba6b392174
3 changed files with 43 additions and 24 deletions

View File

@ -116,6 +116,11 @@ namespace chaiscript
*/ */
struct Object_Cache struct Object_Cache
{ {
Object_Cache()
: m_cullcount(0)
{
}
boost::shared_ptr<Data> get(Boxed_Value::Void_Type) boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
{ {
return boost::shared_ptr<Data> (new Data( return boost::shared_ptr<Data> (new Data(
@ -219,6 +224,12 @@ namespace chaiscript
*/ */
void cull() void cull()
{ {
++m_cullcount;
if (m_cullcount % 10 != 0)
{
return;
}
std::map<const void *, Data >::iterator itr = m_ptrs.begin(); std::map<const void *, Data >::iterator itr = m_ptrs.begin();
while (itr != m_ptrs.end()) while (itr != m_ptrs.end())
@ -235,6 +246,7 @@ namespace chaiscript
} }
std::map<const void *, Data > m_ptrs; std::map<const void *, Data > m_ptrs;
int m_cullcount;
}; };
public: public:
@ -472,7 +484,7 @@ namespace chaiscript
template<> template<>
struct Cast_Helper<Boxed_Value> struct Cast_Helper<Boxed_Value>
{ {
typedef Boxed_Value Result_Type; typedef const Boxed_Value & Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob)
{ {
@ -486,7 +498,7 @@ namespace chaiscript
template<> template<>
struct Cast_Helper<const Boxed_Value &> struct Cast_Helper<const Boxed_Value &>
{ {
typedef Boxed_Value Result_Type; typedef const Boxed_Value & Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob)
{ {

View File

@ -133,12 +133,13 @@ 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> Stack; typedef boost::shared_ptr<std::deque<Scope> > Stack;
Dispatch_Engine() Dispatch_Engine()
: m_place_holder(boost::shared_ptr<Placeholder_Object>(new Placeholder_Object())) : m_scopes(new Stack::element_type()),
m_place_holder(boost::shared_ptr<Placeholder_Object>(new Placeholder_Object()))
{ {
m_scopes.push_back(Scope()); m_scopes->push_back(Scope());
} }
/** /**
@ -163,12 +164,12 @@ namespace chaiscript
*/ */
void add(const Boxed_Value &obj, const std::string &name) void add(const Boxed_Value &obj, const std::string &name)
{ {
for (int i = m_scopes.size()-1; i >= 0; --i) for (int i = m_scopes->size()-1; i >= 0; --i)
{ {
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name); std::map<std::string, Boxed_Value>::const_iterator itr = (*m_scopes)[i].find(name);
if (itr != m_scopes[i].end()) if (itr != (*m_scopes)[i].end())
{ {
m_scopes[i][name] = Boxed_Value(obj); (*m_scopes)[i][name] = Boxed_Value(obj);
return; return;
} }
} }
@ -181,7 +182,7 @@ namespace chaiscript
*/ */
void add_object(const std::string &name, const Boxed_Value &obj) void add_object(const std::string &name, const Boxed_Value &obj)
{ {
m_scopes.back()[name] = Boxed_Value(obj); m_scopes->back()[name] = Boxed_Value(obj);
} }
/** /**
@ -189,7 +190,7 @@ namespace chaiscript
*/ */
void new_scope() void new_scope()
{ {
m_scopes.push_back(Scope()); m_scopes->push_back(Scope());
} }
/** /**
@ -197,9 +198,9 @@ namespace chaiscript
*/ */
void pop_scope() void pop_scope()
{ {
if (m_scopes.size() > 1) if (m_scopes->size() > 1)
{ {
m_scopes.pop_back(); m_scopes->pop_back();
} else { } else {
throw std::range_error("Unable to pop global stack"); throw std::range_error("Unable to pop global stack");
} }
@ -218,10 +219,16 @@ namespace chaiscript
* \returns the old stack * \returns the old stack
* \param[in] s The new stack * \param[in] s The new stack
*/ */
Stack set_stack(Stack s) Stack set_stack(const Stack &s)
{ {
std::swap(s, m_scopes); Stack old = m_scopes;
return s; m_scopes = s;
return old;
}
Stack new_stack()
{
return Stack(new Stack::element_type());
} }
/** /**
@ -236,10 +243,10 @@ namespace chaiscript
return m_place_holder; return m_place_holder;
} }
for (int i = m_scopes.size()-1; i >= 0; --i) for (int i = m_scopes->size()-1; i >= 0; --i)
{ {
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name); std::map<std::string, Boxed_Value>::const_iterator itr = (*m_scopes)[i].find(name);
if (itr != m_scopes[i].end()) if (itr != (*m_scopes)[i].end())
{ {
return itr->second; return itr->second;
} }
@ -377,7 +384,7 @@ namespace chaiscript
return true; return true;
} }
std::deque<Scope> m_scopes; Stack m_scopes;
std::multimap<std::string, Proxy_Function > m_functions; std::multimap<std::string, Proxy_Function > m_functions;
Type_Name_Map m_types; Type_Name_Map m_types;

View File

@ -405,10 +405,10 @@ namespace chaiscript
Boxed_Value retval; Boxed_Value retval;
Param_List_Builder plb; Param_List_Builder plb;
Dispatch_Engine::Stack prev_stack = ss.get_stack(); Dispatch_Engine::Stack prev_stack = ss.get_stack();
Dispatch_Engine::Stack new_stack; Dispatch_Engine::Stack new_stack = ss.new_stack();
unsigned int i; unsigned int i;
new_stack.push_back(Dispatch_Engine::Scope()); new_stack->push_back(Dispatch_Engine::Scope());
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) { if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) { for (i = 0; i < node->children[1]->children.size(); ++i) {
@ -452,10 +452,10 @@ namespace chaiscript
Boxed_Value retval; Boxed_Value retval;
std::vector<std::pair<std::string, Proxy_Function > > fn; std::vector<std::pair<std::string, Proxy_Function > > fn;
Dispatch_Engine::Stack prev_stack = ss.get_stack(); Dispatch_Engine::Stack prev_stack = ss.get_stack();
Dispatch_Engine::Stack new_stack; Dispatch_Engine::Stack new_stack = ss.new_stack();
unsigned int i, j; unsigned int i, j;
new_stack.push_back(Dispatch_Engine::Scope()); new_stack->push_back(Dispatch_Engine::Scope());
//todo: Please extract a single way of doing function calls between this and eval_fun_call //todo: Please extract a single way of doing function calls between this and eval_fun_call