Skip to content

Conversation

@jkotas
Copy link
Member

@jkotas jkotas commented Jan 5, 2024

No description provided.

@ghost ghost assigned jkotas Jan 5, 2024
@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 5, 2024
@jkotas jkotas marked this pull request as ready for review January 5, 2024 22:53

END_QCALL;

return (pMatchingMethod != NULL);
Copy link
Member

Choose a reason for hiding this comment

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

This is technically a C++ bool and not a BOOL. We should be consistent here with types.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is implicit conversion from bool to BOOL. We depend on this implicit conversion in thousands of places. Is depending on this implicit conversion bad for some reasons?

(I am fine with adding the explicit conversion in the two places you have commented on, but I do not understand why it helps.)

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT Jan 6, 2024

Choose a reason for hiding this comment

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

Is depending on this implicit conversion bad for some reasons?

No. It has no functional impact and as stated it is correct. My reasoning for it is the historical issue at the interop boundary where bool and BOOL are used interchangably and assumptions creep in. When I see P/Invoked methods I tend to make it explicit so it is clear in code what people are using. Most of the newer interop code uses bool throughout, but at the boundaries occasionally shifts to BOOL in an explicit manner as a way to acknowledge the type being used.

In this case, I think the caller could change the marshal to UnmanagedType.U1 and completely avoid the normalization that typically occurs. The DisableRuntimeMarshalling does impact that but as a demonstration of "best practice" we've been explicit about types.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not pressing on it, but it is inconsistent with the other boolean returns in this PR. In fact, it is the only one that uses the implicit conversion.

Copy link
Member Author

@jkotas jkotas Jan 6, 2024

Choose a reason for hiding this comment

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

Thank you for the discussion. I think I am going to leave it as is. I do not see the explicit ? TRUE : FALSE as adding clarity. I think it is fine to depend on implicit conversions.

I think the caller could change the marshal to UnmanagedType.U1 and completely avoid the normalization

I see this just as a style question. There is no material perf difference between BOOL and bool for custom P/Invokes. C/C++ compilers are very good at optimizing the bool normalizations. Depending on the compiler and the architecture, the difference can be one extra instruction in the worst case. And the second normalization happens as part of LibraryImport marshalling for both UnmanagedType.U1 and UnmanagedType.Bool.

Outside tests, I only see 6 UnmanagedType.U1 for custom P/Invokes in System.Security.Cryptography.Native.Android. All other custom P/Invokes in this repo use UnmanagedType.Bool.


MethodDesc* pMDLeft = COMDelegate::GetMethodDesc(left.Get());
MethodDesc* pMDRight = COMDelegate::GetMethodDesc(right.Get());
fRet = pMDLeft == pMDRight;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fRet = pMDLeft == pMDRight;
fRet = (pMDLeft == pMDRight) ? TRUE : FALSE;

- Delete dead code
- Move data first in RuntimeModule.cs
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants