Skip to content

Commit 9d3e403

Browse files
committed
furthur refinement, bench-mark refactored.
1 parent 9eba414 commit 9d3e403

File tree

15 files changed

+369
-301
lines changed

15 files changed

+369
-301
lines changed

RTLBenchmarkApp/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ set(CXX_EXE_NAME RTLBenchmarkApp)
4646
# ===============================
4747
add_executable(${CXX_EXE_NAME}
4848
src/main.cpp
49-
src/BenchMark.cpp # <-- added
50-
src/BenchMark.h # <-- optional (for IDE visibility)
49+
src/BenchMark.h
50+
src/BenchMark.cpp
51+
src/StandardCall.h
52+
src/StandardCall.cpp
53+
src/ReflectedCall.h
54+
src/ReflectedCall.cpp
5155
)
5256

5357

RTLBenchmarkApp/src/BenchMark.cpp

Lines changed: 25 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -6,182 +6,53 @@
66

77
#include "BenchMark.h"
88

9-
namespace {
9+
extern std::size_t g_work_load_scale;
1010

11-
static const char* LONG_STR = "Lorem ipsum";
12-
// dolor sit amet, consectetur adipiscing elit, sed do"
13-
// "do aeiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
14-
// "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"
15-
// "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except"
16-
// "eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id"
17-
// "Lorem ipsum dolor sit amet laboris nisi ut aliquip ex ea commodo";
18-
}
19-
20-
// Pre-created string to isolate call overhead
21-
argStr_t g_longStr(LONG_STR);
22-
23-
extern decltype(&rtl_bench::getMessage) getMessagePtr;
24-
extern decltype(&rtl_bench::sendMessage) sendMessagePtr;
25-
extern decltype(&rtl_bench::Node::getMessage) getMessageNodePtr;
26-
extern decltype(&rtl_bench::Node::sendMessage) sendMessageNodePtr;
27-
28-
namespace rtl_bench
11+
namespace
2912
{
30-
static Node* node = new Node();
31-
32-
void BenchMark::directCall_noReturn(benchmark::State& state)
33-
{
34-
for (auto _ : state)
35-
{
36-
sendMessagePtr(g_longStr);
37-
benchmark::DoNotOptimize(g_msg);
38-
}
39-
}
40-
41-
42-
void BenchMark::stdFunctionCall_noReturn(benchmark::State& state)
43-
{
44-
static std::function sendMsg = [](argStr_t& pMsg) {
45-
sendMessagePtr(pMsg);
46-
};
47-
48-
for (auto _ : state)
49-
{
50-
sendMsg(g_longStr);
51-
benchmark::DoNotOptimize(g_msg);
52-
}
53-
}
54-
55-
void BenchMark::stdFunctionMethodCall_noReturn(benchmark::State& state)
13+
static void work_load(bm::argStr_t& pMsg)
5614
{
57-
static std::function sendMsg = [=](argStr_t& pMsg) {
58-
(node->*sendMessageNodePtr)(pMsg);
59-
};
60-
61-
for (auto _ : state)
15+
bm::g_msg = std::string();
16+
for(int i = 0; i < g_work_load_scale; ++i)
6217
{
63-
sendMsg(g_longStr);
64-
benchmark::DoNotOptimize(g_msg);
65-
}
66-
}
67-
68-
69-
void BenchMark::directCall_withReturn(benchmark::State& state)
70-
{
71-
static auto _ = []() {
72-
std::cout << "--------------------------------------------------"
73-
"-----------------------------------------------" << std::endl;
74-
return 0;
75-
}();
76-
77-
for (auto _ : state)
78-
{
79-
benchmark::DoNotOptimize(getMessage(g_longStr));
80-
}
81-
}
82-
83-
84-
void BenchMark::stdFunctionCall_withReturn(benchmark::State& state)
85-
{
86-
static std::function getMsg = [](argStr_t& pMsg) {
87-
return getMessagePtr(pMsg);
88-
};
89-
90-
for (auto _ : state)
91-
{
92-
benchmark::DoNotOptimize(getMsg(g_longStr));
93-
}
94-
}
95-
96-
97-
void BenchMark::stdFunctionMethodCall_withReturn(benchmark::State& state)
98-
{
99-
static std::function getMsg = [=](argStr_t& pMsg) {
100-
return (node->*getMessageNodePtr)(pMsg);
101-
};
102-
103-
for (auto _ : state)
104-
{
105-
benchmark::DoNotOptimize(getMsg(g_longStr));
18+
bm::g_msg->append(pMsg);
10619
}
10720
}
10821
}
10922

11023

111-
namespace rtl_bench
24+
namespace bm
11225
{
113-
void BenchMark::reflectedCall_noReturn(benchmark::State& state)
26+
NOINLINE void sendMessage(argStr_t pMsg)
11427
{
115-
static rtl::Function sendMsg = cxx_mirror().getFunction("sendMessage").value();
116-
static auto _ = []() {
117-
auto err = sendMsg.bind().call(g_longStr).err;
118-
if (err != rtl::error::None) {
119-
std::cout << "[rtl:0] err: "<< rtl::to_string(err)<<"\n";
120-
}
121-
return 0;
122-
}();
123-
124-
for (auto _ : state)
125-
{
126-
benchmark::DoNotOptimize(sendMsg.bind().call(g_longStr));
127-
}
28+
volatile auto* p = &pMsg;
29+
static_cast<void>(p);
30+
work_load(pMsg);
12831
}
12932

13033

131-
void BenchMark::reflectedMethodCall_noReturn(benchmark::State& state)
34+
NOINLINE retStr_t getMessage(argStr_t pMsg)
13235
{
133-
static rtl::Record rNode = cxx_mirror().getRecord("Node").value();
134-
static rtl::Method sendMsg = rNode.getMethod("sendMessage").value();
135-
static rtl::RObject robj = rNode.create<rtl::alloc::Heap>().rObject;
136-
static auto _ = []() {
137-
auto err = sendMsg.bind(robj).call(g_longStr).err;
138-
if (err != rtl::error::None) {
139-
std::cout << "[rtl:1] err: " << rtl::to_string(err) << "\n";
140-
}
141-
return 0;
142-
}();
143-
144-
for (auto _ : state)
145-
{
146-
benchmark::DoNotOptimize(sendMsg.bind(robj).call(g_longStr));
147-
}
36+
volatile auto* p = &pMsg;
37+
static_cast<void>(p);
38+
work_load(pMsg);
39+
return bm::retStr_t(bm::g_msg->c_str());
14840
}
14941

15042

151-
void BenchMark::reflectedCall_withReturn(benchmark::State& state)
43+
NOINLINE void Node::sendMessage(argStr_t pMsg)
15244
{
153-
static rtl::Function getMsg = cxx_mirror().getFunction("getMessage").value();
154-
static auto _ = []() {
155-
auto err = getMsg.bind().call(g_longStr).err;
156-
if (err != rtl::error::None) {
157-
std::cout << "[rtl:2] err: " << rtl::to_string(err) << "\n";
158-
}
159-
return 0;
160-
}();
161-
162-
for (auto _ : state)
163-
{
164-
benchmark::DoNotOptimize(getMsg.bind().call(g_longStr));
165-
}
45+
volatile auto* p = &pMsg;
46+
static_cast<void>(p);
47+
work_load(pMsg);
16648
}
16749

16850

169-
void BenchMark::reflectedMethodCall_withReturn(benchmark::State& state)
51+
NOINLINE retStr_t Node::getMessage(argStr_t pMsg)
17052
{
171-
static rtl::Record rNode = cxx_mirror().getRecord("Node").value();
172-
static rtl::Method getMsg = rNode.getMethod("getMessage").value();
173-
static rtl::RObject robj = rNode.create<rtl::alloc::Heap>().rObject;
174-
static auto _ = []() {
175-
auto err = getMsg.bind(robj).call(g_longStr).err;
176-
if (err != rtl::error::None) {
177-
std::cout << "[rtl:3] err: " << rtl::to_string(err) << "\n";
178-
}
179-
return 0;
180-
}();
181-
182-
for (auto _ : state)
183-
{
184-
benchmark::DoNotOptimize(getMsg.bind(robj).call(g_longStr));
185-
}
53+
volatile auto* p = &pMsg;
54+
static_cast<void>(p);
55+
work_load(pMsg);
56+
return bm::retStr_t(bm::g_msg->c_str());
18657
}
18758
}

RTLBenchmarkApp/src/BenchMark.h

Lines changed: 21 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <benchmark/benchmark.h>
44

5-
#include <vector>
6-
7-
#include "RTLibInterface.h"
5+
#include <optional>
6+
#include <string>
7+
#include <string_view>
88

99
#if defined(_MSC_VER)
1010
# define NOINLINE __declspec(noinline)
@@ -14,79 +14,28 @@
1414
# define NOINLINE
1515
#endif
1616

17-
using argStr_t = std::string_view;
18-
using retStr_t = std::string_view;
19-
20-
#define WORK_LOAD(S) (std::string(S))
21-
22-
namespace rtl_bench
17+
namespace bm
2318
{
24-
static std::optional<std::string> g_msg;
25-
26-
NOINLINE static void sendMessage(argStr_t pMsg)
27-
{
28-
g_msg = WORK_LOAD(pMsg);
29-
}
30-
31-
NOINLINE static retStr_t getMessage(argStr_t pMsg)
32-
{
33-
g_msg = WORK_LOAD(pMsg);
34-
return retStr_t(g_msg->c_str());
35-
}
19+
using argStr_t = std::string_view;
20+
using retStr_t = std::string_view;
21+
22+
static const char* LONG_STR = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
23+
"do aeiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
24+
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"
25+
"dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except"
26+
"eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id"
27+
"Lorem ipsum dolor sit amet laboris nisi ut aliquip ex ea commodo";
28+
29+
static argStr_t g_longStr(LONG_STR);
3630

3731
struct Node
3832
{
39-
NOINLINE void sendMessage(argStr_t pMsg)
40-
{
41-
g_msg = WORK_LOAD(pMsg);
42-
}
43-
44-
NOINLINE retStr_t getMessage(argStr_t pMsg)
45-
{
46-
g_msg = WORK_LOAD(pMsg);
47-
return retStr_t(g_msg->c_str());
48-
}
33+
void sendMessage(argStr_t);
34+
retStr_t getMessage(argStr_t);
4935
};
5036

37+
extern void sendMessage(argStr_t);
38+
extern retStr_t getMessage(argStr_t);
5139

52-
static const rtl::CxxMirror& cxx_mirror()
53-
{
54-
static auto m = rtl::CxxMirror({
55-
56-
rtl::type().record<Node>("Node").build(),
57-
58-
rtl::type().function("sendMessage").build(sendMessage),
59-
60-
rtl::type().member<Node>().method("sendMessage").build(&Node::sendMessage),
61-
62-
rtl::type().function("getMessage").build(getMessage),
63-
64-
rtl::type().member<Node>().method("getMessage").build(&Node::getMessage)
65-
});
66-
return m;
67-
}
68-
69-
70-
struct BenchMark
71-
{
72-
static void directCall_noReturn(benchmark::State& state);
73-
74-
static void stdFunctionCall_noReturn(benchmark::State& state);
75-
76-
static void reflectedCall_noReturn(benchmark::State& state);
77-
78-
static void stdFunctionMethodCall_noReturn(benchmark::State& state);
79-
80-
static void reflectedMethodCall_noReturn(benchmark::State& state);
81-
82-
static void directCall_withReturn(benchmark::State& state);
83-
84-
static void stdFunctionCall_withReturn(benchmark::State& state);
85-
86-
static void stdFunctionMethodCall_withReturn(benchmark::State& state);
87-
88-
static void reflectedCall_withReturn(benchmark::State& state);
89-
90-
static void reflectedMethodCall_withReturn(benchmark::State& state);
91-
};
92-
}
40+
static std::optional<std::string> g_msg;
41+
}

0 commit comments

Comments
 (0)