Skip to content

Commit 9eba414

Browse files
committed
method calling optimized, inlinig, compiler hints.
1 parent 7bfc6c8 commit 9eba414

File tree

6 files changed

+59
-73
lines changed

6 files changed

+59
-73
lines changed

ReflectionTemplateLib/access/inc/Function.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace rtl
5353
* a 'Function' object may be associated with multiple functors in case of overloads.
5454
* every overload will have unique 'FunctorId', contained by one 'Function' object.
5555
* given signatureId is compared against the signatureId of all overloads registered.
56-
*/ inline const std::size_t Function::hasSignatureId(const std::size_t pSignatureId) const
56+
*/ FORCE_INLINE const std::size_t Function::hasSignatureId(const std::size_t pSignatureId) const
5757
{
5858
//simple linear-search, efficient for small set of elements.
5959
for (const auto& functorId : m_functorIds) {

ReflectionTemplateLib/access/inc/Method.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ namespace rtl {
6464
friend detail::CxxReflection;
6565

6666
template<class ..._signature>
67-
friend class detail::DefaultInvoker;
67+
friend struct detail::DefaultInvoker;
6868

6969
template<class ..._signature>
70-
friend class detail::NonConstInvoker;
70+
friend struct detail::NonConstInvoker;
7171

7272
public:
7373

ReflectionTemplateLib/access/inc/Method.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
namespace rtl
1717
{
1818
template<class ..._signature>
19-
inline const detail::DefaultInvoker<_signature...> Method::bind(const RObject& pTarget) const
19+
FORCE_INLINE const detail::DefaultInvoker<_signature...> Method::bind(const RObject& pTarget) const
2020
{
21-
return detail::DefaultInvoker<_signature...>(*this, pTarget);
21+
return detail::DefaultInvoker<_signature...>{ this, &pTarget };
2222
}
2323

2424

2525
template<class ..._signature>
26-
inline const detail::NonConstInvoker<_signature...> Method::bind(constCast<RObject>&& pTarget) const
26+
FORCE_INLINE const detail::NonConstInvoker<_signature...> Method::bind(constCast<RObject>&& pTarget) const
2727
{
28-
return detail::NonConstInvoker<_signature...>(*this, pTarget.m_target);
28+
return detail::NonConstInvoker<_signature...>{ this, &pTarget.m_target };
2929
}
3030

3131

@@ -51,17 +51,17 @@ namespace rtl
5151
{
5252
switch (getQualifier())
5353
{
54-
case detail::methodQ::None: {
55-
return Function::hasSignature<_args...>();
56-
}
57-
case detail::methodQ::NonConst: {
58-
using Container = detail::MethodContainer<detail::methodQ::NonConst, _args...>;
59-
return (hasSignatureId(Container::getContainerId()) != -1);
60-
}
61-
case detail::methodQ::Const: {
62-
using Container = detail::MethodContainer<detail::methodQ::Const, _args...>;
63-
return (hasSignatureId(Container::getContainerId()) != -1);
64-
}
54+
case detail::methodQ::None: {
55+
return Function::hasSignature<_args...>();
56+
}
57+
case detail::methodQ::NonConst: {
58+
using Container = detail::MethodContainer<detail::methodQ::NonConst, _args...>;
59+
return (hasSignatureId(Container::getContainerId()) != -1);
60+
}
61+
case detail::methodQ::Const: {
62+
using Container = detail::MethodContainer<detail::methodQ::Const, _args...>;
63+
return (hasSignatureId(Container::getContainerId()) != -1);
64+
}
6565
}
6666
return false;
6767
}

ReflectionTemplateLib/access/inc/RObject.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ namespace rtl
184184
{
185185
return traits::Cloner::template forwardCall<const RObject&>(pClonerIndex, alloc::Heap, pClonerIndex, *this);
186186
}
187-
return { error::CloningDisabled, RObject() };
187+
return { error::CloningDisabled, RObject{} };
188188
}
189189

190190

@@ -196,7 +196,7 @@ namespace rtl
196196
{
197197
return traits::Cloner::template forwardCall<const RObject&>(pClonerIndex, alloc::Stack, pClonerIndex, *this);
198198
}
199-
return { error::CloningDisabled, RObject() };
199+
return { error::CloningDisabled, RObject{} };
200200
}
201201

202202

ReflectionTemplateLib/detail/inc/MethodInvoker.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ namespace rtl {
2020
namespace rtl::detail {
2121

2222
template<class ..._signature>
23-
class DefaultInvoker
23+
struct DefaultInvoker
2424
{
2525
//the method to be called.
26-
const Method& m_method;
26+
const Method* m_method;
2727

2828
//the object on which, the method needs to be called.
29-
const RObject& m_target;
30-
31-
DefaultInvoker(const Method& pMethod, const RObject& pTarget);
29+
const RObject* m_target;
3230

3331
template<class ..._invokSignature>
3432
struct Invoker {
@@ -47,15 +45,13 @@ namespace rtl::detail {
4745

4846

4947
template<class ..._signature>
50-
class NonConstInvoker
48+
struct NonConstInvoker
5149
{
5250
//the method to be called.
53-
const Method& m_method;
51+
const Method* m_method;
5452

5553
//the object on which, the method needs to be called.
56-
const RObject& m_target;
57-
58-
NonConstInvoker(const Method& pMethod, const RObject& pTarget);
54+
const RObject* m_target;
5955

6056
template<class ..._invokSignature>
6157
struct Invoker {

ReflectionTemplateLib/detail/inc/MethodInvoker.hpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,35 @@
1818

1919
namespace rtl::detail
2020
{
21-
//DefaultInvoker, holds const-ref of the 'Method' and 'RObject' on which it will be invoked.
22-
template<class ..._signature>
23-
inline DefaultInvoker<_signature...>::DefaultInvoker(const Method& pMethod, const RObject& pTarget)
24-
: m_method(pMethod)
25-
, m_target(pTarget) {
26-
}
27-
28-
2921
/* @method: call()
3022
@params: params... (corresponding to functor associated with 'm_method')
3123
@return: RObject, indicating success of the reflected call.
3224
* invokes non-static-member-function functor associated with 'm_method' on object 'm_target'.
3325
*/ template<class ..._signature>
3426
template<class ..._args>
35-
inline Return DefaultInvoker<_signature...>::call(_args&& ...params) const noexcept
27+
FORCE_INLINE Return DefaultInvoker<_signature...>::call(_args&& ...params) const noexcept
3628
{
3729
//Only static-member-functions have Qualifier- 'methodQ::None'
38-
if (m_method.getQualifier() == methodQ::None) {
39-
return static_cast<Function>(m_method).bind().call(std::forward<_args>(params)...);
30+
if (m_method->getQualifier() == methodQ::None) [[unlikely]] {
31+
return static_cast<Function>(*m_method).bind().call(std::forward<_args>(params)...);
4032
}
41-
if (m_target.isEmpty()) {
33+
else if (m_target->isEmpty()) [[unlikely]] {
4234
//if the target is empty.
43-
return { error::EmptyRObject, RObject{ } };
35+
return { error::EmptyRObject, RObject{} };
4436
}
45-
if (m_target.getTypeId() != m_method.getRecordTypeId()) {
37+
else if (m_target->getTypeId() != m_method->getRecordTypeId()) [[unlikely]] {
4638
//if the m_target's type-id & type-id of the 'class/struct' owner of the associated functor(m_method's) do not match.
47-
return { error::TargetMismatch, RObject{ } };
48-
}
49-
if constexpr (sizeof...(_signature) == 0) {
50-
// executes when bind doesn't have any explicit signature types specified. (e.g. perfect-forwaring)
51-
return Invoker<traits::remove_const_n_ref_t<_args>...>::invoke(m_method, m_target, std::forward<_args>(params)...);
39+
return { error::TargetMismatch, RObject{} };
5240
}
53-
else {
54-
return Invoker<_signature...>::invoke(m_method, m_target, std::forward<_args>(params)...);
41+
else [[likely]]
42+
{
43+
if constexpr (sizeof...(_signature) == 0) {
44+
// executes when bind doesn't have any explicit signature types specified. (e.g. perfect-forwaring)
45+
return Invoker<traits::remove_const_n_ref_t<_args>...>::invoke(*m_method, *m_target, std::forward<_args>(params)...);
46+
}
47+
else {
48+
return Invoker<_signature...>::invoke(*m_method, *m_target, std::forward<_args>(params)...);
49+
}
5550
}
5651
}
5752

@@ -60,15 +55,15 @@ namespace rtl::detail
6055
template<class ..._signature>
6156
template<class ..._invokSignature>
6257
template<class ..._args>
63-
inline Return
58+
FORCE_INLINE Return
6459
DefaultInvoker<_signature...>::Invoker<_invokSignature...>::invoke(const Method& pMethod,
6560
const RObject& pTarget,
6661
_args&&... params)
6762
{
6863
using containerConst = detail::MethodContainer<detail::methodQ::Const, _invokSignature...>;
6964
std::size_t constMethodIndex = pMethod.hasSignatureId(containerConst::getContainerId());
7065

71-
if (constMethodIndex != rtl::index_none)
66+
if (constMethodIndex != rtl::index_none) [[likely]]
7267
{
7368
return containerConst::template forwardCall<_args...>(pTarget, constMethodIndex, std::forward<_args>(params)...);
7469
}
@@ -92,38 +87,33 @@ namespace rtl::detail
9287

9388
namespace rtl::detail
9489
{
95-
//NonConstInvoker, holds const-ref of the 'Method' and 'RObject' on which it will be invoked.
96-
template<class ..._signature>
97-
inline NonConstInvoker<_signature...>::NonConstInvoker(const Method& pMethod, const RObject& pTarget)
98-
: m_method(pMethod)
99-
, m_target(pTarget) {
100-
}
101-
102-
10390
/* @method: call()
10491
@params: params... (corresponding to functor associated with 'm_method')
10592
@return: RObject, indicating success of the reflected call.
10693
* invokes non-static-member-function functor associated with 'm_method' on object 'm_target'.
10794
*/ template<class ..._signature>
10895
template<class ..._args>
109-
inline Return NonConstInvoker<_signature...>::call(_args&& ...params) const noexcept
96+
FORCE_INLINE Return NonConstInvoker<_signature...>::call(_args&& ...params) const noexcept
11097
{
111-
if (m_method.getQualifier() == methodQ::None) {
112-
return static_cast<Function>(m_method).bind().call(std::forward<_args>(params)...);
98+
if (m_method->getQualifier() == methodQ::None) [[unlikely]] {
99+
return static_cast<Function>(*m_method).bind().call(std::forward<_args>(params)...);
113100
}
114-
if (m_target.isEmpty()) {
101+
else if (m_target->isEmpty()) [[unlikely]] {
115102
//if the target is empty.
116103
return { error::EmptyRObject, RObject{} };
117104
}
118-
if (m_target.getTypeId() != m_method.getRecordTypeId()) {
105+
else if (m_target->getTypeId() != m_method->getRecordTypeId()) [[unlikely]] {
119106
//if the m_target's type-id & type-id of the 'class/struct' owner of the associated functor(m_method's) do not match.
120107
return { error::TargetMismatch, RObject{} };
121108
}
122-
if constexpr (sizeof...(_signature) == 0) {
123-
return Invoker<traits::remove_const_n_ref_t<_args>...>::invoke(m_method, m_target, std::forward<_args>(params)...);
124-
}
125-
else {
126-
return Invoker<_signature...>::invoke(m_method, m_target, std::forward<_args>(params)...);
109+
else [[likely]]
110+
{
111+
if constexpr (sizeof...(_signature) == 0) {
112+
return Invoker<traits::remove_const_n_ref_t<_args>...>::invoke(*m_method, *m_target, std::forward<_args>(params)...);
113+
}
114+
else {
115+
return Invoker<_signature...>::invoke(*m_method, *m_target, std::forward<_args>(params)...);
116+
}
127117
}
128118
}
129119

@@ -132,14 +122,14 @@ namespace rtl::detail
132122
template<class ..._signature>
133123
template<class ..._invokSignature>
134124
template<class ..._args>
135-
inline Return
125+
FORCE_INLINE Return
136126
NonConstInvoker<_signature...>::Invoker<_invokSignature...>::invoke(const Method& pMethod,
137127
const RObject& pTarget,
138128
_args&&... params)
139129
{
140130
using container0 = detail::MethodContainer<detail::methodQ::NonConst, _invokSignature...>;
141131
const std::size_t index = pMethod.hasSignatureId(container0::getContainerId());
142-
if (index != rtl::index_none) {
132+
if (index != rtl::index_none) [[likely]] {
143133
return container0::template forwardCall<_args...>(pTarget, index, std::forward<_args>(params)...);
144134
}
145135
else

0 commit comments

Comments
 (0)