Skip to content

Commit 652ebee

Browse files
authored
Merge pull request #80 from ReflectCxx/temp_dev
- Introduced rtl::LambdaFunction - tried replacing std::function with custom - exact same overhead, removed 'LambdaFunction' - minor changes.
2 parents c1f6fd5 + ae64053 commit 652ebee

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

RTLBenchmarkApp/src/BenchMark.cpp

Lines changed: 11 additions & 11 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
{
@@ -34,9 +35,9 @@ namespace rtl_bench
3435

3536
void BenchMark::autoLambdaCall_noReturn(benchmark::State& state)
3637
{
37-
auto sendMsg = [](const str_type& pMsg) {
38+
static auto sendMsg = [](const str_type& pMsg) {
3839
sendMessage(pMsg);
39-
};
40+
};
4041

4142
for (auto _ : state)
4243
{
@@ -50,7 +51,7 @@ namespace rtl_bench
5051
{
5152
static std::function sendMsg = [](const str_type& pMsg) {
5253
sendMessage(pMsg);
53-
};
54+
};
5455

5556
for (auto _ : state)
5657
{
@@ -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
{
@@ -91,14 +92,13 @@ namespace rtl_bench
9192
{
9293
static std::function getMsg = [](const str_type& pMsg) {
9394
return getMessage(pMsg);
94-
};
95+
};
9596

9697
for (auto _ : state)
9798
{
9899
benchmark::DoNotOptimize(getMsg(g_longStr));
99100
}
100101
}
101-
102102
}
103103

104104

@@ -171,7 +171,7 @@ namespace rtl_bench
171171
{
172172
static rtl::Record rNode = cxx_mirror().getRecord("Node").value();
173173
static rtl::Method getMsg = rNode.getMethod("getMessage").value();
174-
static rtl::RObject robj = rNode.create<rtl::alloc::Stack>().rObject;
174+
static rtl::RObject robj = rNode.create<rtl::alloc::Heap>().rObject;
175175
static auto _ = []() {
176176
if (getMsg.bind<str_type>(robj).call(g_longStr).err == rtl::error::None) {
177177
std::cout << "[rtl:3] call success.\n";
@@ -187,4 +187,4 @@ namespace rtl_bench
187187
benchmark::DoNotOptimize(getMsg.bind<str_type>(robj).call(g_longStr));
188188
}
189189
}
190-
}
190+
}

RTLBenchmarkApp/src/BenchMark.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include "RTLibInterface.h"
66

7-
#include <streambuf>
8-
97
#if defined(_MSC_VER)
108
# define NOINLINE __declspec(noinline)
119
#elif defined(__GNUC__)
@@ -14,30 +12,44 @@
1412
# define NOINLINE
1513
#endif
1614

17-
using str_type = std::string; //*/ std::string_view;
15+
using str_type = /*std::string; //*/ std::string_view;
1816

1917
namespace rtl_bench
2018
{
2119
static std::optional<std::string> g_msg;
2220

2321
NOINLINE static void sendMessage(str_type pMsg) {
22+
std::string result = std::string(pMsg) + std::string(pMsg);
23+
result = result + result;
24+
result = result + result;
2425
g_msg = pMsg;
2526
}
2627

2728
NOINLINE static str_type getMessage(str_type pMsg) {
29+
std::string result = std::string(pMsg) + std::string(pMsg);
30+
result = result + result;
31+
result = result + result;
2832
g_msg = pMsg;
29-
return str_type(pMsg);
33+
return str_type(g_msg->c_str());
3034
}
3135

3236
struct Node
3337
{
3438
NOINLINE void sendMessage(str_type pMsg) {
39+
std::string result = std::string(pMsg) + std::string(pMsg);
40+
result = result + result;
41+
result = result + result;
42+
g_msg = pMsg;
3543
g_msg = pMsg;
3644
}
3745

38-
NOINLINE str_type getMessage(str_type pMsg) {
46+
NOINLINE str_type getMessage(str_type pMsg)
47+
{
48+
std::string result = std::string(pMsg) + std::string(pMsg);
49+
result = result + result;
50+
result = result + result;
3951
g_msg = pMsg;
40-
return str_type(pMsg);
52+
return str_type(g_msg->c_str());
4153
}
4254
};
4355

RTLBenchmarkApp/src/main.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
#include "BenchMark.h"
66

7-
// ------------------------------------------------------------
8-
// Register benchmarks
9-
// ------------------------------------------------------------
107

118
BENCHMARK(rtl_bench::BenchMark::directCall_noReturn);
129
BENCHMARK(rtl_bench::BenchMark::autoLambdaCall_noReturn);
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 _retT, class... _signature>
8+
struct LambdaFunction
9+
{
10+
using Invoker = _retT(*)(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) -> _retT {
30+
31+
auto h = static_cast<Holder*>(stor);
32+
return (h->m_functor)(params...);
33+
};
34+
}
35+
36+
_retT operator()(_signature&... params)
37+
{
38+
return m_invoker(m_storage, params...);
39+
}
40+
};
41+
}

0 commit comments

Comments
 (0)