diff --git a/RTLBenchmarkApp/src/BenchMark.cpp b/RTLBenchmarkApp/src/BenchMark.cpp index 909419b5..a18759e0 100644 --- a/RTLBenchmarkApp/src/BenchMark.cpp +++ b/RTLBenchmarkApp/src/BenchMark.cpp @@ -6,6 +6,8 @@ #include "BenchMark.h" +#include "LambdaFunction.h" + namespace { @@ -14,11 +16,10 @@ namespace { "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure" "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except" "eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id"; - - // Pre-created string to isolate call overhead - static const std::string g_longStr(LONG_STR); } +// Pre-created string to isolate call overhead +static const std::string g_longStr(LONG_STR); namespace rtl_bench { @@ -34,9 +35,9 @@ namespace rtl_bench void BenchMark::autoLambdaCall_noReturn(benchmark::State& state) { - auto sendMsg = [](const str_type& pMsg) { + static auto sendMsg = [](const str_type& pMsg) { sendMessage(pMsg); - }; + }; for (auto _ : state) { @@ -50,7 +51,7 @@ namespace rtl_bench { static std::function sendMsg = [](const str_type& pMsg) { sendMessage(pMsg); - }; + }; for (auto _ : state) { @@ -78,7 +79,7 @@ namespace rtl_bench { auto getMsg = [](const str_type& pMsg) { return getMessage(pMsg); - }; + }; for (auto _ : state) { @@ -91,14 +92,13 @@ namespace rtl_bench { static std::function getMsg = [](const str_type& pMsg) { return getMessage(pMsg); - }; + }; for (auto _ : state) { benchmark::DoNotOptimize(getMsg(g_longStr)); } } - } @@ -171,7 +171,7 @@ namespace rtl_bench { static rtl::Record rNode = cxx_mirror().getRecord("Node").value(); static rtl::Method getMsg = rNode.getMethod("getMessage").value(); - static rtl::RObject robj = rNode.create().rObject; + static rtl::RObject robj = rNode.create().rObject; static auto _ = []() { if (getMsg.bind(robj).call(g_longStr).err == rtl::error::None) { std::cout << "[rtl:3] call success.\n"; @@ -187,4 +187,4 @@ namespace rtl_bench benchmark::DoNotOptimize(getMsg.bind(robj).call(g_longStr)); } } -} +} \ No newline at end of file diff --git a/RTLBenchmarkApp/src/BenchMark.h b/RTLBenchmarkApp/src/BenchMark.h index d485f971..acb9689c 100644 --- a/RTLBenchmarkApp/src/BenchMark.h +++ b/RTLBenchmarkApp/src/BenchMark.h @@ -4,8 +4,6 @@ #include "RTLibInterface.h" -#include - #if defined(_MSC_VER) # define NOINLINE __declspec(noinline) #elif defined(__GNUC__) @@ -14,30 +12,44 @@ # define NOINLINE #endif -using str_type = std::string; //*/ std::string_view; +using str_type = /*std::string; //*/ std::string_view; namespace rtl_bench { static std::optional g_msg; NOINLINE static void sendMessage(str_type pMsg) { + std::string result = std::string(pMsg) + std::string(pMsg); + result = result + result; + result = result + result; g_msg = pMsg; } NOINLINE static str_type getMessage(str_type pMsg) { + std::string result = std::string(pMsg) + std::string(pMsg); + result = result + result; + result = result + result; g_msg = pMsg; - return str_type(pMsg); + return str_type(g_msg->c_str()); } struct Node { NOINLINE void sendMessage(str_type pMsg) { + std::string result = std::string(pMsg) + std::string(pMsg); + result = result + result; + result = result + result; + g_msg = pMsg; g_msg = pMsg; } - NOINLINE str_type getMessage(str_type pMsg) { + NOINLINE str_type getMessage(str_type pMsg) + { + std::string result = std::string(pMsg) + std::string(pMsg); + result = result + result; + result = result + result; g_msg = pMsg; - return str_type(pMsg); + return str_type(g_msg->c_str()); } }; diff --git a/RTLBenchmarkApp/src/main.cpp b/RTLBenchmarkApp/src/main.cpp index a7eb54ec..673364f8 100644 --- a/RTLBenchmarkApp/src/main.cpp +++ b/RTLBenchmarkApp/src/main.cpp @@ -4,9 +4,6 @@ #include "BenchMark.h" -// ------------------------------------------------------------ -// Register benchmarks -// ------------------------------------------------------------ BENCHMARK(rtl_bench::BenchMark::directCall_noReturn); BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_noReturn); diff --git a/ReflectionTemplateLib/detail/inc/LambdaFunction.h b/ReflectionTemplateLib/detail/inc/LambdaFunction.h new file mode 100644 index 00000000..14413017 --- /dev/null +++ b/ReflectionTemplateLib/detail/inc/LambdaFunction.h @@ -0,0 +1,41 @@ +#pragma once + +#include "RObjectBuilder.hpp" + +namespace rtl::detail +{ + template + struct LambdaFunction + { + using Invoker = _retT(*)(void* , _signature&...); + + Invoker m_invoker = nullptr; + void* m_storage = nullptr; + + template + void init(_returnType(*pFunctor)(_signature...)) + { + struct Holder { + + using Functor = decltype(pFunctor); + Functor m_functor; + + Holder(Functor pFptr) : m_functor(pFptr) { } + }; + + static auto holder = Holder{ pFunctor }; + m_storage = &holder; + + m_invoker = +[](void* stor, _signature&... params) -> _retT { + + auto h = static_cast(stor); + return (h->m_functor)(params...); + }; + } + + _retT operator()(_signature&... params) + { + return m_invoker(m_storage, params...); + } + }; +} \ No newline at end of file