From 8c9f8c3341e1f1e860acbbe2a4dd8fccecfd260a Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 16 Jan 2016 02:30:49 -0500 Subject: [PATCH] improved benchmarking --- bench.cpp | 2 + bench_userdata.hpp | 1 - bench_usertype.hpp | 105 +++++++++++++++++++++++++++++++++++++++++++++ sol/proxy.hpp | 22 +++++----- sol/traits.hpp | 5 ++- sol/types.hpp | 16 ++++--- 6 files changed, 133 insertions(+), 18 deletions(-) delete mode 100644 bench_userdata.hpp create mode 100644 bench_usertype.hpp diff --git a/bench.cpp b/bench.cpp index cda10ee1..20a1c7b2 100644 --- a/bench.cpp +++ b/bench.cpp @@ -2,6 +2,7 @@ #include "bench_lua_function.hpp" #include "bench_get.hpp" #include "bench_set.hpp" +#include "bench_usertype.hpp" #include "nonius/nonius.h++" int main( int argc, char* argv[] ) { @@ -12,4 +13,5 @@ int main( int argc, char* argv[] ) { bench_cpp_function( "bench/", configurationname, platformname ); bench_get( "bench/", configurationname, platformname ); bench_set( "bench/", configurationname, platformname ); + bench_usertype( "bench/", configurationname, platformname ); } \ No newline at end of file diff --git a/bench_userdata.hpp b/bench_userdata.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/bench_userdata.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/bench_usertype.hpp b/bench_usertype.hpp new file mode 100644 index 00000000..26a8cb23 --- /dev/null +++ b/bench_usertype.hpp @@ -0,0 +1,105 @@ +#pragma once + +#include "sol.hpp" +#include "nonius/nonius.h++" + +struct woof { + int var; + + int func( int x ) { + return var + x; + } + + int operator + (int x) const { + return var + x; + } +}; + +static sol::state prepare_usertype_state( ) { + sol::state lua; + lua.new_usertype( "woof", + "var", &woof::var, + "func", &woof::func, + sol::meta_function::addition, &woof::operator+ ); + lua.script( R"( +r = woof:new() +)" ); + return lua; +} + +struct sol_usertype_special_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + auto measurement = [ & ] ( int run_index ) { + lua.script("x = r + 2"); + }; + meter.measure( measurement ); + } +}; + +struct sol_usertype_regular_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + auto measurement = [ & ] ( int run_index ) { + lua.script("x = r:func( 2 )"); + }; + meter.measure( measurement ); + } +}; + +struct sol_usertype_variable_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + auto measurement = [ & ] ( int run_index ) { + lua.script("x = r.var + 2"); + }; + meter.measure( measurement ); + } +}; + +struct c_usertype_special_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + lua_State* L = lua.lua_state( ); + auto measurement = [ & ] ( int run_index ) { + + }; + meter.measure( measurement ); + } +}; + +struct c_usertype_regular_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + lua_State* L = lua.lua_state( ); + auto measurement = [ & ] ( int run_index ) { + }; + meter.measure( measurement ); + } +}; + +struct c_usertype_variable_bench { + void operator () ( nonius::chronometer meter ) const { + sol::state lua = prepare_usertype_state( ); + lua_State* L = lua.lua_state( ); + auto measurement = [ & ] ( int run_index ) { + }; + meter.measure( measurement ); + } +}; + +void bench_usertype( const std::string& dir, std::string& configurationname, const std::string& platformname ) { + nonius::configuration cfg; + cfg.output_file = dir + "sol.usertype - " + configurationname + " " + platformname + ".html"; + cfg.title = "sol usertype (" + configurationname + " " + platformname + ")"; + cfg.samples = 100; + nonius::benchmark benchmarks [] = { + nonius::benchmark( "sol - usertype special", sol_usertype_special_bench( ) ), + nonius::benchmark( "sol - usertype regular", sol_usertype_regular_bench( ) ), + nonius::benchmark( "sol - usertype variable", sol_usertype_variable_bench( ) ), + //nonius::benchmark( "plain C - usertype special", c_usertype_special_bench( ) ), + //nonius::benchmark( "plain C - usertype regular", c_usertype_regular_bench( ) ), + //nonius::benchmark( "plain C - usertype variable", c_usertype_variable_bench( ) ), + }; + nonius::go( cfg, std::begin( benchmarks ), std::end( benchmarks ), nonius::html_reporter( ) ); +} diff --git a/sol/proxy.hpp b/sol/proxy.hpp index fd78b30d..5b98f90d 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -63,7 +63,7 @@ public: } template - T get() const { + decltype(auto) get() const { return tbl.template get( key ); } @@ -71,19 +71,19 @@ public: return get(); } - template, const char*>>, Not, char>>, Not, std::string>>, Not, std::initializer_list>>> = 0> - operator T () const { - return get(); + template>, is_lua_primitive> = 0> + operator T ( ) const { + return get( ); + } + + template>, Not>> = 0> + operator T& ( ) const { + return get( ); } template - stack::get_return_or call(Args&&... args) { - return tbl.template get(key)(types(), std::forward(args)...); - } - - template - function_result operator()(Args&&... args) { - return tbl.template get(key)(std::forward(args)...); + decltype(auto) call(Args&&... args) { + return get().call(std::forward(args)...); } }; diff --git a/sol/traits.hpp b/sol/traits.hpp index 1bdf99cb..af3899e6 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -87,7 +87,7 @@ template struct And : If, Bool> {}; template -struct Or : Bool {}; +struct Or : Bool {}; template struct Or : If, Or> {}; @@ -311,6 +311,9 @@ struct has_key_value_pair_impl { template struct has_key_value_pair : decltype(has_key_value_pair_impl::test(0)) {}; +template +using is_string_constructible = Or, const char*>, std::is_same, char>, std::is_same, std::string>, std::is_same, std::initializer_list>>; + template auto unwrapper(T&& item) -> decltype(std::forward(item)) { return std::forward(item); diff --git a/sol/types.hpp b/sol/types.hpp index cb21217e..ff8ffa65 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -32,9 +32,6 @@ const nil_t nil {}; inline bool operator==(nil_t, nil_t) { return true; } inline bool operator!=(nil_t, nil_t) { return false; } -struct void_type : types {}; // This is important because it allows myobject.call( Void, ... ) to work -const void_type Void {}; - template struct function_sig_t {}; using function_t = function_sig_t<>; @@ -146,10 +143,13 @@ template <> struct lua_type_of : std::integral_constant {}; template <> -struct lua_type_of : std::integral_constant {}; +struct lua_type_of : std::integral_constant { }; template <> -struct lua_type_of : std::integral_constant {}; +struct lua_type_of
: std::integral_constant { }; + +template <> +struct lua_type_of : std::integral_constant { }; template <> struct lua_type_of : std::integral_constant {}; @@ -160,6 +160,9 @@ struct lua_type_of : std::integral_constant struct lua_type_of : std::integral_constant {}; +template <> +struct lua_type_of : std::integral_constant {}; + template struct lua_type_of> : std::integral_constant{}; @@ -185,6 +188,9 @@ struct lua_type_of::value>::type> : s }; +template +struct is_lua_primitive : std::integral_constant>::value> { }; + } // sol #endif // SOL_TYPES_HPP