From a7a108a0905d532c9a5fae7af53d340d1f97c151 Mon Sep 17 00:00:00 2001 From: David Machaj <46852402+dmachaj@users.noreply.github.com.> Date: Mon, 11 Nov 2024 14:45:01 -0800 Subject: [PATCH] Reduce the code size of generated consume methods by skipping casts when the type is already a match --- cppwinrt/code_writers.h | 52 +++++++++++++++++++++++++++++++---------- strings/base_error.h | 3 +-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/cppwinrt/code_writers.h b/cppwinrt/code_writers.h index 0e516b528..f93279a74 100644 --- a/cppwinrt/code_writers.h +++ b/cppwinrt/code_writers.h @@ -1135,10 +1135,18 @@ namespace cppwinrt // immediately while preserving the error code and local variables. format = R"( template auto consume_%::%(%) const noexcept {% - auto const& castedResult = static_cast<% const&>(static_cast(*this)); - auto const abiType = *(abi_t<%>**)&castedResult; - check_cast_result(abiType); - abiType->%(%);% + if constexpr (!std::is_same_v) + { + auto const& castedResult = static_cast<% const&>(static_cast(*this)); + auto const abiType = *(abi_t<%>**)&castedResult; + check_cast_result(abiType); + abiType->%(%); + } + else + { + auto const abiType = *(abi_t<%>**)this; + abiType->%(%); + }% } )"; } @@ -1146,10 +1154,18 @@ namespace cppwinrt { format = R"( template auto consume_%::%(%) const noexcept {% - auto const& castedResult = static_cast<% const&>(static_cast(*this)); - auto const abiType = *(abi_t<%>**)&castedResult; - check_cast_result(abiType); - WINRT_VERIFY_(0, abiType->%(%));% + if constexpr (!std::is_same_v) + { + auto const& castedResult = static_cast<% const&>(static_cast(*this)); + auto const abiType = *(abi_t<%>**)&castedResult; + check_cast_result(abiType); + WINRT_VERIFY_(0, abiType->%(%)); + } + else + { + auto const abiType = *(abi_t<%>**)this; + WINRT_VERIFY_(0, abiType->%(%)); + }% } )"; } @@ -1158,10 +1174,18 @@ namespace cppwinrt { format = R"( template auto consume_%::%(%) const {% - auto const& castedResult = static_cast<% const&>(static_cast(*this)); - auto const abiType = *(abi_t<%>**)&castedResult; - check_cast_result(abiType); - check_hresult(abiType->%(%));% + if constexpr (!std::is_same_v) + { + auto const& castedResult = static_cast<% const&>(static_cast(*this)); + auto const abiType = *(abi_t<%>**)&castedResult; + check_cast_result(abiType); + check_hresult(abiType->%(%)); + } + else + { + auto const abiType = *(abi_t<%>**)this; + check_hresult(abiType->%(%)); + }% } )"; } @@ -1175,6 +1199,10 @@ namespace cppwinrt bind(signature, false), type, type, + type, + get_abi_name(method), + bind(signature), + type, get_abi_name(method), bind(signature), bind(signature)); diff --git a/strings/base_error.h b/strings/base_error.h index 21010ccb2..9b9624277 100644 --- a/strings/base_error.h +++ b/strings/base_error.h @@ -539,8 +539,7 @@ namespace winrt::impl return result; } - template - WINRT_IMPL_NOINLINE void check_cast_result(T* from, winrt::impl::slim_source_location const& sourceInformation = winrt::impl::slim_source_location::current()) + inline WINRT_IMPL_NOINLINE void check_cast_result(void* from, winrt::impl::slim_source_location const& sourceInformation = winrt::impl::slim_source_location::current()) { if (!from) {