Skip to content

Commit 7bfc6c8

Browse files
committed
removed .at(), static-cache encouraging inlining, BM with functor-dref.
1 parent 3ee7a28 commit 7bfc6c8

File tree

13 files changed

+60
-65
lines changed

13 files changed

+60
-65
lines changed

CxxTestProps/src/Date.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ namespace nsdate
151151
vector<string> date;
152152
for (size_t i = 0; i < pDateStr.length(); i++)
153153
{
154-
if (pDateStr.at(i) == '/') {
154+
if (pDateStr[i] == '/') {
155155
date.push_back(strBuf);
156156
strBuf.clear();
157157
}
158158
else {
159-
strBuf.push_back(pDateStr.at(i));
159+
strBuf.push_back(pDateStr[i]);
160160
}
161161
}
162162
m_day = stoi(date[0]);
@@ -208,12 +208,12 @@ namespace nsdate
208208
vector<string> date;
209209
for (size_t i = 0; i < pDateStr.length(); i++)
210210
{
211-
if (pDateStr.at(i) == '/') {
211+
if (pDateStr[i] == '/') {
212212
date.push_back(strBuf);
213213
strBuf.clear();
214214
}
215215
else {
216-
strBuf.push_back(pDateStr.at(i));
216+
strBuf.push_back(pDateStr[i]);
217217
}
218218
}
219219
m_day = stoi(date[0]);

RTLBenchmarkApp/src/BenchMark.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@
99
namespace {
1010

1111
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";
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";
1818
}
1919

2020
// Pre-created string to isolate call overhead
21-
static argStr_t g_longStr(LONG_STR);
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;
2227

2328
namespace rtl_bench
2429
{
30+
static Node* node = new Node();
31+
2532
void BenchMark::directCall_noReturn(benchmark::State& state)
2633
{
2734
for (auto _ : state)
2835
{
29-
sendMessage(g_longStr);
36+
sendMessagePtr(g_longStr);
3037
benchmark::DoNotOptimize(g_msg);
3138
}
3239
}
@@ -35,7 +42,7 @@ namespace rtl_bench
3542
void BenchMark::stdFunctionCall_noReturn(benchmark::State& state)
3643
{
3744
static std::function sendMsg = [](argStr_t& pMsg) {
38-
sendMessage(pMsg);
45+
sendMessagePtr(pMsg);
3946
};
4047

4148
for (auto _ : state)
@@ -45,12 +52,10 @@ namespace rtl_bench
4552
}
4653
}
4754

48-
4955
void BenchMark::stdFunctionMethodCall_noReturn(benchmark::State& state)
5056
{
51-
Node* node = new Node();
5257
static std::function sendMsg = [=](argStr_t& pMsg) {
53-
node->sendMessage(pMsg);
58+
(node->*sendMessageNodePtr)(pMsg);
5459
};
5560

5661
for (auto _ : state)
@@ -79,10 +84,7 @@ namespace rtl_bench
7984
void BenchMark::stdFunctionCall_withReturn(benchmark::State& state)
8085
{
8186
static std::function getMsg = [](argStr_t& pMsg) {
82-
auto msgStr = getMessage(pMsg);
83-
volatile auto* p = &msgStr;
84-
static_cast<void>(p);
85-
return msgStr;
87+
return getMessagePtr(pMsg);
8688
};
8789

8890
for (auto _ : state)
@@ -94,12 +96,8 @@ namespace rtl_bench
9496

9597
void BenchMark::stdFunctionMethodCall_withReturn(benchmark::State& state)
9698
{
97-
static Node* node = new Node();
9899
static std::function getMsg = [=](argStr_t& pMsg) {
99-
auto msgStr = node->getMessage(pMsg);
100-
volatile auto* p = &msgStr;
101-
static_cast<void>(p);
102-
return msgStr;
100+
return (node->*getMessageNodePtr)(pMsg);
103101
};
104102

105103
for (auto _ : state)

RTLBenchmarkApp/src/BenchMark.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <benchmark/benchmark.h>
44

5+
#include <vector>
6+
57
#include "RTLibInterface.h"
68

79
#if defined(_MSC_VER)
@@ -17,44 +19,31 @@ using retStr_t = std::string_view;
1719

1820
#define WORK_LOAD(S) (std::string(S))
1921

20-
2122
namespace rtl_bench
2223
{
2324
static std::optional<std::string> g_msg;
2425

2526
NOINLINE static void sendMessage(argStr_t pMsg)
2627
{
27-
std::string str = WORK_LOAD(pMsg);
28-
volatile auto* p = &str;
29-
static_cast<void>(p);
30-
g_msg = str;
28+
g_msg = WORK_LOAD(pMsg);
3129
}
3230

3331
NOINLINE static retStr_t getMessage(argStr_t pMsg)
3432
{
35-
std::string str = WORK_LOAD(pMsg);
36-
volatile auto* p = &str;
37-
static_cast<void>(p);
38-
g_msg = str;
33+
g_msg = WORK_LOAD(pMsg);
3934
return retStr_t(g_msg->c_str());
4035
}
4136

4237
struct Node
4338
{
4439
NOINLINE void sendMessage(argStr_t pMsg)
4540
{
46-
std::string str = WORK_LOAD(pMsg);
47-
volatile auto* p = &str;
48-
static_cast<void>(p);
49-
g_msg = str;
41+
g_msg = WORK_LOAD(pMsg);
5042
}
5143

5244
NOINLINE retStr_t getMessage(argStr_t pMsg)
5345
{
54-
std::string str = WORK_LOAD(pMsg);
55-
volatile auto* p = &str;
56-
static_cast<void>(p);
57-
g_msg = str;
46+
g_msg = WORK_LOAD(pMsg);
5847
return retStr_t(g_msg->c_str());
5948
}
6049
};

RTLBenchmarkApp/src/main.cpp

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

55
#include "BenchMark.h"
66

7+
auto sendMessagePtr = rtl_bench::sendMessage;
8+
auto getMessagePtr = rtl_bench::getMessage;
9+
auto sendMessageNodePtr = &rtl_bench::Node::sendMessage;
10+
auto getMessageNodePtr = &rtl_bench::Node::getMessage;
711

812
BENCHMARK(rtl_bench::BenchMark::directCall_noReturn);
913
BENCHMARK(rtl_bench::BenchMark::stdFunctionCall_noReturn);

ReflectionTemplateLib/access/inc/Function.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace rtl
5757
{
5858
//simple linear-search, efficient for small set of elements.
5959
for (const auto& functorId : m_functorIds) {
60-
if (functorId.getSignatureId() == pSignatureId) {
60+
if (functorId.getSignatureId() == pSignatureId) [[likely]] {
6161
return functorId.getIndex();
6262
}
6363
}

ReflectionTemplateLib/access/inc/RObject.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace rtl
182182
std::size_t pClonerIndex = m_objectId.m_clonerIndex;
183183
if (pClonerIndex != rtl::index_none)
184184
{
185-
return traits::Cloner::template forwardCall(pClonerIndex, alloc::Heap, pClonerIndex, *this);
185+
return traits::Cloner::template forwardCall<const RObject&>(pClonerIndex, alloc::Heap, pClonerIndex, *this);
186186
}
187187
return { error::CloningDisabled, RObject() };
188188
}
@@ -194,7 +194,7 @@ namespace rtl
194194
std::size_t pClonerIndex = m_objectId.m_clonerIndex;
195195
if (pClonerIndex != rtl::index_none)
196196
{
197-
return traits::Cloner::template forwardCall(pClonerIndex, alloc::Stack, pClonerIndex, *this);
197+
return traits::Cloner::template forwardCall<const RObject&>(pClonerIndex, alloc::Stack, pClonerIndex, *this);
198198
}
199199
return { error::CloningDisabled, RObject() };
200200
}

ReflectionTemplateLib/access/inc/Record.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace rtl {
6565

6666
GETTER_CREF(MethodMap, MethodMap, m_methods)
6767
GETTER_CREF(std::string, RecordName, m_recordName)
68+
6869
/* @method: getMethod
6970
@param: const std::string& (name of the method)
7071
@return: std::optional<Method>
@@ -93,7 +94,7 @@ namespace rtl {
9394
{
9495
static_assert(_alloc != rtl::alloc::None, "Instance cannot be created with 'rtl::alloc::None' option.");
9596
const auto& method = m_methods.at(detail::ctor_name(m_recordName));
96-
std::size_t copyCtorIndex = method.getFunctorIds().at(detail::Index::CopyCtor).getIndex();
97+
std::size_t copyCtorIndex = method.getFunctorIds()[detail::Index::CopyCtor].getIndex();
9798
return method.invokeCtor( _alloc,
9899
std::move(copyCtorIndex),
99100
std::forward<_ctorArgs>(params)...);

ReflectionTemplateLib/access/src/CxxMirror.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ namespace rtl
2525
}
2626
}
2727

28-
error CxxMirror::enableCloning(const RObject& pTarget) const
28+
error CxxMirror::enableCloning(const RObject& pTarget) const
2929
{
3030
const auto& itr = getRecordIdMap().find(pTarget.getTypeId());
3131
if (itr != getRecordIdMap().end())
3232
{
3333
const Record& record = itr->second;
3434
Method ctors = record.getMethod(detail::ctor_name(record.getRecordName())).value();
35-
const_cast<RObject&>(pTarget).m_objectId.m_clonerIndex = ctors.getFunctors().at(detail::Index::CopyCtor).getIndex();
35+
const_cast<RObject&>(pTarget).m_objectId.m_clonerIndex = ctors.getFunctors()[detail::Index::CopyCtor].getIndex();
3636
return error::None;
3737
}
3838
return error::CloningDisabled;

ReflectionTemplateLib/detail/inc/CallReflector.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ namespace rtl::detail {
3030
* gets the lambda vector from '_derivedType' and calls the lambda at given index with '_args'.
3131
* this 'forwardCall' is for calling lambda containing non-member-function and static-member-function functors.
3232
*/ template<class ..._params>
33-
static Return forwardCall(std::size_t pFunctorIndex, _params&&..._args)
33+
FORCE_INLINE static Return forwardCall(std::size_t pFunctorIndex, _params&&..._args)
3434
{
3535
//'getFunctors()' must be implemented by _derivedType (FunctorContainer).
36-
return _derivedType::getFunctors().at(pFunctorIndex)(std::forward<_params>(_args)...);
36+
return _derivedType::getFunctors()[pFunctorIndex](std::forward<_params>(_args)...);
3737
}
3838

3939

@@ -42,10 +42,10 @@ namespace rtl::detail {
4242
* gets the lambda vector from '_derivedType' and calls the lambda at given index with '_args'.
4343
* this 'forwardCall' is for calling lambda containing constructors.
4444
*/ template<class ..._params>
45-
static Return forwardCall(std::size_t pFunctorIndex, rtl::alloc pAllocType, std::size_t pClonerIndex, _params&&..._args)
45+
FORCE_INLINE static Return forwardCall(std::size_t pFunctorIndex, rtl::alloc pAllocType, std::size_t pClonerIndex, _params&&..._args)
4646
{
4747
//'getFunctors()' must be implemented by _derivedType (FunctorContainer).
48-
return _derivedType::getFunctors().at(pFunctorIndex)(pAllocType, pClonerIndex, std::forward<_params>(_args)...);
48+
return _derivedType::getFunctors()[pFunctorIndex](pAllocType, pClonerIndex, std::forward<_params>(_args)...);
4949
}
5050

5151

@@ -54,10 +54,10 @@ namespace rtl::detail {
5454
* gets the lambda vector from '_derivedType' and calls the lambda at given index with '_args'.
5555
* this 'forwardCall' is for calling lambda containing member-function functors.
5656
*/ template<class ..._params>
57-
static Return forwardCall(const rtl::RObject& pTarget, std::size_t pFunctorIndex, _params&&..._args)
57+
FORCE_INLINE static Return forwardCall(const rtl::RObject& pTarget, std::size_t pFunctorIndex, _params&&..._args)
5858
{
5959
//'getMethodFunctors()' is implemented by _derivedType (MethodContainer)
60-
return _derivedType::getMethodFunctors().at(pFunctorIndex)(pTarget, std::forward<_params>(_args)...);
60+
return _derivedType::getMethodFunctors()[pFunctorIndex](pTarget, std::forward<_params>(_args)...);
6161
}
6262
};
6363
}

ReflectionTemplateLib/detail/inc/FunctionCaller.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace rtl::detail
3333
FunctorContainer<_signature...>>;
3434

3535
std::size_t index = m_function.hasSignatureId(Container::getContainerId());
36-
if (index != rtl::index_none) {
36+
if (index != rtl::index_none) [[likely]] {
3737
return Container::template forwardCall<_args...>(index, std::forward<_args>(params)...);
3838
}
3939
return { error::SignatureMismatch, RObject{} };

0 commit comments

Comments
 (0)