Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make consume methods more amenable to optimization #1462

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

DefaultRyan
Copy link
Member

The temporary std::pair introduced with try_as_with_reason sometimes causes spurious temporary dtor calls, depending on the optimization flags used and/or the version of the compiler. This didn't result in an extra refcount bump/release, but it still resulted in generating spurious destructor calls to winrt::Foundation::~IUnknown() as a result of that temporary.

The temporary had already been moved-from into the local variables, so was always zero, but this code bloat, extra function call and branch, when multiplied over thousands of projected methods can still add up.

@DefaultRyan DefaultRyan requested a review from dmachaj December 12, 2024 01:24
@@ -1137,7 +1137,8 @@ namespace cppwinrt
{%
if constexpr (!std::is_same_v<D, %>)
{
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
winrt::hresult _winrt_cast_result_code;
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it is still the recommended coding standard, but for a long time non-const references were disallowed and passing a pointer was preferred. That way the call site is clearly mutating the variable because the & is visible. In this case it is generated code so I guess it is [shrug].

@DefaultRyan DefaultRyan merged commit d7c89b5 into master Dec 12, 2024
75 checks passed
@DefaultRyan DefaultRyan deleted the user/defaultryan/consume-optimize branch December 12, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants