From 28124e4b33b9ff77b39dd3f509cc64870d6bb3d5 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 14 Nov 2014 20:28:53 -0700 Subject: [PATCH] Fix stack memory management From 747M to 6.2M for profiling tests. --- include/chaiscript/dispatchkit/dispatchkit.hpp | 17 +++++++++-------- include/chaiscript/language/chaiscript_eval.hpp | 3 ++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 9d78507..68f9bdd 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -497,11 +497,13 @@ namespace chaiscript void new_scope() { get_stack_data().emplace_back(); + m_stack_holder->call_params.emplace_back(); } /// Pops the current scope from the stack void pop_scope() { + m_stack_holder->call_params.pop_back(); StackData &stack = get_stack_data(); if (stack.size() > 1) { @@ -918,7 +920,7 @@ namespace chaiscript void save_function_params(std::initializer_list t_params) { Stack_Holder &s = *m_stack_holder; - s.call_params.insert(s.call_params.begin(), std::move(t_params)); + s.call_params.back().insert(s.call_params.back().begin(), std::move(t_params)); } void save_function_params(std::vector &&t_params) @@ -927,14 +929,14 @@ namespace chaiscript for (auto &¶m : t_params) { - s.call_params.insert(s.call_params.begin(), std::move(param)); + s.call_params.back().insert(s.call_params.back().begin(), std::move(param)); } } void save_function_params(const std::vector &t_params) { Stack_Holder &s = *m_stack_holder; - s.call_params.insert(s.call_params.begin(), t_params.begin(), t_params.end()); + s.call_params.back().insert(s.call_params.back().begin(), t_params.begin(), t_params.end()); } void new_function_call() @@ -945,7 +947,7 @@ namespace chaiscript m_conversions.enable_conversion_saves(true); } - ++m_stack_holder->call_depth; + ++s.call_depth; save_function_params(m_conversions.take_saves()); } @@ -959,9 +961,7 @@ namespace chaiscript if (s.call_depth == 0) { - /// \todo Critical: this needs to be smarter, memory can expand quickly - /// in tight loops involving function calls - s.call_params.clear(); + s.call_params.back().clear(); m_conversions.enable_conversion_saves(false); } } @@ -1153,11 +1153,12 @@ namespace chaiscript : call_depth(0) { stacks.emplace_back(1); + call_params.emplace_back(); } std::deque stacks; - std::list call_params; + std::deque> call_params; int call_depth; }; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 36978ab..f87b87f 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -382,6 +382,7 @@ namespace chaiscript virtual ~Equation_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { + chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); Boxed_Value retval = this->children.back()->eval(t_ss); @@ -595,8 +596,8 @@ namespace chaiscript AST_Node(t_ast_node_text, AST_Node_Type::Dot_Access, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } virtual ~Dot_Access_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{ - Boxed_Value retval = this->children[0]->eval(t_ss); chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); + Boxed_Value retval = this->children[0]->eval(t_ss); if (this->children.size() > 1) { for (size_t i = 2; i < this->children.size(); i+=2) {