From 38334fdc115c27da7623d927f7b7cbc9dbe63bf5 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 30 Jul 2023 23:18:02 +0200 Subject: [PATCH] PtrToArg::convert() returns const-references where possible, avoids unnecessary copies --- core/variant/method_ptrcall.h | 44 +++++++++++++++++------------------ core/variant/typed_array.h | 3 +-- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/core/variant/method_ptrcall.h b/core/variant/method_ptrcall.h index cbfb9cc257d6..79be85cae68c 100644 --- a/core/variant/method_ptrcall.h +++ b/core/variant/method_ptrcall.h @@ -38,26 +38,26 @@ template struct PtrToArg {}; -#define MAKE_PTRARG(m_type) \ - template <> \ - struct PtrToArg { \ - _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ - return *reinterpret_cast(p_ptr); \ - } \ - typedef m_type EncodeT; \ - _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ - *((m_type *)p_ptr) = p_val; \ - } \ - }; \ - template <> \ - struct PtrToArg { \ - _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ - return *reinterpret_cast(p_ptr); \ - } \ - typedef m_type EncodeT; \ - _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ - *((m_type *)p_ptr) = p_val; \ - } \ +#define MAKE_PTRARG(m_type) \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \ + return *reinterpret_cast(p_ptr); \ + } \ + typedef m_type EncodeT; \ + _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ + *((m_type *)p_ptr) = p_val; \ + } \ + }; \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \ + return *reinterpret_cast(p_ptr); \ + } \ + typedef m_type EncodeT; \ + _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ + *((m_type *)p_ptr) = p_val; \ + } \ } #define MAKE_PTRARGCONV(m_type, m_conv) \ @@ -85,7 +85,7 @@ struct PtrToArg {}; #define MAKE_PTRARG_BY_REFERENCE(m_type) \ template <> \ struct PtrToArg { \ - _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \ return *reinterpret_cast(p_ptr); \ } \ typedef m_type EncodeT; \ @@ -95,7 +95,7 @@ struct PtrToArg {}; }; \ template <> \ struct PtrToArg { \ - _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \ return *reinterpret_cast(p_ptr); \ } \ typedef m_type EncodeT; \ diff --git a/core/variant/typed_array.h b/core/variant/typed_array.h index 98afc7e717ff..420f53fdf131 100644 --- a/core/variant/typed_array.h +++ b/core/variant/typed_array.h @@ -145,8 +145,7 @@ struct PtrToArg> { template struct PtrToArg &> { typedef Array EncodeT; - _FORCE_INLINE_ static TypedArray - convert(const void *p_ptr) { + _FORCE_INLINE_ static TypedArray convert(const void *p_ptr) { return TypedArray(*reinterpret_cast(p_ptr)); } };