Skip to content

Commit 524adff

Browse files
committed
introducing rtl::LambdaFunction
1 parent e458d9e commit 524adff

File tree

5 files changed

+95
-31
lines changed

5 files changed

+95
-31
lines changed

RTLBenchmarkApp/src/BenchMark.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "BenchMark.h"
88

9+
#include "LambdaFunction.h"
10+
911

1012
namespace {
1113

@@ -14,11 +16,10 @@ namespace {
1416
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"
1517
"dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except"
1618
"eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id";
17-
18-
// Pre-created string to isolate call overhead
19-
static const std::string g_longStr(LONG_STR);
2019
}
2120

21+
// Pre-created string to isolate call overhead
22+
static const std::string g_longStr(LONG_STR);
2223

2324
namespace rtl_bench
2425
{
@@ -78,7 +79,7 @@ namespace rtl_bench
7879
{
7980
auto getMsg = [](const str_type& pMsg) {
8081
return getMessage(pMsg);
81-
};
82+
};
8283

8384
for (auto _ : state)
8485
{
@@ -98,29 +99,34 @@ namespace rtl_bench
9899
benchmark::DoNotOptimize(getMsg(g_longStr));
99100
}
100101
}
101-
102102
}
103103

104104

105105
namespace rtl_bench
106106
{
107107
void BenchMark::BM_FunctionCall(benchmark::State& state)
108108
{
109-
static std::function func = [](const str_type& pMsg) {
109+
static std::function getMsg = [](const str_type& pMsg) {
110110
return getMessage(pMsg);
111111
};
112-
113-
for (auto _ : state) {
114-
benchmark::DoNotOptimize(func(g_longStr));
112+
113+
for (auto _ : state)
114+
{
115+
benchmark::DoNotOptimize(getMsg(g_longStr));
115116
}
116117
}
117118

118-
void BenchMark::BM_AnyCast(benchmark::State& state)
119+
void BenchMark::BM_LambdaFunc(benchmark::State& state)
119120
{
120-
std::any a = getMessage;
121+
static rtl::detail::LambdaFunction<const std::string> obj;
122+
123+
static auto _ = []() {
124+
obj.init(getMessage);
125+
return 0;
126+
}();
127+
121128
for (auto _ : state) {
122-
auto anyfunc = std::any_cast<decltype(&getMessage)>(a);
123-
benchmark::DoNotOptimize(anyfunc(g_longStr));
129+
benchmark::DoNotOptimize(obj(g_longStr));
124130
}
125131
}
126132
}
@@ -195,7 +201,7 @@ namespace rtl_bench
195201
{
196202
static rtl::Record rNode = cxx_mirror().getRecord("Node").value();
197203
static rtl::Method getMsg = rNode.getMethod("getMessage").value();
198-
static rtl::RObject robj = rNode.create<rtl::alloc::Stack>().rObject;
204+
static rtl::RObject robj = rNode.create<rtl::alloc::Heap>().rObject;
199205
static auto _ = []() {
200206
if (getMsg.bind<str_type>(robj).call(g_longStr).err == rtl::error::None) {
201207
std::cout << "[rtl:3] call success.\n";
@@ -211,4 +217,4 @@ namespace rtl_bench
211217
benchmark::DoNotOptimize(getMsg.bind<str_type>(robj).call(g_longStr));
212218
}
213219
}
214-
}
220+
}

RTLBenchmarkApp/src/BenchMark.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,37 @@ namespace rtl_bench
2121
static std::optional<std::string> g_msg;
2222

2323
NOINLINE static void sendMessage(str_type pMsg) {
24+
std::string result = pMsg + pMsg;
25+
result = result + result;
26+
result = result + result;
2427
g_msg = pMsg;
2528
}
2629

2730
NOINLINE static str_type getMessage(str_type pMsg) {
31+
std::string result = pMsg + pMsg;
32+
result = result + result;
33+
result = result + result;
2834
g_msg = pMsg;
2935
return str_type(pMsg);
3036
}
3137

3238
struct Node
3339
{
3440
NOINLINE void sendMessage(str_type pMsg) {
41+
std::string result = pMsg + pMsg;
42+
result = result + result;
43+
result = result + result;
44+
g_msg = pMsg;
3545
g_msg = pMsg;
3646
}
3747

38-
NOINLINE str_type getMessage(str_type pMsg) {
48+
NOINLINE str_type getMessage(str_type pMsg)
49+
{
50+
std::string result = pMsg + pMsg;
51+
result = result + result;
52+
result = result + result;
3953
g_msg = pMsg;
40-
return str_type(pMsg);
54+
return pMsg;
4155
}
4256
};
4357

@@ -83,6 +97,7 @@ namespace rtl_bench
8397
static void reflectedMethodCall_withReturn(benchmark::State& state);
8498

8599
static void BM_FunctionCall(benchmark::State& state);
86-
static void BM_AnyCast(benchmark::State& state);
100+
101+
static void BM_LambdaFunc(benchmark::State& state);
87102
};
88103
}

RTLBenchmarkApp/src/main.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44

55
#include "BenchMark.h"
66

7-
BENCHMARK(rtl_bench::BenchMark::BM_FunctionCall);
8-
BENCHMARK(rtl_bench::BenchMark::BM_AnyCast);
97

10-
//BENCHMARK(rtl_bench::BenchMark::directCall_noReturn);
11-
//BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_noReturn);
12-
//BENCHMARK(rtl_bench::BenchMark::stdFunctionCall_noReturn);
13-
//BENCHMARK(rtl_bench::BenchMark::reflectedCall_noReturn);
14-
//BENCHMARK(rtl_bench::BenchMark::reflectedMethodCall_noReturn);
15-
//
16-
//BENCHMARK(rtl_bench::BenchMark::directCall_withReturn);
17-
//BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_withReturn);
18-
//BENCHMARK(rtl_bench::BenchMark::stdFunctionCall_withReturn);
19-
//BENCHMARK(rtl_bench::BenchMark::reflectedCall_withReturn);
20-
//BENCHMARK(rtl_bench::BenchMark::reflectedMethodCall_withReturn);
8+
BENCHMARK(rtl_bench::BenchMark::directCall_noReturn);
9+
BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_noReturn);
10+
BENCHMARK(rtl_bench::BenchMark::stdFunctionCall_noReturn);
11+
BENCHMARK(rtl_bench::BenchMark::reflectedCall_noReturn);
12+
BENCHMARK(rtl_bench::BenchMark::reflectedMethodCall_noReturn);
13+
14+
BENCHMARK(rtl_bench::BenchMark::directCall_withReturn);
15+
BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_withReturn);
16+
BENCHMARK(rtl_bench::BenchMark::stdFunctionCall_withReturn);
17+
BENCHMARK(rtl_bench::BenchMark::reflectedCall_withReturn);
18+
BENCHMARK(rtl_bench::BenchMark::reflectedMethodCall_withReturn);
19+
20+
BENCHMARK(rtl_bench::BenchMark::BM_LambdaFunc);
21+
BENCHMARK(rtl_bench::BenchMark::BM_FunctionCall);
2122

2223
BENCHMARK_MAIN();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include "RObjectBuilder.hpp"
4+
5+
namespace rtl::detail
6+
{
7+
template<class... _signature>
8+
struct LambdaFunction
9+
{
10+
using Invoker = std::string(*)(void* , _signature&...);
11+
12+
Invoker m_invoker = nullptr;
13+
void* m_storage = nullptr;
14+
15+
template<class _returnType>
16+
void init(_returnType(*pFunctor)(_signature...))
17+
{
18+
struct Holder {
19+
20+
using Functor = decltype(pFunctor);
21+
Functor m_functor;
22+
23+
Holder(Functor pFptr) : m_functor(pFptr) { }
24+
};
25+
26+
static auto holder = Holder{ pFunctor };
27+
m_storage = &holder;
28+
29+
m_invoker = +[](void* stor, _signature&... params) -> std::string {
30+
31+
auto h = static_cast<Holder*>(stor);
32+
return (h->m_functor)(params...);
33+
};
34+
}
35+
36+
std::string operator()(_signature&... params)
37+
{
38+
return m_invoker(m_storage, params...);
39+
}
40+
};
41+
}

ReflectionTemplateLib/detail/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(LOCAL_SOURCES
99

1010
SET(LOCAL_HEADERS
1111

12+
"${PROJECT_SOURCE_DIR}/detail/inc/LambdaFunction.h"
1213
"${PROJECT_SOURCE_DIR}/detail/inc/CallReflector.h"
1314
"${PROJECT_SOURCE_DIR}/detail/inc/CxxReflection.h"
1415
"${PROJECT_SOURCE_DIR}/detail/inc/FunctionCaller.h"

0 commit comments

Comments
 (0)