Skip to content

Commit

Permalink
Merge pull request ChaiScript#151 from ChaiScript/performance_test
Browse files Browse the repository at this point in the history
Performance test
  • Loading branch information
lefticus committed Dec 17, 2014
2 parents 60fe242 + 7b7e717 commit 019ea57
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 84 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ if(BUILD_SAMPLES)
target_link_libraries(memory_leak_test ${LIBS})
add_executable(inheritance samples/inheritance.cpp)
target_link_libraries(inheritance ${LIBS})
add_executable(fun_call_performance samples/fun_call_performance.cpp)
target_link_libraries(fun_call_performance ${LIBS})
endif()


Expand Down
6 changes: 3 additions & 3 deletions include/chaiscript/dispatchkit/bootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ namespace chaiscript

static void print(const std::string &s)
{
std::cout << s;
fwrite(s.c_str(), 1, s.size(), stdout);
}

static void println(const std::string &s)
{
std::cout << s << std::endl;
puts(s.c_str());
}


Expand Down Expand Up @@ -443,7 +443,7 @@ namespace chaiscript
operators::assign<bool>(m);
operators::equal<bool>(m);

m->add(fun(&to_string<const std::string &>), "to_string");
m->add(fun<std::string (const std::string &t_ss)>([](const std::string &s) -> std::string { return s; }), "to_string");
m->add(fun(&Bootstrap::bool_to_string), "to_string");
m->add(fun(&unknown_assign), "=");
m->add(fun(&throw_exception), "throw");
Expand Down
4 changes: 2 additions & 2 deletions include/chaiscript/dispatchkit/boxed_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ namespace chaiscript
if (t_conversions && t_conversions->convertable_type<Type>())
{
try {
// std::cout << "trying an up conversion " << typeid(Type).name() << std::endl;
// std::cout << "trying an up conversion " << typeid(Type).name() << '\n';
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
// either way, we are not responsible if it doesn't work
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_conversion<Type>(bv), t_conversions);
} catch (...) {
try {
// std::cout << "trying a down conversion " << typeid(Type).name() << std::endl;
// std::cout << "trying a down conversion " << typeid(Type).name() << '\n';
// try going the other way - down the inheritance graph
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_down_conversion<Type>(bv), t_conversions);
} catch (const chaiscript::detail::exception::bad_any_cast &) {
Expand Down
24 changes: 10 additions & 14 deletions include/chaiscript/dispatchkit/dispatchkit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,13 @@ namespace chaiscript


/// Adds a named object to the current scope
/// \warning This version does not check the validity of the name
/// it is meant for internal use only
void add_object(const std::string &name, const Boxed_Value &obj) const
{
auto &stack = get_stack_data();
validate_object_name(name);

auto &scope = stack.back();
if (scope.find(name) != scope.end())
if (!get_stack_data().back().insert(std::make_pair(name, obj)).second)
{
throw chaiscript::exception::name_conflict_error(name);
} else {
scope.insert(std::make_pair(name, obj));
}
}

Expand Down Expand Up @@ -798,7 +794,7 @@ namespace chaiscript
/// Dump object info to stdout
void dump_object(const Boxed_Value &o) const
{
std::cout << (o.is_const()?"const ":"") << type_name(o) << std::endl;
std::cout << (o.is_const()?"const ":"") << type_name(o) << '\n';
}

/// Dump type info to stdout
Expand Down Expand Up @@ -832,7 +828,7 @@ namespace chaiscript
}
}

std::cout << ") " << std::endl;
std::cout << ") \n";
}

/// Returns true if a call can be made that consists of the first parameter
Expand All @@ -852,28 +848,28 @@ namespace chaiscript
/// Dump all system info to stdout
void dump_system() const
{
std::cout << "Registered Types: " << std::endl;
std::cout << "Registered Types: \n";
std::vector<std::pair<std::string, Type_Info> > types = get_types();
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
itr != types.end();
++itr)
{
std::cout << itr->first << ": ";
std::cout << itr->second.bare_name();
std::cout << std::endl;
std::cout << '\n';
}

std::cout << std::endl;
std::cout << '\n';
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();

std::cout << "Functions: " << std::endl;
std::cout << "Functions: \n";
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
{
dump_function(*itr);
}
std::cout << std::endl;
std::cout << '\n';
}

/// return true if the Boxed_Value matches the registered type by name
Expand Down
16 changes: 8 additions & 8 deletions include/chaiscript/language/chaiscript_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ namespace chaiscript

ss << what();
if (call_stack.size() > 0) {
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")" << std::endl;
ss << std::endl << detail << std::endl;
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")\n";
ss << '\n' << detail << '\n';
ss << " " << fname(call_stack[0]) << " (" << startpos(call_stack[0]) << ") '" << pretty(call_stack[0]) << "'";
for (size_t j = 1; j < call_stack.size(); ++j) {
if (id(call_stack[j]) != chaiscript::AST_Node_Type::Block
&& id(call_stack[j]) != chaiscript::AST_Node_Type::File)
{
ss << std::endl;
ss << '\n';
ss << " from " << fname(call_stack[j]) << " (" << startpos(call_stack[j]) << ") '" << pretty(call_stack[j]) << "'";
}
}
}
ss << std::endl;
ss << '\n';
return ss.str();
}

Expand Down Expand Up @@ -264,13 +264,13 @@ namespace chaiscript
std::stringstream ss;
if (t_functions.size() == 1)
{
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << std::endl;
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << '\n';
} else {
ss << " " << t_functions.size() << " overloads available:" << std::endl;
ss << " " << t_functions.size() << " overloads available:\n";

for (const auto & t_function : t_functions)
{
ss << " " << format_types((t_function), t_dot_notation, t_ss) << std::endl;
ss << " " << format_types((t_function), t_dot_notation, t_ss) << '\n';
}

}
Expand Down Expand Up @@ -430,7 +430,7 @@ namespace chaiscript
std::ostringstream oss;

oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
<< this->text << " : " << this->start.line << ", " << this->start.column << std::endl;
<< this->text << " : " << this->start.line << ", " << this->start.column << '\n';

for (size_t j = 0; j < this->children.size(); ++j) {
oss << this->children[j]->to_string(t_prepend + " ");
Expand Down
4 changes: 2 additions & 2 deletions include/chaiscript/language/chaiscript_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,11 +735,11 @@ namespace chaiscript
{
try {
const auto name = elem + prefix + t_module_name + postfix;
// std::cerr << "trying location: " << name << std::endl;
// std::cerr << "trying location: " << name << '\n';
load_module(version_stripped_name, name);
return name;
} catch (const chaiscript::exception::load_module_error &e) {
// std::cerr << "error: " << e.what() << std::endl;
// std::cerr << "error: " << e.what() << '\n';
errors.push_back(e);
// Try next set
}
Expand Down
2 changes: 1 addition & 1 deletion include/chaiscript/language/chaiscript_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace chaiscript
/// Prints the parsed ast_nodes as a tree
/*
void debug_print(AST_NodePtr t, std::string prepend = "") {
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl;
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << '\n';
for (unsigned int j = 0; j < t->children.size(); ++j) {
debug_print(t->children[j], prepend + " ");
}
Expand Down
16 changes: 8 additions & 8 deletions samples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

void log(const std::string &msg)
{
std::cout << "[" << time(nullptr) << "] " << msg << std::endl;
std::cout << "[" << time(nullptr) << "] " << msg << '\n';
}

void log(const std::string &module, const std::string &msg)
{
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << std::endl;
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << '\n';
}

void bound_log(const std::string &msg)
Expand All @@ -28,12 +28,12 @@ void bound_log(const std::string &msg)

void hello_world(const chaiscript::Boxed_Value & /*o*/)
{
std::cout << "Hello World" << std::endl;
std::cout << "Hello World\n";
}

void hello_constructor(const chaiscript::Boxed_Value & /*o*/)
{
std::cout << "Hello Constructor" << std::endl;
std::cout << "Hello Constructor\n";
}


Expand Down Expand Up @@ -62,7 +62,7 @@ struct System

void take_shared_ptr(const std::shared_ptr<const std::string> &p)
{
std::cout << *p << std::endl;
std::cout << *p << '\n';
}

int main(int /*argc*/, char * /*argv*/[]) {
Expand Down Expand Up @@ -122,17 +122,17 @@ int main(int /*argc*/, char * /*argv*/[]) {
//the templated version of eval:
int i = chai.eval<int>("5+5");

std::cout << "5+5: " << i << std::endl;
std::cout << "5+5: " << i << '\n';

//Add a new variable
chai("var scripti = 15");

//We can even get a handle to the variables in the system
int &scripti = chai.eval<int &>("scripti");

std::cout << "scripti: " << scripti << std::endl;
std::cout << "scripti: " << scripti << '\n';
scripti *= 2;
std::cout << "scripti (updated): " << scripti << std::endl;
std::cout << "scripti (updated): " << scripti << '\n';
chai("print(\"Scripti from chai: \" + to_string(scripti))");

//To do: Add examples of handling Boxed_Values directly when needed
Expand Down
4 changes: 4 additions & 0 deletions samples/fun_call_performance.chai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
for (var i = 0; i < 1000; ++i) {
puts(helloWorld("Bob12345"))
}

Loading

0 comments on commit 019ea57

Please sign in to comment.