Skip to content

Commit 1e9a370

Browse files
committed
benchmark setup refined.
1 parent 7ea49bb commit 1e9a370

24 files changed

+156
-132
lines changed

RTLBenchmarkApp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_executable(${CXX_EXE_NAME}
5050
src/BenchMark.cpp
5151
src/StandardCall.h
5252
src/StandardCall.cpp
53+
src/StdFunction.cpp
5354
src/ReflectedCall.h
5455
src/ReflectedCall.cpp
5556
)

RTLBenchmarkApp/src/BenchMark.cpp

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,74 @@
11

22

33
#include <optional>
4-
54
#include <iostream>
5+
#include <functional>
66

77
#include "BenchMark.h"
8+
#include "RTLibInterface.h"
89

9-
extern std::size_t g_work_load;
10-
extern std::optional<std::string> g_work_done;
1110

12-
namespace
11+
namespace bm
1312
{
14-
NOINLINE static std::string perform_work(bm::argStr_t& pMsg)
15-
{
16-
auto workStr = std::string();
17-
for(int i = 0; i < g_work_load; ++i)
18-
{
19-
workStr += pMsg;
20-
}
21-
return workStr;
22-
}
13+
std::size_t g_work_load = 0;
14+
15+
std::optional<std::string> g_work_done = std::string();
16+
17+
extern std::string perform_work(const argStr_t& pMsg);
2318
}
2419

2520

2621
namespace bm
2722
{
28-
NOINLINE void sendMessage(argStr_t pMsg)
23+
void sendMessage(argStr_t pMsg)
2924
{
30-
volatile auto* p = &pMsg;
31-
static_cast<void>(p);
32-
33-
g_work_done = perform_work(pMsg);
25+
if(g_work_load){
26+
g_work_done = perform_work(pMsg);
27+
}
3428
}
3529

36-
NOINLINE void Node::sendMessage(argStr_t pMsg)
30+
void Node::sendMessage(argStr_t pMsg)
3731
{
38-
volatile auto* p = &pMsg;
39-
static_cast<void>(p);
40-
41-
g_work_done = perform_work(pMsg);
32+
if(g_work_load){
33+
g_work_done = perform_work(pMsg);
34+
}
4235
}
4336

44-
NOINLINE retStr_t getMessage(argStr_t pMsg)
37+
retStr_t getMessage(argStr_t pMsg)
4538
{
46-
volatile auto* p = &pMsg;
47-
static_cast<void>(p);
39+
if(g_work_load){
40+
g_work_done = perform_work(pMsg);
41+
}
42+
return retStr_t(g_work_done->c_str());
43+
}
4844

49-
g_work_done = perform_work(pMsg);
50-
return bm::retStr_t(g_work_done->c_str());
45+
retStr_t Node::getMessage(argStr_t pMsg)
46+
{
47+
if(g_work_load){
48+
g_work_done = perform_work(pMsg);
49+
}
50+
return retStr_t(g_work_done->c_str());
5151
}
52+
}
5253

53-
NOINLINE retStr_t Node::getMessage(argStr_t pMsg)
54+
55+
namespace cxx
56+
{
57+
const rtl::CxxMirror& mirror()
5458
{
55-
volatile auto* p = &pMsg;
56-
static_cast<void>(p);
59+
static auto cxx_mirror = rtl::CxxMirror({
60+
61+
rtl::type().function("getMessage").build(bm::getMessage),
62+
63+
rtl::type().function("sendMessage").build(bm::sendMessage),
64+
65+
rtl::type().record<bm::Node>("Node").build(),
66+
67+
rtl::type().member<bm::Node>().method("sendMessage").build(&bm::Node::sendMessage),
68+
69+
rtl::type().member<bm::Node>().method("getMessage").build(&bm::Node::getMessage)
70+
});
5771

58-
g_work_done = perform_work(pMsg);
59-
return bm::retStr_t(g_work_done->c_str());
72+
return cxx_mirror;
6073
}
6174
}

RTLBenchmarkApp/src/BenchMark.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
#pragma once
22

3-
#include <benchmark/benchmark.h>
4-
5-
#include <optional>
63
#include <string>
74
#include <string_view>
85

9-
#if defined(_MSC_VER)
10-
# define NOINLINE __declspec(noinline)
11-
#elif defined(__GNUC__)
12-
# define NOINLINE __attribute__((noinline))
13-
#else
14-
# define NOINLINE
15-
#endif
16-
176
namespace bm
187
{
198
using argStr_t = std::string_view;
209
using retStr_t = std::string_view;
21-
22-
static const char* LONG_STR = "Lorem ipsum"
23-
"dolor sit amet, consectetur adipiscing elit, sed do"
24-
"do aeiusmod tempor incididunt uth labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
25-
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"
26-
"dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except"
27-
"eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id"
28-
"Lorem ipsum dolor sit amet laboris nisi ut aliquip ex ea commodo";
2910

30-
static argStr_t g_longStr(LONG_STR);
31-
3211
struct Node
3312
{
3413
void sendMessage(argStr_t);
3514
retStr_t getMessage(argStr_t);
3615
};
16+
}
3717

38-
extern void sendMessage(argStr_t);
39-
extern retStr_t getMessage(argStr_t);
18+
19+
namespace bm
20+
{
21+
static argStr_t g_longStr = "Lorem ipsum"
22+
"dolor sit amet, consectetur adipiscing elit, sed do"
23+
"do aeiusmod tempor incididunt uth 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";
4028
}

RTLBenchmarkApp/src/ReflectedCall.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11

2+
#include <benchmark/benchmark.h>
3+
24
#include "ReflectedCall.h"
35
#include "RTLibInterface.h"
6+
#include "BenchMark.h"
47

5-
namespace
8+
namespace cxx
69
{
7-
static rtl::RObject nodeObj;
8-
static rtl::Method NodeGetMessage;
9-
static rtl::Method NodeSendMessage;
10-
static rtl::Function GetMessage;
11-
static rtl::Function SendMessage;
12-
13-
static auto _= []() {
14-
15-
rtl::CxxMirror m = rtl::CxxMirror({
16-
17-
rtl::type().function("getMessage").build(bm::getMessage),
18-
19-
rtl::type().function("sendMessage").build(bm::sendMessage),
20-
21-
rtl::type().record<bm::Node>("Node").build(),
22-
23-
rtl::type().member<bm::Node>().method("sendMessage").build(&bm::Node::sendMessage),
10+
extern const rtl::CxxMirror& mirror();
11+
}
2412

25-
rtl::type().member<bm::Node>().method("getMessage").build(&bm::Node::getMessage)
26-
});
13+
namespace
14+
{
15+
static rtl::Function GetMessage = cxx::mirror().getFunction("getMessage").value();
16+
static rtl::Function SendMessage = cxx::mirror().getFunction("sendMessage").value();
2717

28-
GetMessage = m.getFunction("getMessage").value();
29-
30-
SendMessage = m.getFunction("sendMessage").value();
18+
static rtl::Method NodeGetMessage = cxx::mirror().getRecord("Node")->getMethod("getMessage").value();
19+
static rtl::Method NodeSendMessage = cxx::mirror().getRecord("Node")->getMethod("sendMessage").value();
20+
21+
static rtl::RObject nodeObj = []()
22+
{
23+
auto Node = cxx::mirror().getRecord("Node").value();
3124

32-
rtl::Record Node = m.getRecord("Node").value();
25+
rtl::RObject robj = Node.create<rtl::alloc::Stack>().rObject;
3326

34-
nodeObj = std::move(Node.create<rtl::alloc::Stack>().rObject);
35-
36-
NodeGetMessage = Node.getMethod("getMessage").value();
37-
NodeSendMessage = Node.getMethod("sendMessage").value();
38-
return true;
27+
return std::move(robj);
3928
}();
4029
}
4130

RTLBenchmarkApp/src/ReflectedCall.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "BenchMark.h"
3+
#include <benchmark/benchmark.h>
44

55
struct ReflectedCall
66
{
Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
#include <iostream>
3+
#include <optional>
34
#include <functional>
4-
#include "StandardCall.h"
55

6-
extern std::optional<std::string> g_work_done;
6+
#include "BenchMark.h"
7+
#include "StandardCall.h"
78

89
namespace
910
{
@@ -19,39 +20,22 @@ namespace
1920
};
2021
}
2122

22-
namespace
23+
24+
namespace bm
2325
{
24-
static bm::Node node;
26+
extern void sendMessage(argStr_t);
2527

26-
static std::function SendMessage = [](bm::argStr_t& pMsg)
27-
{
28-
volatile auto* p = &pMsg;
29-
static_cast<void>(p);
30-
bm::sendMessage(pMsg);
31-
};
28+
extern retStr_t getMessage(argStr_t);
3229

33-
static std::function NodeSendMessage = [](bm::argStr_t& pMsg)
34-
{
35-
volatile auto* p = &pMsg;
36-
static_cast<void>(p);
37-
node.sendMessage(pMsg);
38-
};
30+
extern std::optional<std::string> g_work_done;
3931

40-
static std::function GetMessage = [](bm::argStr_t& pMsg)
41-
{
42-
auto retMsg = bm::getMessage(pMsg);
43-
volatile auto* p = &retMsg;
44-
static_cast<void>(p);
45-
return retMsg;
46-
};
32+
extern std::function<void(argStr_t&)> SendMessage;
4733

48-
static std::function NodeGetMessage = [](bm::argStr_t& pMsg)
49-
{
50-
auto retMsg = node.getMessage(pMsg);
51-
volatile auto* p = &retMsg;
52-
static_cast<void>(p);
53-
return retMsg;
54-
};
34+
extern std::function<void(argStr_t&)> NodeSendMessage;
35+
36+
extern std::function<retStr_t(argStr_t&)> GetMessage;
37+
38+
extern std::function<retStr_t(argStr_t&)> NodeGetMessage;
5539
}
5640

5741

@@ -60,7 +44,7 @@ void DirectCall::noReturn(benchmark::State& state)
6044
for (auto _: state)
6145
{
6246
bm::sendMessage(bm::g_longStr);
63-
benchmark::DoNotOptimize(g_work_done->c_str());
47+
benchmark::DoNotOptimize(bm::g_work_done->c_str());
6448
}
6549
}
6650

@@ -75,14 +59,13 @@ void DirectCall::withReturn(benchmark::State& state)
7559
}
7660

7761

78-
7962
void StdFuncCall::noReturn(benchmark::State& state)
8063
{
8164
static auto _=_new_line();
8265
for (auto _: state)
8366
{
84-
SendMessage(bm::g_longStr);
85-
benchmark::DoNotOptimize(g_work_done->c_str());
67+
bm::SendMessage(bm::g_longStr);
68+
benchmark::DoNotOptimize(bm::g_work_done->c_str());
8669
}
8770
}
8871

@@ -92,8 +75,8 @@ void StdFuncMethodCall::noReturn(benchmark::State& state)
9275
static auto _=_new_line();
9376
for (auto _: state)
9477
{
95-
NodeSendMessage(bm::g_longStr);
96-
benchmark::DoNotOptimize(g_work_done->c_str());
78+
bm::NodeSendMessage(bm::g_longStr);
79+
benchmark::DoNotOptimize(bm::g_work_done->c_str());
9780
}
9881
}
9982

@@ -103,7 +86,7 @@ void StdFuncCall::withReturn(benchmark::State& state)
10386
static auto _=_new_line();
10487
for (auto _: state)
10588
{
106-
benchmark::DoNotOptimize(GetMessage(bm::g_longStr));
89+
benchmark::DoNotOptimize(bm::GetMessage(bm::g_longStr));
10790
}
10891
}
10992

@@ -113,6 +96,6 @@ void StdFuncMethodCall::withReturn(benchmark::State& state)
11396
static auto _=_new_line();
11497
for (auto _: state)
11598
{
116-
benchmark::DoNotOptimize(NodeGetMessage(bm::g_longStr));
99+
benchmark::DoNotOptimize(bm::NodeGetMessage(bm::g_longStr));
117100
}
118101
}

RTLBenchmarkApp/src/StandardCall.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "BenchMark.h"
3+
#include <benchmark/benchmark.h>
44

55
struct DirectCall
66
{

0 commit comments

Comments
 (0)